`

【转载】java实现动态切换上网IP (ADSL拨号上网

    博客分类:
  • java
 
阅读更多

转载:http://sesame.iteye.com/blog/434088

 

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

rasdial命令:

 

拨号

Java代码  收藏代码
  1. 语法: rasdial  连接名称 username password  
  2. 实例: rasdial 我的宽带 hzhz1234567890 dfdfdfdfdf  

断网

Java代码  收藏代码
  1. 语法:rasdial  连接名称 /disconnect    
  2. 实例: rasdial 宽带  /disconnect    

 


java程序调用rasdial命令:

Java代码  收藏代码
  1. package com.sesame.network;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.InputStreamReader;  
  5.   
  6. public class ConnectNetWork {  
  7.   
  8.     /** 
  9.      * 执行CMD命令,并返回String字符串 
  10.      */  
  11.     public static String executeCmd(String strCmd) throws Exception {  
  12.         Process p = Runtime.getRuntime().exec("cmd /c " + strCmd);  
  13.         StringBuilder sbCmd = new StringBuilder();  
  14.         BufferedReader br = new BufferedReader(new InputStreamReader(p  
  15.                 .getInputStream()));  
  16.         String line;  
  17.         while ((line = br.readLine()) != null) {  
  18.             sbCmd.append(line + "\n");  
  19.         }  
  20.         return sbCmd.toString();  
  21.     }  
  22.   
  23.     /** 
  24.      * 连接ADSL 
  25.      */  
  26.     public static boolean connAdsl(String adslTitle, String adslName, String adslPass) throws Exception {  
  27.         System.out.println("正在建立连接.");  
  28.         String adslCmd = "rasdial " + adslTitle + " " + adslName + " "  
  29.                 + adslPass;  
  30.         String tempCmd = executeCmd(adslCmd);  
  31.         // 判断是否连接成功  
  32.         if (tempCmd.indexOf("已连接") > 0) {  
  33.             System.out.println("已成功建立连接.");  
  34.             return true;  
  35.         } else {  
  36.             System.err.println(tempCmd);  
  37.             System.err.println("建立连接失败");  
  38.             return false;  
  39.         }  
  40.     }  
  41.   
  42.     /** 
  43.      * 断开ADSL 
  44.      */  
  45.     public static boolean cutAdsl(String adslTitle) throws Exception {  
  46.         String cutAdsl = "rasdial " + adslTitle + " /disconnect";  
  47.         String result = executeCmd(cutAdsl);  
  48.          
  49.         if (result.indexOf("没有连接")!=-1){  
  50.             System.err.println(adslTitle + "连接不存在!");  
  51.             return false;  
  52.         } else {  
  53.             System.out.println("连接已断开");  
  54.             return true;  
  55.         }  
  56.     }  
  57.      
  58.     public static void main(String[] args) throws Exception {  
  59.         connAdsl("宽带","hzhz**********","******");  
  60.         Thread.sleep(1000);  
  61.         cutAdsl("宽带");  
  62.         Thread.sleep(1000);  
  63.         //再连,分配一个新的IP  
  64.         connAdsl("宽带","hzhz**********","******");  
  65.     }  
  66. }  

 


执行结果:

Java代码  收藏代码
  1. 正在建立连接.  
  2. 已成功建立连接.  
  3. 连接已断开  
  4. 正在建立连接.  
  5. 已成功建立连接.  

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics