java写宽带连接.adsl拨号,并且检测断线自动重新连接,更换ip

package com.hua;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Scanner;

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(), "GB2312"));
		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 boolean isConnect(){
	        boolean connect = false;
	        Runtime runtime = Runtime.getRuntime();
	        Process process;
	        try {
	            process = runtime.exec("ping " + "www.baidu.com");
	            InputStream is = process.getInputStream(); 
	            InputStreamReader isr = new InputStreamReader(is); 
	            BufferedReader br = new BufferedReader(isr); 
	            String line = null; 
	            StringBuffer sb = new StringBuffer(); 
	            while ((line = br.readLine()) != null) { 
	                sb.append(line); 
	            } 
	            System.out.println("返回值为:"+sb);  
	            is.close(); 
	            isr.close(); 
	            br.close(); 
	 
	            if (null != sb && !sb.toString().equals("")) { 
	                String logString = ""; 
	                if (sb.toString().indexOf("TTL") > 0) { 
	                    // 网络畅通  
	                    connect = true;
	                } else { 
	                    // 网络不畅通  
	                    connect = false;
	                } 
	            } 
	        } catch (IOException e) {
	            e.printStackTrace();
	        } 
	        return connect;
	    }

	 //测试代码
	public static void main(String[] args) throws InterruptedException,
	  Exception { Scanner sc = new Scanner(System.in);
	  System.out.println("宽带连接名称:"); //看你宽带连接的名称
	  String name = sc.next();
	  System.out.println("宽带账户:"); 
	  String username = sc.next();
	  System.out.println("宽带密码:"); 
	  String password = sc.next(); 
	  String adsl= "宽带连接";
		  while(true){   
			  boolean connect = isConnect();
	          Thread.sleep(100000);//单位毫秒,我设置的是100秒.自己看情况更改
	          if(!connect){
	        	  System.out.println("无网络,正在重新拨号");
	              connAdsl(name,username,password);
	          }
		  }
	  
	}
}
 /**
   * 自动更换ip
   * */   
 public void changeIp() throws Exception{
    Scanner sc = new Scanner(System.in);
    System.out.println("宽带连接名称:"); // 看你宽带连接的名称
    String name = sc.next();
    System.out.println("宽带账户:");
    String username = sc.next();
    System.out.println("宽带密码:");
    String password = sc.next();
    System.out.println("更换时间(单位毫秒1秒等于1000毫秒):");
    int wait = sc.nextInt();
    while(true){
      connAdsl(name, username, password);
      Thread.sleep(wait);
      cutAdsl(name);
      Thread.sleep(wait);
      //再连,分配一个新的IP
      connAdsl(name, username, password);
    }
    }

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,这是一个比较复杂的任务,需要用到不少Python库和一些系统命令,我会尽量详细地解释。首先,你需要安装以下库: - requests:用于发送HTTP请求和处理响应。 - openpyxl:用于读取Excel文件。 - subprocess:用于执行系统命令。 - time:用于等待一定时间。 接下来,我们来一步一步实现你的要求。 ## 切换网卡的MAC地址 在Windows系统下,可以使用以下命令来修改网卡的MAC地址: ``` netsh interface set interface "网卡名称" newMAC地址 ``` 其中,“网卡名称”是你要修改的网卡的名称,可以在“控制面板”-“网络和共享中心”-“更改适配器设置”中找到;“newMAC地址”是你要设置的新MAC地址。你可以在Python中使用subprocess库来执行这个命令,例如: ```python import subprocess # 修改网卡名称为“本地连接”的MAC地址为“00-11-22-33-44-55” subprocess.call('netsh interface set interface "本地连接" 00-11-22-33-44-55', shell=True) ``` ## 从Excel导入宽带账号和密码 假设你的Excel文件名为“accounts.xlsx”,账号和密码分别在第1列和第2列,第1行为表头,那么你可以这样读取数据: ```python from openpyxl import load_workbook # 加载Excel文件 workbook = load_workbook('accounts.xlsx') # 选择第一个工作表 sheet = workbook.active # 读取所有行的账号和密码 accounts = [] for row in sheet.iter_rows(min_row=2, values_only=True): accounts.append((row[0], row[1])) ``` 这里使用了openpyxl库来读取Excel文件,可以通过`load_workbook`函数加载文件,然后使用`active`属性选择第一个工作表。`iter_rows`方法可以按行迭代,`min_row`参数表示从第几行开始读取,`values_only`参数表示只读取单元格的值而不读取格式等信息。 ## 进行自动拨号 可以使用requests库发送POST请求来模拟拨号操作。具体操作步骤可以通过浏览器的开发者工具来分析,这里以“电信宽带拨号助手”为例: ```python import requests # 模拟浏览器登录 s = requests.Session() s.get('http://web.home.189.cn/autoLogin.do') s.get('http://www.189.cn/dqmh/userCenter/userinfo.do') # 获取拨号页面的URL response = s.get('http://www.189.cn/dqmh/ssoLink.do?method=linkTo&platNo=10024&toStUrl=http://www.189.cn/dqmh/ssoLogin.do?method=linkTo&platNo=10024&toStUrl=http://www.189.cn/webportal-wt/nportal/login') url = response.url # 提取拨号页面的参数 params = {} for i in url.split('?')[1].split('&'): k, v = i.split('=') params[k] = v # 构造POST请求的数据 data = { 'mobilenum': account[0], 'account': account[0], 'password': account[1], 'validatecode': '', 'perpetual': 'on', 'submitbt': '立即登录', 'backurl': '', 'backurl2': '', 'loginType': '12', 'state': 'undefined', 'wxUrl': '' } # 发送POST请求 response = s.post('http://www.189.cn/dqmh/ssoLogin.do', data=data, params=params) ``` 这里使用Session来模拟登录,首先访问一个地址来获取cookies,然后访问用户中心页面来获取更多cookies和参数。接着,访问拨号页面,提取页面URL中的参数,并构造POST请求的数据,最后发送POST请求即可。 ## 跑300M流量 可以使用time库来等待一定时间,例如: ```python import time # 等待10分钟 time.sleep(10 * 60) ``` 这里等待10分钟,单位是秒。你可以根据需要调整等待时间。 至此,我们完成了你的所有要求。不过,我还需要提醒你,这个程序只是一个简单的示例,实际使用时可能会遇到很多问题,例如登录需要验证码、拨号失败等等。你需要根据具体情况来进行修改和调试。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值