-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathNetworkConfig.java
More file actions
118 lines (105 loc) · 3.1 KB
/
Copy pathNetworkConfig.java
File metadata and controls
118 lines (105 loc) · 3.1 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package javaforce.vm;
import java.io.*;
import javaforce.*;
import javaforce.net.*;
/** NetworkConfig
*
* @author pquiring
*/
public class NetworkConfig implements Serializable {
private static final long serialVersionUID = 1L;
public String name;
public String ip;
public String netmask;
public String mac;
public transient NetworkState state;
public NetworkConfig(String name) {
this.name = name;
state = new NetworkState();
}
/** Return [name, ip/mask, mac, link] */
public String[] getState() {
return new String[] {name, state.ip + "/" + state.netmask, state.mac, state.link};
}
public static void getInfo(NetworkConfig[] nics) {
ShellProcess p = new ShellProcess();
p.keepOutput(true);
String output = p.run(new String[] {"/usr/bin/ip", "addr"}, true);
if (output == null) return;
for(NetworkConfig nic : nics) {
nic.state = new NetworkState();
}
/*
1: eth0: <...> state UP/DOWN
link/ether 00:11:22:33:44:55 brd ff:ff:ff:ff:ff:ff ...
inet 192.168.1.2/24 ...
2: eth2: <...> state UP/DOWN
link/ether 00:11:22:33:44:55 brd ff:ff:ff:ff:ff:ff ...
inet 192.168.2.2/24 ...
*/
String[] lns = output.split("\n");
NetworkConfig nic = null;
for(int a=0;a<lns.length;a++) {
String ln = lns[a].trim();
if (ln.length() == 0) continue;
char start = ln.charAt(0);
if (start >= '0' && start <= '9') {
String[] fs = ln.split("[:]");
String name = fs[1].trim();
nic = null;
for(NetworkConfig n : nics) {
if (n.name.equals(name)) {
nic = n;
break;
}
}
if (nic != null) {
nic.state.link = "down";
int i1 = ln.indexOf('<');
int i2 = ln.indexOf('>');
if (i1 > 0 && i2 > 0) {
String[] ss = ln.substring(i1+1,i2).split("[,]");
for(String s : ss) {
switch (s) {
case "UP": nic.state.link = "up"; break;
}
}
}
}
}
if (nic != null && ln.startsWith("link/ether")) {
//MAC
String[] f = ln.split("[ ]");
nic.state.mac = f[1];
}
if (nic != null && ln.startsWith("inet ")) {
//IP4/NETMASK
String[] f = ln.split("[ ]");
String ip_mask = f[1];
int slash = ip_mask.indexOf('/');
if (slash == -1) continue;
nic.state.ip = ip_mask.substring(0, slash);
int cidr = Integer.valueOf(ip_mask.substring(slash + 1));
nic.state.netmask = Subnet4.fromCIDR(cidr);
}
}
}
public boolean set_ip() {
//assign ip address
ShellProcess p = new ShellProcess();
p.keepOutput(true);
p.run(new String[] {"/usr/bin/ip", "addr", "add", ip + "/" + netmask, "dev", name}, true);
return p.getErrorLevel() == 0;
}
private boolean link(String dir) {
ShellProcess sp = new ShellProcess();
sp.run(new String[] {"ip", "link", "set", name, dir}, true);
return sp.getErrorLevel() == 0;
}
public boolean link_up() {
return link("up");
}
public boolean link_down() {
return link("down");
}
}