-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathTestPCAP.java
More file actions
95 lines (87 loc) · 2.18 KB
/
Copy pathTestPCAP.java
File metadata and controls
95 lines (87 loc) · 2.18 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package javaforce.tests;
import javaforce.*;
import javaforce.net.*;
/** Tests PCAP
*
* @author pquiring
*/
public class TestPCAP {
private static String nic_ip = null;
private static String timeout = "2000";
public static void main(String[] args) {
if (args.length == 0) {
JFLog.log("Usage : PacketCapture cmd [...] [opts]");
JFLog.log(" cmd : list");
JFLog.log(" : arp {ip}");
JFLog.log(" opts : -i interface_ip");
JFLog.log(" : -t timeout");
return;
}
try {
switch (args[0]) {
case "list":
cmd_list();
break;
case "arp":
parse_opts(args);
cmd_arp(args[1]);
break;
default:
JFLog.log("Unknown cmd:" + args[0]);
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void cmd_list() {
PacketCapture cap = new PacketCapture();
String[] ifs = cap.listLocalInterfaces();
for(int a=0;a<ifs.length;a++) {
JFLog.log(ifs[a]);
}
}
public static void cmd_arp(String ip) {
PacketCapture cap = new PacketCapture();
String[] nics = cap.listLocalInterfaces();
int nicidx = 0;
if (nic_ip != null) {
nicidx = -1;
for(int a=0;a<nics.length;a++) {
String sif = nics[a];
String[] pif = sif.split(",");
for(int b=1;b<pif.length;b++) {
if (pif[b].equals(nic_ip)) {
nicidx = a;
break;
}
}
}
}
if (nicidx == -1) {
JFLog.log("Interface not found for IP:" + nic_ip);
return;
}
String sif = nics[nicidx];
String[] pif = sif.split(",");
long id = cap.start(pif[0], pif[1]);
cap.compile(id, "arp");
byte[] mac = PacketCapture.arp(id, ip, Integer.valueOf(timeout));
cap.stop(id);
JFLog.log("MAC=" + PacketCapture.toString(mac));
}
private static void parse_opts(String[] args) {
for(int a=0;a<args.length;a++) {
if (args[a].startsWith("-")) {
switch (args[a]) {
case "-i":
nic_ip = args[a+1];
break;
case "-t":
timeout = args[a+1];
break;
}
}
}
}
}