ABcv.java 2.45 KB
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();
        }	
	}

	
}