Skip to content

Instantly share code, notes, and snippets.

@jinyu121 jinyu121/NetAuth.py
Last active Oct 23, 2019

Embed
What would you like to do?
复旦大学网络自动认证脚本

复旦大学网络自动认证脚本

本脚本分为Python版和sh版。其中sh版为官方版,Python版为非官方版。

sh版

配置学号、密码、ip后,使用bash NetAuth.sh 进行登录

Python版

  1. 配置学号、密码
  2. 登录: python3 NetAuth.py
  3. 登出: python3 NetAuth.py --action logout

自动登录

Linux下可以将脚本加入crontab以实现自动登录。

  1. sudo crontab -e -uroot
  2. 如使用Python版: */30 * * * * python3 /path/to/NetAuth.py
  3. 如使用sh版: */30 * * * * bash /path/to/NetAuth.sh

上述代码表示“每半小时自动登录一次”。如需更改此逻辑,请查阅《Cron - 维基百科》

免责声明

  1. 随着复旦大学信息办的调整,此脚本可能会失效。请及时关注复旦大学信息办的通知和公告。
  2. 请遵循GNU 通用公共许可协议第三版协议。
  3. 请在合法且合理的范围内使用此脚本。作者不承担因此脚本滥用导致的任何后果。

版本记录

20171225

  1. 去掉一个警告

20170927

  1. 修改一个with语句的bug
  2. 删除旧脚本
  3. 增加README.md

20170920

  1. Python版可以免配IP

20170915

  1. Hello World
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse
import requests
import logging
import socket
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
CONFIG_USER = {
"username": "YOUR_STUDENT_ID_HERE",
"password": "YOUR_PASSWORD_HERE"
}
AUTH_URL = "https://10.108.255.249/include/auth_action.php"
logging.basicConfig(
filename='login_record.log',
level=logging.ERROR,
format='[%(asctime)s] %(levelname)s: %(message)s'
)
config_login = {
"action": "login",
"ac_id": 1,
# "nas_ip": "",
# "user_mac": "",
"save_me": 1,
"ajax": 1
}
config_logout = {
"action": "logout",
"ajax": 1
}
def get_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.connect(('10.255.255.255', 1)) # Any IP is OK here
my_ip = s.getsockname()[0]
except:
my_ip = '127.0.0.1'
finally:
s.close()
return my_ip
def sign(action):
config = None
if "login" == action:
config = config_login
elif "logout" == action:
config = config_logout
config.update(CONFIG_USER)
config.update({"user_ip": get_ip()})
try:
result = requests.post(
AUTH_URL,
data=config,
timeout=5,
verify=False
)
logging.info(result.text.split(",")[0])
except Exception as e:
logging.error(str(e))
if '__main__' == __name__:
parser = argparse.ArgumentParser()
parser.add_argument('--action', type=str, default="login", choices=['login', 'logout'])
args = parser.parse_args()
sign(args.action)
#!/bin/bash
username="YOUR_STUDENT_ID_HERE"
password="YOUR_PASSWORD_HERE"
ip="YOUR_IP_HERE"
URL="https://10.108.255.249/include/auth_action.php"
curl $URL --insecure --data "action=login&username=$username&password=$password&ac_id=1&user_ip=$ip&nas_ip=&user_mac=&save_me=1&ajax=1" > /dev/null 2>&1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.