js实现自动登录Tp-Link886N路由器,查看设备网速

作者: Brave 分类: 学习 发布时间: 2020-06-27 19:08

网上教程一大堆,C#的实现,python的实现等都有,本篇文章主要是js的实现,基于Tp-Link886N V7版本。

首先讲一下大概原理,打开192.168.1.1,输入登陆密码,js把密码进行加密,发送给路由器,密码正确就会返回一个stok,否则返回401错误。登陆成功后,所有数据请求都是通过检查请求链接里面是否有stok值,请求接口一般都是:

http://192.168.1.1/stok=${stok}/ds

要获取不同的数据,只需要修改post里面data的值。

附上js部分的所有代码,页面显示部分是基于layui,没有用layui的人就把最外层的layui.use();方法删除,里面的table.render()也可以删除。

layui.use(['table','form','jquery'], function(){
  var table = layui.table,form = layui.form,$ = layui.jquery;

  var stok;
  var data;
  var arr = [];
  var up_Speed;
  var down_Speed;

  let settings = {
    "url": "http://192.168.1.1/stok=/ds",
    "method": "POST",
    "timeout": 2000,
    "headers": {
      "Content-Type": "text/plain"
    },
    "data": "{\"hosts_info\":{\"table\":\"online_host\"},\"method\":\"get\"}",
    "async": false,
    "statusCode":{
        401: function(){
            //未登陆,执行登陆方法得到stok
           login();
           getData();  //再次登陆。
        }
    }
  };
  
  

    //执行方法实例化
    getData();
    //两秒自动刷新网页
    refresh = () => location.reload();
    setTimeout(refresh,2000);

    //table渲染
    table.render({
        elem: '#demo'
        //,height: 312
        ,data:arr
        ,cols: [[ //表头
        {field: 'hostname', title: '设备名', width:180, fixed: 'left'}
        ,{field: 'ip', title: 'ip地址', width:180, sort: true}
        ,{field: 'mac', title: 'mac', width:180, sort: true}
        ,{field: 'up_speed', title: '上传速率KB/s', minWidth:180, sort: true} 
        ,{field: 'down_speed', title: '下载速率KB/s', minWidth: 180, sort: true}
        ]]
    });

    //设置input框数据
    $("input[name='up_speed']").val(up_Speed+'  KB/s');
    $("input[name='down_speed']").val(down_Speed+'  KB/s');

    
    //ajax获取实时数据,并解析成表格格式
    function getData(){
        //获取stok
        stok = sessionStorage.getItem('stok');
        settings.url = `http://192.168.1.1/stok=${stok}/ds`;
        //请求数据
        $.ajax(settings).done(function (response) {
          console.log(JSON.parse(response));
          data = JSON.parse(response);
        });
        if(arr.length == 0){
        //数据拼接成表格格式
        let host = data['hosts_info']['online_host'];
          for(let obj of host){
            for(key in obj){
              obj[key]['hostname'] = decodeURI(obj[key]['hostname']);
              obj[key]['up_speed'] = (obj[key]['up_speed'] / 1024).toFixed(2);
              obj[key]['down_speed'] = (obj[key]['down_speed'] / 1024).toFixed(2);
              arr.push(obj[key]);
              if(obj[key]['ip'] == '192.168.1.100'){
                  up_Speed = obj[key]['up_speed'];
                  down_Speed = obj[key]['down_speed'];
              }
            }
          }
        }
        console.log(arr);
    }

    //登陆方法,获取stok
    function login(){
      console.log('开始执行login方法');
        //加密函数
      orgAuthPwd = function(a) {
          return this.securityEncode(a, "RDpbLfCPsJZ7fiv", "yLwVl0zKqws7LgKPRQ84Mdt708T1qQ3Ha7xv3H7NyU84p21BriUWBU43odz3iP4rBL3cD02KZciXTysVXiV8ngg6vL48rPJyAUw0HurW20xqxv9aYb4M9wK1Ae0wlro510qXeU07kV57fQMc8L6aLgMLwygtc0F10a0Dg70TOoouyFhdysuRMO51yY5ZlOZZLEal1h0t9YQW0Ko7oBwmCAHoic4HYbUyVeU3sfQ1xtXcPcf1aT303wAQhv66qzW")
      };
      //a密码 c短 b长
      securityEncode = function(a, c, b) {
          var d = "",
          e, f, g, h, 
          k = 187,
          m = 187;
          f = a.length;
          g = c.length;
          h = b.length;
          e = f > g ? f : g;
          //e = g = c.length
          for (var l = 0; l < e; l++)
              m = k = 187, 
              l >= f ? m = c.charCodeAt(l) : l >= g ? k = a.charCodeAt(l) : (k = a.charCodeAt(l), m = c.charCodeAt(l)), d += b.charAt((k ^ m) % h);
          return d
      };
      var sPwd = orgAuthPwd('AABBCCDD'); //这里填你的路由器登陆密码
      console.log(sPwd); //输出加密后的密码字符串
      
      var login_data = {
      "url": "http://192.168.1.1/",
      "method": "POST",
      "timeout": 2000,
      "headers": {
          "Content-Type": ["application/json; charset=UTF-8", "text/plain"],
      },
      "crossDomain":true,
      "async": false,
      "data": "{\"method\":\"do\",\"login\":{\"password\":\""+ sPwd +"\"}}",
      };

          let login_set = {
        "url": "http://192.168.1.1/",
        "method": "POST",
        "timeout": 2000,
        "headers": {
          "Content-Type": "text/plain"
        },
        "data": "{\"method\":\"do\",\"login\":{\"password\":\"MysT57aR9TefbwK\"}}",
        "async": false,
        "statusCode":{
            401: function(){
                //未登陆,执行登陆方法
              console.log('登陆失败');
            }
        }
      };
      //请求数据,得到stok,写入sessionStorage
      let res;
      $.ajax(login_set).done(function (response) {
        res = JSON.parse(response);
        console.log(res.stok);
      });
      sessionStorage.setItem('stok',res.stok);
    }

});

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!

发表评论

电子邮件地址不会被公开。 必填项已用*标注