Skip to content

Commit d5477f4

Browse files
committed
IfconfigCommand for network interface information
- Displays network interface information similar to the ifconfig utility. - Handles the enumeration of network interfaces, displaying details like IP addresses, MAC address, MTU, status, and more. - Error handling for unexpected flag inputs.
1 parent 4ef452e commit d5477f4

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed
Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,54 @@
11
package com.unixtools.command.networking;
22

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+
}
554
}

src/com/unixtools/core/CommandFactory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public static Command getCommand(String commandName) {
3737
return new CurlCommand();
3838
case "wget":
3939
return new WgetCommand();
40+
case "ifconfig":
41+
return new IfconfigCommand();
4042
default:
4143
return null;
4244
}

0 commit comments

Comments
 (0)