|
| 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