注意:这篇文章上次更新于1689天前,文章内容可能已经过时。
This article was last updated1689 days ago, the content may be outdated.
需求分析
emm… 怎么说呢,日常使用电脑你肯定不需要自动登录校园网,但是在某些特殊情况下,如果你的主机与互联网断开了连接,而你又无法在现场操作主机登录校园网,那通常意味着生产力的下降,科研进度的延误等等后果…
前两周由于疫情原因学校关闭了实验室,志成哥在寝室用笔记本登录了一下校园网,就挤掉了实验室的服务器网络,导致无法使用向日葵进行远程连接。只能选择挨门卫大爷一顿骂然后去实验室重新登录校园网。如果当时志成哥的服务器上运行了自动登录校园网的定时任务,就可以免于这一顿挨骂了😂。
试想一下,如果寒假在家期间想要使用实验室的主机进行工作,但是却因为学校的网络中心抽风进行升级维护,导致主机与互联网断开连接,那可就不是挨一顿骂就能解决的了。
寒假即将到来,为了避免上述尴尬局面的发生,故提前完成这一需求。
Requirements Analysis
Emm… how to put it? For everyday computer use, you certainly don’t need auto-login to the campus network. But in certain special situations, if your host loses its connection to the Internet and you can’t be physically present to log the host in to the campus network, it usually means a drop in productivity, delays in research progress, and other such consequences…
Two weeks ago, the school closed the lab because of the pandemic. Brother Zhicheng logged in to the campus network with his laptop in the dorm, which kicked the lab server off the network and made it impossible to remote-connect via Sunflower (向日葵). The only option was to take a scolding from the security guard and go to the lab to log the campus network back in. If Brother Zhicheng’s server had been running a scheduled auto-login task at the time, he would have been spared that scolding😂.
Imagine that during the winter break you want to use the lab’s host to work from home, but the school’s network center has a glitch during an upgrade or maintenance, cutting the host off from the Internet. That would not be solved by just getting scolded.
Winter break is coming. To avoid the embarrassing situation above, I’m completing this requirement in advance.
思路
网上查了一下校园网的自动登录脚本,大多是通过 requests 包模拟网络请求实现的。鉴于本人学习能力比较差,看了好多博客也不知道到底该怎么实现,最主要的原因是不明白网络通信的具体过程,每个参数都是干什么用的,比如说 cookie 等等,所以也写不出来相应的代码。
但是,正所谓条条大路通罗马,实现一个自动登录的需求,又何必局限于 requests 包呢,本文使用 RPA-Python 来实现这一功能,使用这个包不需要了解网络通信过程,只需要你拥有基本的英语阅读能力或者翻译软件的使用能力,了解相关接口的使用方法即可。
这个包的功能大概可以描述为使用代码控制你的电脑。我们需要使用这个包完成 打开浏览器 ==> 输入登录地址 ==> 输入学号,密码 ==> 点击回车 这一系列操作。
最后,在 Windows 平台上,将我们写好的代码打包成 exe 可执行文件,创建定时任务,让该程序每天都自动执行一遍;在 Linux 平台上,不需要打包过程,直接创建定时任务执行 python 脚本即可。
Approach
When I searched online for campus network auto-login scripts, most of them were implemented by simulating network requests with the requests package. Given that my learning ability is rather poor, I couldn’t figure out how to implement it even after reading many blog posts. The main reason was that I didn’t understand the specific process of network communication — what each parameter, such as cookie, is for — so I couldn’t write the corresponding code either.
However, as the saying goes, all roads lead to Rome. Why limit yourself to the requests package for an auto-login requirement? This article uses RPA-Python to implement this feature. With this package, you don’t need to understand the network communication process; you only need basic English reading abilityor the ability to use translation software and an understanding of the relevant interface usage.
The package’s function can be roughly described as using code to control your computer. We need to use this package to complete a series of operations: open the browser ==> enter the login address ==> enter student ID and password ==> press Enter.
Finally, on Windows, we package the code into an exe executable, create a scheduled task, and have the program run automatically every day; on Linux, no packaging is needed — just create a scheduled task to run the Python script.
实现
这里记录一下在 Windows 平台上 RPA-Python 的使用示例。
脚本编写
首先需要获取校园网的登录地址,我们在没有登录校园网的情况下请求一下百度的地址。
可以看到我们获得的请求结果是一段 js 脚本,这个脚本给我们重定向到了一个新的地址,这个新地址指向了我们平时使用的校园网的登录认证界面。
Implementation
Here I’ll record an example of using RPA-Python on the Windows platform.
Writing the Script
First, we need to obtain the campus network login address. Without logging in to the campus network, we make a request to Baidu’s address.
As you can see, the request result we get is a piece of JS code that redirects us to a new address, which points to the campus network login authentication page we usually use.
1 | import requests |

上面的网址就是我们登录认证的地址了,嗯… ,我也不知道这个地址里包含了什么信息,为了防止它是动态的,还是不要在脚本里把这个 url 写死了。可以通过简单的字符串分割来获得 url。

