Internet access in schools requires identity authentication. This identity authentication mechanism is troublesome for people who often use the Internet. In order to solve this problem, several feasible methods without manual authentication are proposed here to solve the pain point of identity authentication.
-
Personal use host
For personal computers, the simplest is to use Google browser and other browsers that have the ability to remember the account and password. Of course, you can’t avoid manual clicking. For personal notebooks, I suggest that you can buy a newwifi router to manually brush the old hairy padavan firmware, or go to the salted fish to buy the brushed old hairy router. The price is about forty or fifty yuan, which is easy, affordable and easy. The network authentication can be automatically performed through the old maozi router, and the computers connected to the WiFi do not need to be authenticated.
-
Server Internet access
Personal computers can access the internet no matter what, just because it is easy or not. This article focuses on the Internet access of servers in the computer room. When we connect the servers in the LAN through SSH, we will encounter the situation that the servers cannot be connected to the network (note that the SSH connection host is in the same LAN, and the server cannot be connected to the network means that it cannot communicate with the external network, such as Baidu, Google and other websites). Although the network cable is plugged in, the annoying authentication mechanism makes us need to authenticate. We can’t authenticate only through the command line. Unless we use special means to call the visual browser interface, we can only go to the computer room for manual authentication, and then go back to the laboratory to use the server.
-
Writing scripts to access the Internet
For each authentication, access the 192.168.6.1 interface through the browser, fill in the online account password, and click the submit button to access the Internet. So the main idea is to use code to implement this process, eliminating all manual operations. The environment of this article is to write Python code under the Ubuntu system. The browser is Firefox. In fact, there are only some differences in details between windows and Ubuntu systems.
There are two ways to simulate the submission process. The first one is to grab the data packets, analyze the data packets, and then simulate the request to implement the process. However, there are many parameters in the process, so it takes a little effort to guess the meaning of a large number of submitted parameters.
The second is to use the selenium module of Python. We use this module to control the behavior of the browser. After filling in the form, click the submit button to simulate the whole process.
-
Scripting environment
Python parser requires that after version 3.0, thepip install selenium==4.0.0
Installing modules
Then you only need the Firefox browser and the webdriver driver corresponding to the browser version. The code here uses the Firefox browser. Similarly, Google browser can be used, but the code needs to be modified. The driver needs to correspond to the browser version, otherwise there may be various errors.
Webdriver download address of chrome
Webdriver download address of Firefox
Move the downloaded file to /user/bin under Linux and grant execution permission
mv chromedriver /usr/bin/
chmod +x /usr/bin/chromedriver
-
Code writing
We separate the account password from the code and create a user Txt file, which stores your account and password,
separate.
admin,password
The account and password can be easily modified by putting them in a separate file, and then use Python code to read the account and password
with open('./user.txt') as f:
line = f.readline()
if line:
mess = re.split(',|,', line)
account, password = mess[0], mess[1]
After reading the account and password, we need to use selenium to drive our browser to open 192.168.6.1. Here are a bunch of basic settings. One thing to note is that when setting the log path, different versions of selenium will have different writing methods, and it is recommended to write each path as an absolute path.
options = webdriver.FirefoxOptions()
options.add_argument("--no-sandbox")
options. add_ Argument ("--headless") \
options.add_argument("--disable-gpu")
tmp_service = Service(log_path="/home/geckodriver.log", executable_path='geckodriver')
driver = webdriver.Firefox(options=options, service=tmp_service)
driver.get('http://192.168.6.1')
After opening the interface, we use XPath to locate the element, find the user name and password element, fill in the account password, and then locate the submit button to simulate clicking. Throw an exception to an exception using try
try:
driver.find_element(By.XPATH, '//form[@name="f1"]/input[@name="DDDDD"]').send_keys(mess[0])
driver.find_element(By.XPATH, '//form[@name="f1"]/input[@name="upass"]').send_keys(mess[1])
driver.find_element(By.XPATH, '//form[@name="f1"]/input[@type="submit"]').click()
time.sleep(0.2)
Print ('you have successfully logged in!!! -- East Lake White Goose ')
except Exception as e:
print(e)
Print ('the device is online!!! -- East Lake White Goose ')
driver.quit()
Here is the complete code:
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.common.action_chains import ActionChains
import re
from selenium.webdriver.firefox.service import Service
With open ('./user.txt') as f: \
line = f.readline()
if line:
mess = re.split(',|,', line)
options = webdriver.FirefoxOptions()
options.add_argument("--no-sandbox")
options.add_argument("--headless")
options.add_argument("--disable-gpu")
tmp_ Service = Service (log\u path= "/home/geckodriver.log", executable\u path='geckodriver') \
driver = webdriver.Firefox(options=options, service=tmp_service)
driver.get('http://192.168.6.1')
try:
driver.find_element(By.XPATH, '//form[@name="f1"]/input[@name="DDDDD"]').send_keys(mess[0])
driver.find_element(By.XPATH, '//form[@name="f1"]/input[@name="upass"]').send_keys(mess[1])
driver.find_element(By.XPATH, '//form[@name="f1"]/input[@type="submit"]').click()
time.sleep(0.2)
Print ('you have successfully logged in!!! -- East Lake White Goose ')
except Exception as e:
print(e)
Print ('the device is online!!! -- East Lake White Goose ')
driver.quit()
else:
Print ('Please enter the account and password in user.txt ')
-
Set execution command
At this point, use the commandpython login.py
If you don’t report an error, you’ll be more than half successful
However, there are usually many Python parsers, and this command can only be used in login Py directory, so we need to get the absolute path of the python parser and login The absolute path of. Py, which is used to execute commands.
When the command can be executed successfully, we will write the command using the absolute path to Zshrc or In the bashrc file, it depends on which shell you use and which shell you use to write commands to the corresponding file
Alias'zjut'= 'your command'
Then usesource ~/.zshrc
Update your command and try the effect at last.
Here, you need to have absolute paths in your code and set the log path. In this way, you can access the Internet gracefully. Interested students can write an exit and login script, so that they can easily log in or log out through the command line.
-
Automatic execution
Maybe the command line is still very troublesome for many students. Here is the idea of executing scripts regularly every day to achieve a permanent online effect. Here, we need to use the Linux cron to implement it. The cron can set scheduled tasks, which is provided by Ubuntu.
Using commandscrontab -e
Edit automatically executed commands and the time of each automatic execution
30 8 * * * /you_path/python /you_path/login.py
The syntax here means to automatically execute the following commands at 8:30 every day. Required after modificationservice cron restart
Restart the service to take effect.
-
summary
The whole article is mainly aimed at the solution to the Internet access of Linux servers that need campus network authentication. It can be set as a short command line or an automatically executed task. The code idea uses the selenium module of Python to simulate the user’s operation behavior on the browser to achieve the goal. This method is much simpler than the construction request.
The article is for study.
——East Lake white goose