java实现内外网同时上路由设置
执行前保证:内网网都打开,网线连上。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import com.tomrrow.collect.util.RegExMatcher;
public class CommandRuntime {
/**
* @param args
* @throws UnsupportedEncodingException
*/
public static void main(String[] args) throws UnsupportedEncodingException {
String wifiName = "Intel(R) Centrino(R) Advanced-N 6205";// 无线网卡名称
String yitaName = "Intel(R) 82579LM Gigabit Network Connection";//有线网卡名称
String internetRoute = "0.0.0.0";//外网路由
String internetGateWayEnd = "1";//外网网关第四位数字
String internetSubMask = "0.0.0.0";//外网子网掩码
String[] intranetRoute = {"10.0.0.0","132.0.0.0","134.0.0.0"};//内网要范围的网段
String[] intranetGateWay = {"10.72.45.1","10.72.67.120","10.72.45.1"};//内网网段分别对应的内网网关
String intranetSubMask = "255.0.0.0";//内网子网掩码
String wifiIp = "";//当前无线网卡的ip
String yitaIp = "";
String wifiInterface = "";
String yitaInterface = "";
String internetGateWay = "";
List<String> ret = CommandRuntime.execCommand("route print");//预览当前路由表
for (String s : ret){
if (s.trim().endsWith(yitaName)){//找到宽带网卡系统注册接口id
yitaInterface = s.substring(0, s.indexOf("…")).trim();
}else if (s.trim().endsWith(wifiName)){//找到无线网卡系统注册接口id
wifiInterface = s.substring(0, s.indexOf("…")).trim();
}
}
ret = CommandRuntime.execCommand("route print -4 0.0.0.0"); // 获取无线ip(Ipv4)
for (String s : ret){
if (s.trim().indexOf("0.0.0.0") > -1){
wifiIp = RegExMatcher.regexReplace(s.trim(),"[‘ ‘]+", ",").split(",")[3];
System.out.println(RegExMatcher.regexReplace(s.trim(),"[‘ ‘]+", ","));
internetGateWay = wifiIp.substring(0, wifiIp.lastIndexOf(".")+1) + internetGateWayEnd;
}
}
String[] commandBat = new String[(intranetRoute.length + 1)*2];//内网路由数+1个外网路由,先删除 再添加
// 内网路由
for (int x = 0; x < intranetRoute.length; x++){
commandBat[2*x] = "route delete " + intranetRoute[x];
commandBat[2*x+1] = "route add " + intranetRoute[x] + " mask " + intranetSubMask + " " + intranetGateWay[x] + " metric 20 if " + yitaInterface;
}
// 外网路由
commandBat[commandBat.length-2] = "route delete 0.0.0.0";
commandBat[commandBat.length-1] = "route add 0.0.0.0 mask 0.0.0.0 " + internetGateWay + " metric 1 if " + wifiInterface;
for (String c : commandBat){
System.out.println(c);
ret = CommandRuntime.execCommand(c);
System.out.println(ret);
}
}
/**
* 执行shell,返回执行成功结果 同时打印命令返回结果
* @param command
* @return
* @throws UnsupportedEncodingException
*/
public static List<String> execCommand(String command) throws UnsupportedEncodingException{
Process process = null;
BufferedReader input = null;
List<String> sd = new ArrayList<String>();
try {
try{
process = Runtime.getRuntime().exec(command);
input = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));
String line = "";
while ((line = input.readLine()) != null) {
sd.add(line);
}
}catch (Throwable e) {
e.printStackTrace();
}
} finally{
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sd;
}
}
声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 嗅谱网
转载请注明:转自《java实现内外网同时上路由设置》
本文地址:http://www.xiupu.net/archives-6539.html
关注公众号:
微信赞赏
支付宝赞赏