Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/com/mycmd/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ private static void registerCommands(Map<String, Command> commands) {
commands.put("pwd", new PwdCommand());
commands.put("uptime", new UptimeCommand());
commands.put("clearhistory", new ClearHistoryCommand());
commands.put("ipconfig", new IpConfig());
commands.put("alias", new AliasCommand());
commands.put("unalias", new UnaliasCommand());
}
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/com/mycmd/commands/IpConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.mycmd.commands;

import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import com.mycmd.Command;
import com.mycmd.ShellContext;

/**
* Implements the "ipconfig" command for MyCMD.
*/
public class IpConfig implements Command {

@Override
public void execute(String[] args, ShellContext context) throws IOException {
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface ni = interfaces.nextElement();
System.out.println("Interface: " + ni.getDisplayName());

Enumeration<InetAddress> addresses = ni.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress addr = addresses.nextElement();
System.out.println(" IP Address: " + addr.getHostAddress());
}

System.out.println(); // blank line between interfaces
}
} catch (SocketException e) {
throw new IOException("Failed to get network interfaces", e);
}
}

@Override
public String description() {
return "Displays all network interfaces and their IP addresses";
}

@Override
public String usage() {
return "ipconfig";
}
}