【教程】路由器自动定时登录校园网

转载请注明出处:小锋学长生活大爆炸[xfxuezhang.cn]

前景提要

Python脚本

Shell脚本(推荐)

路由器挂机

前景提要

现在需要自动定时的去登录校园网,省的每次还得自己手动去登录,也太麻烦了。

Python脚本

适合在电脑上挂着

import requests import socket import urllib.parse import time import psutil import subprocess import json5 headers = { '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 True except: return False def 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 ip def 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).text print(res) break except: pass if __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/bash check() { url="http://www.baidu.com" if curl --output /dev/null --silent --head --fail "$url" -m 2; then return 0 else return 1 fi } 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; do echo ">> 当前尝试链接:$url" response=$(curl -s -k -X GET "${url}${data}") if [ $? -eq 0 ]; then echo ">> 请求成功:$response" break fi done } # 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

路由器挂机

比如,每30分钟执行一次:

*/30 * * * * sh /root/net.sh

原文链接:https://blog.csdn.net/sxf1061700625/article/details/131004838?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522171828104516800225591750%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=171828104516800225591750&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~times_rank-11-131004838-null-null.nonecase&utm_term=%E8%B7%AF%E7%94%B1%E5%99%A8%E3%80%81%E5%8D%83%E5%85%86

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享