苏大内网服务器登陆网关脚本

Posted by tianhaoo on May 21, 2019 本文阅读量

ubutnu下自动检测网络连接并自动登录网关的shell脚本

实验室有台服务器在连接校园网,偶尔会出现问题需要重新登录网关,写了个shell脚本在后台运行,每隔十分钟ping一次百度,如果ping不通的话就自动登录网关

#!/bin/bash
#检测网络连接

#判断输出日志文件是否存在
log=./network.log
if [ ! -f ${log} ]
then
   touch ${log}
fi

# 每隔十分钟判断一次
while [ true ]; do
   echo "checking inernet connecting..."
   # 判断网络是否连接
   ping -c 1 www.baidu.com > /dev/null 2>&1
   if [ $? -eq 0 ];then
      echo `date` good >> ${log}
      echo "connecting is good"
   else
      echo "========================================" >> ${log}
      echo `date` bad >> ${log}
      echo "connecting is bad"
      # 尝试进行网络连接
      echo `date` trying to connect internet... >> ${log}
      echo "trying to connect internet..."
      echo `date` `curl -d "username=20175227015&domain=&password=xxxxxxx&enablemacauth=0" http://a.suda.edu.cn/index.php/index/login` >> ${log}
      echo "=======================================" >> ${log}
   fi
   seconds_left=600
   echo "Please wating for ${seconds_left} s..."
   while [ $seconds_left -gt 0 ];do
      echo -n $seconds_left
      sleep 1
      seconds_left=$(($seconds_left - 1))
      echo -ne "\r     \r" #清除本行文字
   done

done

更新

学校网关系统升级了,现在只需要在网关认证界面加入MAC免认证即可,但一个用户最多只能有两个MAC免认证,所以此方法仍有价值。

再次更新

#!/bin/bash
if [ $# -lt 1 ]; then
    echo "Usage: ./login_suda.sh <ip>"
    echo "Example: ./login_suda.sh 10.10.65.124"
    exit 1
fi
ip=$1
curl -i "http://10.9.1.3:801/eportal/?c=Portal&a=login&callback=dr1022&login_method=1&user_account=%2C0%2C20175227039&user_password=zj821220&wlan_user_ip=${ip}&wlan_user_ipv6=&wlan_user_mac=000000000000&wlan_ac_ip=&wlan_ac_name=&jsVersion=3.3.3&v=2849"
echo "\n"



2020年更新

2020年暑假学校升级了网关认证系统,上面的两个脚本已经失效。

下面是一个自动登录网关的python脚本,可以辅以windows定时任务,或者linux下的crontab

import requests
import socket
import time
import sys


uname = "1234"
passwd = "5678"


def get_host_ip():
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(('8.8.8.8', 80))
        ip = s.getsockname()[0]
    finally:
        s.close()
    return ip


def login_wg(uname, passwd):
    ip = get_host_ip()
    url = "http://10.9.1.3:801/eportal/?c=Portal&a=login&callback=dr1022&login_method=1&user_account=%2C0%2C" + uname + "&user_password=" + passwd + "&wlan_user_ip=" + ip +"&wlan_user_ipv6=&wlan_user_mac=000000000000&wlan_ac_ip=&wlan_ac_name=&jsVersion=3.3.3&v=2849"
    data = requests.get(url).text
    print(data)


login_wg(uname, passwd)

time.sleep(60)
sys.exit()