转载请注明出处:小锋学长生活大爆炸[xfxuezhang.cn]
目录
前景提要
Python脚本
Shell脚本(推荐)
路由器挂机
前景提要
- 小米路由R4A千兆版安装breed+OpenWRT教程以及救砖(全脚本无需硬改)
- 【教程】保姆级红米AX6000刷UBoot和OpenWrt固件
- 详解OpenWrt路由器设置Crontab定时检查网络并重启
现在需要自动定时的去登录校园网,省的每次还得自己手动去登录,也太麻烦了。
Python脚本
适合在电脑上挂着
import requests
import socket
import urllib.parse
import time
import psutil
import subprocess
import json5headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.57'
}def check():url = 'http://www.baidu.com'try:requests.get(url, timeout=2)return Trueexcept:return Falsedef get_ip():s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)s.connect(('10.255.255.255', 1))ip = s.getsockname()[0]print(ip)return ipdef get_ip2():url = r'http://192.168.53.229/drcom/chkstatus?callback='resp = requests.get(url=url, headers=headers).text.strip()[1:-1]return json5.loads(resp)['v46ip']def list_interfaces():# 获取当前计算机上所有的网卡信息interfaces = psutil.net_if_addrs()# 遍历网卡信息,输出网卡名称for interface in interfaces:print(interface)def set_dns(interface_name="WLAN", dns_server='223.5.5.5'):'''interface_name是网卡的名称,如"Ethernet/WLAN";dns_servers是DNS服务器地址,多个地址可以用逗号隔开。'''# 调用netsh命令修改DNS服务器output = subprocess.run(["netsh", "interface", "ipv4", "set", "dnsservers", interface_name, "static", dns_server], check=True, capture_output=True)output_str = output.stdout.decode('gbk')print(output_str)# 输出修改后的DNS服务器地址output = subprocess.run(["netsh", "interface", "ipv4", "show", "dnsservers", interface_name], capture_output=True)output_str = output.stdout.decode('gbk')print(output_str)def connect():urls = list(['https://192.168.53.229:801/eportal/?', 'https://s2.scut.edu.cn:801/eportal/?'])data = {'c':'Portal','a':'login','callback':'dr1003','login_method':'1','user_account':',0,你的网络账号(也就是学号)主要前面的[0,]不要删','user_password':'你的网络密码','wlan_user_ip': get_ip2(),'wlan_user_ipv6':'','wlan_user_mac':'000000000000','wlan_ac_ip':'','wlan_ac_name':'','jsVersion':'3.3.2','v':'10315'}for url in urls:try:res = requests.get(url + urllib.parse.urlencode(data), headers=headers, verify=False).textprint(res)breakexcept:passif __name__ == '__main__':# set_dns(dns_server='202.38.193.33')# set_dns(dns_server='223.5.5.5')connect()# while True:# connect()# # connect() if check() else None# time.sleep(60*30)
Shell脚本(推荐)
适合在路由器上挂着,由上面的python脚本转换而来
#!/bin/bashcheck() {url="http://www.baidu.com"if curl --output /dev/null --silent --head --fail "$url" -m 2; thenreturn 0elsereturn 1fi
}get_ip() {ip=$(ip route get 8.8.8.8 | awk '{print $NF; exit}')echo "$ip"
}get_ip2() {url="http://192.168.53.229/drcom/chkstatus?callback="resp=$(curl -s -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.57" "$url")#echo $resp#echo "$resp" | grep -Eo '"v4ip":"\K[^"]+'ip=$(echo "$resp" | awk -F'"' '/"v4ip":/{for(i=1;i<=NF;i++){if($i=="v4ip"){print $(i+2); exit}}}')echo $ip
}list_interfaces() {interfaces=$(ip link show | awk -F': ' '{print $2}')echo "$interfaces"
}set_dns() {interface_name="$1"dns_server="$2"# 调用nmcli命令修改DNS服务器nmcli con mod "$interface_name" ipv4.dns "$dns_server"# 输出修改后的DNS服务器地址nmcli con show "$interface_name" | grep ipv4.dns
}connect() {ip=$(get_ip2)echo ">> 当前分配的IP:$ip"data="c=Portal&a=login&callback=dr1003&login_method=1&user_account=%2C0%2C你的网络账号(也就是学号)主要前面的[%2C0%2C]不要删&user_password=你的网络密码&wlan_user_ip=$ip&wlan_user_ipv6=&wlan_user_mac=000000000000&wlan_ac_ip=&wlan_ac_name=&jsVersion=3.3.2&v=10315"urls="https://192.168.53.229:801/eportal/? https://s2.scut.edu.cn:801/eportal/?"for url in $urls; doecho ">> 当前尝试链接:$url"response=$(curl -s -k -X GET "${url}${data}")if [ $? -eq 0 ]; thenecho ">> 请求成功:$response"breakfidone
}# set_dns "WLAN" "202.38.193.33"
# set_dns "WLAN" "223.5.5.5"
connect
# while true; do
# connect
# # connect if check else None
# sleep 1800
# done
路由器挂机
将shell代码保存为net.sh,然后参考这个博客,设置crontab定时执行即可。
比如,每30分钟执行一次:
*/30 * * * * sh /root/net.sh