The URL above is our login authentication address. Well… I don’t know what information is contained in this address. In case it’s dynamic, it’s better not to hardcode this url in the script. We can obtain the url through simple string splitting.
1 | def auto_login(): |
接下来我们就可以使用 RPA-Python 来进行自动操作了。具体的使用方法可以参考官方仓库。
其中需要注意的是,第一次使用该模块的 init 函数时会从 GitHub 进行下载,需要你保持一个通畅的网络连接。
先给出示例代码,再进行简单解释。
Next, we can use RPA-Python to perform the automated operations. For specific usage, you can refer to the official repository.
One thing to note: the first time you use the module’s init function, it downloads from GitHub, so you need to maintain a smooth network connection.
Let me first provide the example code, then give a brief explanation.
1 | import requests |
执行上面的代码,就会自动完成 打开浏览器 ==> 输入登录地址 ==> 输入学号,密码 ==> 点击回车 这一些列操作,效果如下图。

下面说明几个我踩过的坑以及注意事项。
-
关于 r.init() 函数。这个函数设置 chrome_browser = True 浏览器为 True 的模式会调用 Chrome 浏览器,因此,这个脚本的运行是依赖于 Chrome 的。第二个参数 headless_mode = False,表明不使用 Chrome 浏览器的 headless 模式。那么什么是 headless 模式呢?建议自行百度。我的理解是,headless 模式决定了是否进行界面的渲染,要知道通信的过程主要是编码后的信息进行交换,是否进行界面渲染,是不影响通信的顺利完成的,因此,在实际使用过程中建议使用 headless 模式,也就是说:将 init 函数的 headless_mode 参数指定为 True,虽然不会显式调用 Chrome进行界面渲染,后面的工作还是可以顺利完成的。至于为什么建议设置 headless_mode = True ,可以参考:https://github.com/tebelorg/RPA-Python/issues/309,我在第二次运行脚本时似乎也遇到了这个问题。
-
type 函数的第一个参数时元素选择器,具体使用方法建议参考官方仓库。本案例中使用两个 input 标签的 name 和 type 属性来进行定位,你也可以使用其他属性。


Running the code above will automatically complete the series of operations — open the browser ==> enter the login address ==> enter student ID and password ==> press Enter — and the result is as shown below.

Below I’ll explain a few pitfalls I fell into and some things to note.
-
About the r.init() function. Setting chrome_browser = True in this function invokes the Chrome browser, so running this script depends on Chrome. The second parameter, headless_mode = False, means the Chrome browser’s headless mode is not used. So what is headless mode? I suggest you Baidu it yourself. My understanding is that headless mode determines whether the interface is rendered. You should know that communication mainly exchanges encoded information; whether the interface is rendered does not affect the successful completion of communication. Therefore, in actual use, it is recommended to use headless mode, that is: set the init function’s headless_mode parameter to True. Although Chrome won’t be explicitly invoked for interface rendering, the rest of the work can still be completed smoothly. As for why setting headless_mode = True is recommended, you can refer to: https://github.com/tebelorg/RPA-Python/issues/309; I seem to have encountered this problem when running the script the second time as well.
-
The first parameter of the type function is the element selector; for specific usage, I suggest referring to the official repository. In this case, I use the name and type attributes of two input tags for locating, and you can also use other attributes.


Windows Scheduled Task
First, we use pyinstaller to package the Python script into an executable file.
Run in the terminal:
1 | pyinstaller -F main.py |
编译完成后,可以在项目目录下找到 main.exe 文件。


运行这个可执行文件,会在该目录下生成一个 rpa_python.log 文件,打开这个文件,如果是如下内容,代表执行成功。

在正确执行的前提下可以设置 Windows 定时任务了。
首先,右键点击此电脑,点击管理,选择任务计划程序,点击创建任务。

填写名称。

点击触发器,点击新建,下图只是一个示例,具体情况根据自己的需要进行修改。

点击操作选项卡,点击新建,填写刚刚生成的可执行文件的位置,起始于填写这个文件的所在目录。

条件和设置选项卡可以根据需要进行调整。最后点击确定,要求我们输入密码。

可以在任务计划程序库中看到详细信息。

可执行文件 main.exe 目录下的 rpa_python.log 的修改日期与定时任务时间一致,代表执行成功。

保险起见,各位也可以断网进行测试,看能否自动联网。
上述操作中的触发器我设置的一次,用来验证能否正确执行,如果可以正确执行,就可以把该项设置为 每天 了。
Ubuntu 上可以使用 crontab 来定时执行 Python 脚本文件。我没有 Ubuntu 系统,就不做演示了。
最后,RPA-Python 这个项目简单易用,应该还可以玩很多骚操作,欢迎有想法的友友们留言。🧐
After compilation, you can find the main.exe file in the project directory.


Running this executable will generate an rpa_python.log file in that directory. Open this file; if it contains the following content, the execution was successful.

With correct execution, we can now set up the Windows scheduled task.
First, right-click “This PC”, click Manage, select Task Scheduler, and click Create Task.

Fill in the name.

Click the Triggers tab, click New. The image below is just an example; modify it according to your own needs.

Click the Actions tab, click New, fill in the location of the executable file just generated, and fill in the directory where this file resides for “Start in”.

The Conditions and Settings tabs can be adjusted as needed. Finally click OK, and you’ll be asked to enter your password.

You can see the detailed information in the Task Scheduler Library.

If the modification date of rpa_python.log in the main.exe directory matches the scheduled task time, the execution was successful.

To be safe, you can also test with the network disconnected to see if it can go online automatically.
In the operations above, I set the trigger to run once to verify correct execution; if it runs correctly, you can set the item to “Daily”.
On Ubuntu, you can use crontab to schedule the Python script. I don’t have an Ubuntu system, so I won’t demonstrate it.
Finally, the RPA-Python project is simple and easy to use, and should support many more fancy tricks. Friends with ideas are welcome to leave comments. 🧐


