本博主為高齡在校生,實驗室服務器需要假期時候無人守候也能實現自動登錄校園網從而實現網絡連接,以使實驗室同學在家也可以使用校園vpn連接服務器。
由於假期時候實驗室沒有人,而假期實驗室可能會出現斷電斷網問題(假期一般需要定期檢修校園的電力和網絡),雖然服務器設置了來電自啟動但是網絡連接卻是不好解決的問題,因此給出了下面的自己設計的解決方案。
首先,給出校園網網絡自連接的程序(自動登錄校園網):
參看: https://www.cnblogs.com/devilmaycry812839668/p/14201947.html
東北某海濱城市的某高校的某分校區的校園網登錄程序,(python3, 模擬瀏覽器的登入方式)
已經寫好的代碼:(python3)

#encoding:UTF-8 from urllib.parse import urlencode from urllib.request import urlopen import urllib import os import sys import getpass if __name__ == "__main__": #username=input("please input your username\nUsername: ") #print "please input your password" #password=getpass.getpass() username = "123456" password = "123456" opener=urllib.request.build_opener(urllib.request.HTTPRedirectHandler(), urllib.request.HTTPHandler(debuglevel=0)) opener.addheaders=[('User-agent', "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)")] url='http://172.20.20.1:801/srun_portal_pc.php?ac_id=3&' try: response=opener.open(url, urlencode({"ac_id":"3", "action":"login", "nas_ip":"", "password":password, "save_me":"1", "url":"", "user_ip":"", "user_mac":"", "username":username }).encode("utf-8")) xxx_print = response.read().decode("utf-8") if xxx_print.find(u'網絡已連接') != -1: print( '\n\n\n' ) print( '##################################' ) print( '您已經成功登錄' ) print( '##################################' ) print( '\n\n\n' ) else: print( '\n\n\n' ) print( '##################################' ) print( '抱歉,您登錄失敗' ) print( '##################################' ) print( '\n\n\n' ) print( xxx_print ) except Exception as e: print( '\n\n\n' ) print( '##################################' ) print( "網絡聯通有誤, 請檢測網絡是否可達" ) print( '##################################' ) print( e )
修改用戶名和密碼即可以用。
打印服務器網絡配置:
ifconfig

docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255 ether 02:42:ab:11:7b:44 txqueuelen 0 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 enp7s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.105.219.170 netmask 255.255.0.0 broadcast 10.105.255.255 inet6 fe80::2e4d:54ff:fe44:a14b prefixlen 64 scopeid 0x20<link> inet6 2001:da8:a800:af00:2e4d:54ff:fe44:a14b prefixlen 64 scopeid 0x0<global> ether 2c:4d:54:44:a1:4b txqueuelen 1000 (Ethernet) RX packets 117350 bytes 10936594 (10.9 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1222 bytes 122427 (122.4 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 device memory 0xc6400000-c647ffff enp8s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.11.66 netmask 255.255.255.0 broadcast 192.168.11.255 inet6 fe80::2e4d:54ff:fe44:a14c prefixlen 64 scopeid 0x20<link> inet6 2001:da8:a800:af00:2e4d:54ff:fe44:a14c prefixlen 64 scopeid 0x0<global> ether 2c:4d:54:44:a1:4c txqueuelen 1000 (Ethernet) RX packets 115510 bytes 10489267 (10.4 MB) RX errors 910 dropped 0 overruns 0 frame 736 TX packets 392 bytes 40113 (40.1 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 device memory 0xc6300000-c637ffff lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1000 (Local Loopback) RX packets 349 bytes 28888 (28.8 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 349 bytes 28888 (28.8 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
可以看到服務器有兩個網卡,由於是Ubuntu18.04server 系統,參看服務器的網絡配置:
vim /etc/netplan/50-cloud-init.yaml

# This file is generated from information provided by # the datasource. Changes to it will not persist across an instance. # To disable cloud-init's network configuration capabilities, write a file # /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following: # network: {config: disabled} network: ethernets: enp7s0: dhcp4: true nameservers: addresses: - 202.118.66.6 search: [] optional: true enp8s0: addresses: - 192.168.11.66/24 gateway4: 192.168.11.254 nameservers: addresses: - 202.118.66.6 search: [] optional: true version: 2
可以看到服務器是兩個網卡,一個是動態ip的,一個是靜態ip的,其中由於實驗室路由器損壞導致靜態ip無法聯網只能使用動態ip聯網,因此服務器開機時需要設置路由使服務器的網關為使用動態ip的網卡(不然系統會默認使用靜態ip網卡對應的網關作為默認網關,導致無法聯網),這個無法聯網的靜態ip對應的網卡主要用於連接內網打印機等設備。
因此,這里我們需要實現的功能就是服務器開機自動設置路由,然后自動啟動網絡登錄程序。
這里的解決方案是寫一個自啟動的腳本,把網絡路由設置和自啟動登錄腳本的設置寫入進去:
自啟動腳本編寫方式參考:
/etc/init.d 開機自動執行腳本
編輯自啟動腳本:
sudo vim /etc/init.d/keep_alive
#! /bin/sh ### BEGIN INIT INFO # Provides: sshd # Required-Start: $remote_fs $syslog # Required-Stop: $remote_fs $syslog # Default-Start: 2 3 4 5 # Default-Stop: # Short-Description: OpenBSD Secure Shell server ### END INIT INFO route del default gw 192.168.11.254 nohup python3 /home/xxxxxx/keep_alive.py 1>/tmp/network_login.log 2>&1 & exit 0
其中:
route del default gw 192.168.11.254 是刪除掉靜態ip的網關,這樣所有訪問都可以通過動態ip的網卡出去,而且不影響使用靜態ip網卡來訪問局域網的打印機。
nohup python3 /home/xxxxxx/keep_alive.py 1>/tmp/network_login.log 2>&1 & 該語句是設置網絡登錄腳本啟動后把輸出打印到日志中
編寫好 /etc/init.d/ 下面的 keep_alive 自啟動腳本后還需要給其執行權限並更新到系統中:
賦給執行權限:
sudo chmod a+x /etc/init.d/keep_alive
更新到系統中:
sudo update-rc.d keep_alive defaults 90
從系統中移除:
sudo update-rc.d -f keep_alive remove
配置結束。
=================================================
重啟電腦可以成功設定路由並自動登錄校園網。