zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Ubuntu18.04server 双网卡,开机自动设置路由并启动校园网网络认证程序(Ubuntu开机自动设置路由,开机自启动应用程序)

2023-09-11 14:19:19 时间

本博主为高龄在校生,实验室服务器需要假期时候无人守候也能实现自动登录校园网从而实现网络连接,以使实验室同学在家也可以使用校园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 )
View Code

修改用户名和密码即可以用。

 

 

 

 

打印服务器网络配置:

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
View Code

 

 

可以看到服务器有两个网卡,由于是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
View Code

 

可以看到服务器是两个网卡,一个是动态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


 

配置结束。

 

 

PS:

有的时候还需要额外执行命令:systemctl  enable keep_alive   才可以实现自启动服务。

 

 

=================================================

 

 

 

重启电脑可以成功设定路由并自动登录校园网。