校园网无线AP自动开网口脚本

0.background

最初是可以手动打开每个宿舍无线接入点的有线网口的

参考校园网无线接入点修改教程

然鹅无线接入点现在每隔一段时间就会编译新固件关闭网口

具体脚本代码可以跳转GitHub

说明:

  • 需要按照之前教程:连接调试线后断电重启无线接入点才能让系统建立识别
  • 所以如果电脑关机是需要重新断电了(如果你是计算机专业没有关电脑习惯那么挺合适
  • 后期会寻找ARM等低功耗设备进行24小时连接检测(正在研究用树莓派、香橙派等Linux的串口操作

1.硬件准备

Usb-rj45调试线(上一个教程有说明

windows 10电脑

2.运行环境

Windows 10(其实win就可

Python 3+
Python安装好相应的包:import serial

3.具体脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import serial
import time

# connect serial
ser = serial.Serial(port='COM3')#COM3指win设备管理器中你插上调试线的名字,按照具体名字修改
time.sleep(2)
ser.read_all()

while True:
try:
# initial
ser.write('\n'.encode('utf8'))
time.sleep(1)
ser.read_all()

# list interfaces status, find ADM ports
ser.read_all()
ser.write('dis int brief\n'.encode('utf8'))
time.sleep(1)
data = str(ser.read_all(), encoding='utf8')
print(data)
data = data.split('\n')

ports = []
for item in data:
if item.find(r'ADM') != -1:
port = item.split(r' ')[0]
if port.find(r'GE') != -1:
ports.append(port)

# handle ADM ports
if len(ports) != 0:
ser.read_all()
ser.write('sys\n'.encode('utf8'))
time.sleep(1)
print(str(ser.read_all(), encoding='utf8'))

for port in ports:
ser.read_all()
ser.write(('int ' + port + '\nundo shutdown\n').encode('utf8'))
ser.write('quit\n'.encode('utf8'))
time.sleep(1)
print(str(ser.read_all(), encoding='utf8'))

ser.write('quit\n'.encode('utf8'))
time.sleep(1)
print(str(ser.read_all(), encoding='utf8'))

# find ports not in vlan 2002
ports = []
ser.read_all()
ser.write('dis vlan 2002\n'.encode('utf8'))
time.sleep(1)
data = str(ser.read_all(), encoding='utf8')
print(data)
data = data.split(r'Untagged ports:')[-1]
for port_num in [2, 3, 4, 5]:
if data.find(r'GigabitEthernet1/0/' + str(port_num)) == -1:
ports.append(str(port_num))

# handle vlan 2002 if necessary
if len(ports) != 0:
ser.read_all()
ser.write('sys\n'.encode('utf8'))
time.sleep(1)
print(str(ser.read_all(), encoding='utf8'))

ser.write('vlan 2002\n'.encode('utf8'))
time.sleep(1)
print(str(ser.read_all(), encoding='utf8'))
for port in ports:
ser.read_all()
ser.write(('port GE1/0/' + port + '\n').encode('utf8'))
ser.write('quit\n'.encode('utf8'))
time.sleep(1)
print(str(ser.read_all(), encoding='utf8'))

ser.write('quit\n'.encode('utf8'))
time.sleep(1)
print(str(ser.read_all(), encoding='utf8'))

except:
pass
print('')
print(time.asctime( time.localtime(time.time()) ))
print('')
time.sleep(600)

4.Windows守护脚本

1
2
3
4
@echo off
:home
python router.py
goto home