Skip to content

Commit 067e962

Browse files
Merge pull request #64 from Kaveeshakavindi/feat/Kaveesha/ipconfig
feature: IpConfig command implemented and tested
2 parents f119906 + 5f66472 commit 067e962

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
@@ -100,6 +100,7 @@ private static void registerCommands(Map<String, Command> commands) {
100100
commands.put("pwd", new PwdCommand());
101101
commands.put("uptime", new UptimeCommand());
102102
commands.put("clearhistory", new ClearHistoryCommand());
103+
commands.put("ipconfig", new IpConfig());
103104
commands.put("alias", new AliasCommand());
104105
commands.put("unalias", new UnaliasCommand());
105106
commands.put("tasklist", new TasklistCommand());
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)