Skip to content

Commit 432392b

Browse files
feature: IpConfig command implemented and tested
1 parent eaf7cb4 commit 432392b

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

src/main/java/com/mycmd/App.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,6 @@ private static void registerCommands(Map<String, Command> commands) {
8181
commands.put("pwd", new PwdCommand());
8282
commands.put("uptime", new UptimeCommand());
8383
commands.put("clearhistory", new ClearHistoryCommand());
84+
commands.put("ipconfig", new IpConfig());
8485
}
8586
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.mycmd.commands;
2+
3+
import java.io.IOException;
4+
import java.net.InetAddress;
5+
import java.net.NetworkInterface;
6+
import java.net.SocketException;
7+
import java.util.Enumeration;
8+
import com.mycmd.Command;
9+
import com.mycmd.ShellContext;
10+
11+
/**
12+
* Implements the "ipconfig" command for MyCMD.
13+
*/
14+
public class IpConfig implements Command {
15+
16+
@Override
17+
public void execute(String[] args, ShellContext context) throws IOException {
18+
try {
19+
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
20+
while (interfaces.hasMoreElements()) {
21+
NetworkInterface ni = interfaces.nextElement();
22+
System.out.println("Interface: " + ni.getDisplayName());
23+
24+
Enumeration<InetAddress> addresses = ni.getInetAddresses();
25+
while (addresses.hasMoreElements()) {
26+
InetAddress addr = addresses.nextElement();
27+
System.out.println(" IP Address: " + addr.getHostAddress());
28+
}
29+
30+
System.out.println(); // blank line between interfaces
31+
}
32+
} catch (SocketException e) {
33+
throw new IOException("Failed to get network interfaces", e);
34+
}
35+
}
36+
37+
@Override
38+
public String description() {
39+
return "Displays all network interfaces and their IP addresses";
40+
}
41+
42+
@Override
43+
public String usage() {
44+
return "ipconfig";
45+
}
46+
}

0 commit comments

Comments
 (0)