Automatically log into the Tsinghua University campus network portal using Python and Selenium in headless mode.
- ✅ Headless browser automation (no GUI required)
- ✅ Command line interface
- ✅ Environment variable support for credentials
- ✅ Comprehensive logging
- ✅ Error handling and timeout management
- ✅ Automatic external network access (IPv4) checkbox selection
- Python 3.6+
- Chrome or Chromium browser
- Internet connection
-
Clone or download the project:
cd /root/tunet -
Run the setup script:
chmod +x setup.sh ./setup.sh
This will install all required Python packages and check for browser availability.
Create a .env file from the example:
cp .env.example .env
# Edit .env with your credentialsThen simply run:
python3 tunet_login.pyexport TUNET_USERNAME='your_username'
export TUNET_PASSWORD='your_password'
python3 tunet_login.pypython3 tunet_login.py -u your_username -p your_passwordpython3 tunet_login.py -u your_username -p your_password --no-headlessusage: tunet_login.py [-h] [-u USERNAME] [-p PASSWORD] [--no-headless]
[--timeout TIMEOUT] [-v] [-q]
Tsinghua University Network Auto Login
optional arguments:
-h, --help show this help message and exit
-u USERNAME, --username USERNAME
Campus network username
-p PASSWORD, --password PASSWORD
Campus network password
--no-headless Run with visible browser window
--timeout TIMEOUT Timeout in seconds (default: 30)
-v, --verbose Enable verbose logging
-q, --quiet Quiet mode - no output if login successful or already logged in# Edit crontab
crontab -e
# Add line to run every hour (adjust path as needed)
0 * * * * cd /root/tunet && python3 tunet_login.py# Edit crontab
crontab -e
# Add line to run every hour using virtual environment
# Replace /path/to/your/project with your actual project path
0 * * * * cd /path/to/your/project && /path/to/your/project/venv/bin/python tunet_login.py
# Alternative: use full paths and quiet mode for cron
0 * * * * /path/to/your/project/venv/bin/python /path/to/your/project/tunet_login.py -q
# Example with actual paths:
# 0 * * * * cd /home/user/tunet-python && /home/user/tunet-python/venv/bin/python tunet_login.py -q- Create service file:
sudo nano /etc/systemd/system/tunet-login.service- Add content:
[Unit]
Description=Tsinghua University Network Auto Login
After=network.target
[Service]
Type=oneshot
Environment=TUNET_USERNAME=your_username
Environment=TUNET_PASSWORD=your_password
WorkingDirectory=/root/tunet
ExecStart=/usr/bin/python3 tunet_login.py
User=root
[Install]
WantedBy=multi-user.target- Enable and start:
sudo systemctl enable tunet-login.service
sudo systemctl start tunet-login.service- Environment Variables: The most secure way to store credentials
- File Permissions: Ensure scripts have appropriate permissions
- Password Storage: Never commit passwords to version control
If you encounter ChromeDriver issues:
-
Install ChromeDriver manually:
# Download ChromeDriver from https://chromedriver.chromium.org/ # Or use package manager: sudo apt-get install chromium-chromedriver # Ubuntu/Debian
-
Use Selenium Manager (automatic, recommended):
# Selenium Manager (built into Selenium 4.6+) automatically handles driver downloads # No additional setup required - just ensure Chrome/Chromium is installed
- Ensure you're connected to the Tsinghua University network
- Check if the portal URL is accessible:
curl http://auth6.tsinghua.edu.cn/
- "Username and password must be provided": Set environment variables or use command line arguments
- "Timeout waiting for page elements": Increase timeout with
--timeout 60 - "Failed to initialize Chrome driver": Install Chrome/Chromium and ChromeDriver
/root/tunet/
├── tunet_login.py # Main script
├── requirements.txt # Python dependencies
├── setup.sh # Installation script
├── .env.example # Environment variables template
└── README.md # This file
This project is for educational and personal use only. Please comply with Tsinghua University's network usage policies.