forked from Drive-for-Java/MyCMD
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArpCommand.java
More file actions
69 lines (55 loc) · 2.11 KB
/
Copy pathArpCommand.java
File metadata and controls
69 lines (55 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.mycmd.commands;
import com.mycmd.Command;
import com.mycmd.ShellContext;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Displays and modifies the IP-to-Physical address translation tables (ARP cache).
*
* Usage:
* - arp -a : Display ARP cache
*/
public class ArpCommand implements Command {
@Override
public void execute(String[] args, ShellContext context) throws IOException {
try {
StringBuilder cmdBuilder = new StringBuilder("arp");
for (String arg : args) {
cmdBuilder.append(" ").append(arg);
}
// Default to -a if no args provided
if (args.length == 0) {
cmdBuilder.append(" -a");
}
ProcessBuilder pb = new ProcessBuilder();
String os = System.getProperty("os.name").toLowerCase();
if (os.contains("win")) {
pb.command("cmd.exe", "/c", cmdBuilder.toString());
} else {
pb.command("sh", "-c", cmdBuilder.toString());
}
Process process = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
while ((line = errorReader.readLine()) != null) {
System.err.println(line);
}
process.waitFor();
} catch (Exception e) {
System.out.println("Error executing arp: " + e.getMessage());
}
}
@Override
public String description() {
return "Displays and modifies the IP-to-Physical address translation tables.";
}
@Override
public String usage() {
return "arp [-a] [-d ip_addr] [-s ip_addr eth_addr]";
}
}