|
1 | 1 | package com.unixtools.command.networking;
|
2 | 2 |
|
3 |
| -public class IfconfigCommand { |
4 |
| - |
| 3 | +import com.unixtools.core.Command; |
| 4 | + |
| 5 | +import java.net.*; |
| 6 | +import java.util.Enumeration; |
| 7 | + |
| 8 | +public class IfconfigCommand implements Command { |
| 9 | + |
| 10 | + @Override |
| 11 | + public void execute(String[] args) { |
| 12 | + if (args.length > 0) { |
| 13 | + System.out.println("ifconfig: Unexpected flags provided. This command does not take flags."); |
| 14 | + return; |
| 15 | + } |
| 16 | + |
| 17 | + try { |
| 18 | + Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); |
| 19 | + while (interfaces.hasMoreElements()) { |
| 20 | + NetworkInterface networkInterface = interfaces.nextElement(); |
| 21 | + displayInterfaceInformation(networkInterface); |
| 22 | + } |
| 23 | + } catch (SocketException e) { |
| 24 | + System.out.println("Error retrieving network interface information: " + e.getMessage()); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + private void displayInterfaceInformation(NetworkInterface networkInterface) throws SocketException { |
| 29 | + System.out.println("Interface: " + networkInterface.getDisplayName()); |
| 30 | + System.out.println("Name: " + networkInterface.getName()); |
| 31 | + |
| 32 | + byte[] mac = networkInterface.getHardwareAddress(); |
| 33 | + if (mac != null) { |
| 34 | + StringBuilder macAddress = new StringBuilder(); |
| 35 | + for (int i = 0; i < mac.length; i++) { |
| 36 | + macAddress.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "")); |
| 37 | + } |
| 38 | + System.out.println("MAC Address: " + macAddress.toString()); |
| 39 | + } |
| 40 | + |
| 41 | + System.out.println("MTU: " + networkInterface.getMTU()); |
| 42 | + System.out.println("Up: " + networkInterface.isUp()); |
| 43 | + System.out.println("Loopback: " + networkInterface.isLoopback()); |
| 44 | + System.out.println("Virtual: " + networkInterface.isVirtual()); |
| 45 | + |
| 46 | + Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses(); |
| 47 | + while (inetAddresses.hasMoreElements()) { |
| 48 | + InetAddress inetAddress = inetAddresses.nextElement(); |
| 49 | + System.out.println("IP Address: " + inetAddress.getHostAddress()); |
| 50 | + } |
| 51 | + |
| 52 | + System.out.println(); |
| 53 | + } |
5 | 54 | }
|
0 commit comments