java 重启路由器

package com;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;
import com.Http;
 
public class Rotes {
    static final String kuser = "admin"; // 用户名
    static final String kpass = "admin"; // 密码
 
    static class MyAuthenticator extends Authenticator {
        @Override
        public PasswordAuthentication getPasswordAuthentication() {
            return (new PasswordAuthentication(kuser, kpass.toCharArray()));
        }
    }
     
    /**
     * 重启路由后停顿 40 秒等待新链接
     * @throws Exception
     */
    public Rotes() throws Exception{
        boolean v = checkLogin();
        if(v){
            restart();
        }
    }
     
     
    /**
     * 重启路由
     * @return
     * @throws IOException
     * @throws InterruptedException
     */
    private Boolean restart() throws IOException, InterruptedException{
        Http h=new Http("http://192.168.1.1/userRpm/SysRebootRpm.htm?Reboot=%D6%D8%C6%F4%C2%B7%D3%C9%C6%F7");
        String code = h.get();
        if(code.indexOf("正在重新启动")!=-1){
            System.out.println("正在重启路由,请稍等...");
            return true;
        }
        else{
            System.out.println("重启失败!");
            return false;
        }
    }
     
    /**
     * 登陆路由
     * @return
     * @throws Exception
     */
    private Boolean checkLogin() throws Exception{
        Authenticator.setDefault(new MyAuthenticator());
        URL url = new URL("http://192.168.1.1/");
        InputStream ins = url.openConnection().getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(ins,"gbk"));
        String str;
        String html="";
        while ((str = reader.readLine()) != null)
            html+=str;
         
        if(html.indexOf("多功能宽带路由器")!=-1){
            System.out.println("登陆成功!");
            return true;
        }else{
            System.out.println("登陆失败!");
            return false;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值