ABcv.java
2.45 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
package com.bootdo.common;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.URL;
import java.util.Enumeration;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ZipUtil;
public class ABcv {
public static void main(String[] args) throws Exception{
System.out.println("本机vpn-IP:" + getIpAddress());
String vpnIp = getIpAddress();
execCmdStr(" route add 192.168.0.145 mask 255.255.255.255 "+vpnIp);
execCmdStr(" route add 39.97.246.118 mask 255.255.255.255 "+vpnIp);
execCmdStr(" route add 192.168.0.105 mask 255.255.255.255 "+vpnIp);
execCmdStr(" ipconfig ");
}
//获取VPN-ip
public static String getIpAddress() {
try {
Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
//本机vpn-ip
if(netInterface.getDisplayName().contains("VPN")) {
Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
ip = addresses.nextElement();
if (ip != null && ip instanceof Inet4Address) {
return ip.getHostAddress();
}
}
}
}
} catch (Exception e) {
System.err.println("IP地址获取失败" + e.toString());
}
return "";
}
//执行cmd命令
public static void execCmdStr(String cmdStr) throws Exception{
String cmd = cmdStr;
String cdmAdminPowerPre= "c:/nircmd.exe elevate";
try {
Process process = Runtime.getRuntime().exec(cdmAdminPowerPre+cmd);
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is,"GBK");
BufferedReader br = new BufferedReader(isr);
String content = br.readLine();
while (content != null) {
System.out.println(content);
content = br.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}