java实现动态切换上网IP (ADSL拨号上网)

java实现动态切换上网IP (ADSL拨号上网)
转自:http://sesame.iteye.com/blog/434088

动态切换IP的实现主是也由Windows的rasdial命令提供的,其实不是java的功劳,java只是调用一下bat脚本而已:

rasdial命令:

拨号

语法: rasdial  连接名称 username password   
实例: rasdial 我的宽带 hzhz1234567890 dfdfdfdfdf  


断网

语法:rasdial  连接名称 /disconnect     
实例: rasdial 宽带  /disconnect         


java程序调用rasdial命令:

 
package com.sesame.network;   
  
import java.io.BufferedReader;   
import java.io.InputStreamReader;   
  
public class ConnectNetWork {   
  
    /**  
     * 执行CMD命令,并返回String字符串  
     */  
    public static String executeCmd(String strCmd) throws Exception {   
        Process p = Runtime.getRuntime().exec("cmd /c " + strCmd);   
        StringBuilder sbCmd = new StringBuilder();   
        BufferedReader br = new BufferedReader(new InputStreamReader(p   
                .getInputStream()));   
        String line;   
        while ((line = br.readLine()) != null) {   
            sbCmd.append(line + "\n");   
        }   
        return sbCmd.toString();   
    }   
  
    /**  
     * 连接ADSL  
     */  
    public static boolean connAdsl(String adslTitle, String adslName, String adslPass) throws Exception {   
        System.out.println("正在建立连接.");   
        String adslCmd = "rasdial " + adslTitle + " " + adslName + " "  
                + adslPass;   
        String tempCmd = executeCmd(adslCmd);   
        // 判断是否连接成功   
        if (tempCmd.indexOf("已连接") > 0) {   
            System.out.println("已成功建立连接.");   
            return true;   
        } else {   
            System.err.println(tempCmd);   
            System.err.println("建立连接失败");   
            return false;   
        }   
    }   
  
    /**  
     * 断开ADSL  
     */  
    public static boolean cutAdsl(String adslTitle) throws Exception {   
        String cutAdsl = "rasdial " + adslTitle + " /disconnect";   
        String result = executeCmd(cutAdsl);   
          
        if (result.indexOf("没有连接")!=-1){   
            System.err.println(adslTitle + "连接不存在!");   
            return false;   
        } else {   
            System.out.println("连接已断开");   
            return true;   
        }   
    }   
      
    public static void main(String[] args) throws Exception {   
        connAdsl("宽带","hzhz**********","******");   
        Thread.sleep(1000);   
        cutAdsl("宽带");   
        Thread.sleep(1000);   
        //再连,分配一个新的IP   
        connAdsl("宽带","hzhz**********","******");   
    }   
}  

package com.sesame.network;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class ConnectNetWork {

    /**
     * 执行CMD命令,并返回String字符串
     */
    public static String executeCmd(String strCmd) throws Exception {
        Process p = Runtime.getRuntime().exec("cmd /c " + strCmd);
        StringBuilder sbCmd = new StringBuilder();
        BufferedReader br = new BufferedReader(new InputStreamReader(p
                .getInputStream()));
        String line;
        while ((line = br.readLine()) != null) {
            sbCmd.append(line + "\n");
        }
        return sbCmd.toString();
    }

    /**
     * 连接ADSL
     */
    public static boolean connAdsl(String adslTitle, String adslName, String adslPass) throws Exception {
        System.out.println("正在建立连接.");
        String adslCmd = "rasdial " + adslTitle + " " + adslName + " "
                + adslPass;
        String tempCmd = executeCmd(adslCmd);
        // 判断是否连接成功
        if (tempCmd.indexOf("已连接") > 0) {
            System.out.println("已成功建立连接.");
            return true;
        } else {
            System.err.println(tempCmd);
            System.err.println("建立连接失败");
            return false;
        }
    }

    /**
     * 断开ADSL
     */
    public static boolean cutAdsl(String adslTitle) throws Exception {
        String cutAdsl = "rasdial " + adslTitle + " /disconnect";
        String result = executeCmd(cutAdsl);
       
        if (result.indexOf("没有连接")!=-1){
            System.err.println(adslTitle + "连接不存在!");
            return false;
        } else {
            System.out.println("连接已断开");
            return true;
        }
    }
   
    public static void main(String[] args) throws Exception {
        connAdsl("宽带","hzhz**********","******");
        Thread.sleep(1000);
        cutAdsl("宽带");
        Thread.sleep(1000);
        //再连,分配一个新的IP
        connAdsl("宽带","hzhz**********","******");
    }
}  



执行结果:

Java代码
正在建立连接.  
已成功建立连接.  
连接已断开  
正在建立连接.  
已成功建立连接. 

如果你要重连功能的话,这样就可以了:

while(!connAdsl("宽带","hzhz**********","******")){
              Thread.sleep(1000);
}

你可能感兴趣的:(java,thread,c,windows,脚本)