Created
January 9, 2025 13:38
-
-
Save RocketRene/7443f5302c4582100523b22243083bc9 to your computer and use it in GitHub Desktop.
Connect to HPI Wifi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Function to check if nmcli is available | |
| check_requirements() { | |
| if ! command -v nmcli &> /dev/null; then | |
| echo "Error: nmcli is not installed. Please install NetworkManager." | |
| exit 1 | |
| fi | |
| } | |
| # Function to validate input is not empty | |
| validate_input() { | |
| local input=$1 | |
| local field_name=$2 | |
| if [ -z "$input" ]; then | |
| echo "Error: $field_name cannot be empty" | |
| exit 1 | |
| fi | |
| } | |
| # Print welcome message | |
| echo "HPI WiFi Setup Script" | |
| echo "====================" | |
| echo "This script will help you configure the HPI WiFi connection." | |
| echo | |
| # Check requirements | |
| check_requirements | |
| # Get username | |
| read -p "Enter your HPI username (without 'hpi\'): " USERNAME | |
| validate_input "$USERNAME" "Username" | |
| # Get password securely | |
| echo "Enter your password: " | |
| read -s PASSWORD | |
| echo | |
| validate_input "$PASSWORD" "Password" | |
| # Confirm password | |
| echo "Confirm your password: " | |
| read -s PASSWORD_CONFIRM | |
| echo | |
| validate_input "$PASSWORD_CONFIRM" "Password confirmation" | |
| # Check if passwords match | |
| if [ "$PASSWORD" != "$PASSWORD_CONFIRM" ]; then | |
| echo "Error: Passwords do not match" | |
| exit 1 | |
| fi | |
| echo | |
| echo "Creating WiFi connection..." | |
| # Delete existing connection if it exists | |
| if nmcli connection show "HPI" &> /dev/null; then | |
| echo "Removing existing HPI connection..." | |
| nmcli connection delete "HPI" | |
| fi | |
| # Create the connection | |
| if ! nmcli connection add \ | |
| type wifi \ | |
| con-name "HPI" \ | |
| ssid "hpi" \ | |
| wifi-sec.key-mgmt wpa-eap \ | |
| 802-1x.eap peap \ | |
| 802-1x.phase2-auth mschapv2 \ | |
| 802-1x.domain-suffix-match "hpi.uni-potsdam.de" \ | |
| 802-1x.system-ca-certs no \ | |
| 802-1x.identity "hpi\\$USERNAME" \ | |
| 802-1x.anonymous-identity "$USERNAME" \ | |
| 802-1x.password "$PASSWORD"; then | |
| echo "Error: Failed to create connection" | |
| exit 1 | |
| fi | |
| # Set identity | |
| echo "Setting identity..." | |
| if ! nmcli connection modify "HPI" \ | |
| 802-1x.identity "hpi\\$USERNAME" \ | |
| 802-1x.anonymous-identity "$USERNAME"; then | |
| echo "Error: Failed to set identity" | |
| exit 1 | |
| fi | |
| # Set password | |
| echo "Setting password..." | |
| if ! nmcli connection modify "HPI" \ | |
| 802-1x.password "$PASSWORD" \ | |
| 802-1x.password-flags 0; then | |
| echo "Error: Failed to set password" | |
| exit 1 | |
| fi | |
| # Enable autoconnect | |
| echo "Enabling autoconnect..." | |
| if ! nmcli connection modify "HPI" connection.autoconnect yes; then | |
| echo "Error: Failed to enable autoconnect" | |
| exit 1 | |
| fi | |
| # Try to connect | |
| echo | |
| echo "Attempting to connect..." | |
| if ! nmcli connection up "HPI"; then | |
| echo "Error: Failed to connect to HPI network" | |
| echo "Please check your credentials and try again" | |
| exit 1 | |
| fi | |
| echo | |
| echo "Success! You should now be connected to the HPI network." | |
| echo "To check connection status, use: nmcli connection show HPI" | |
| echo "To show current WiFi status, use: nmcli device wifi show" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Function to check if nmcli is available | |
| check_requirements() { | |
| if ! command -v nmcli &> /dev/null; then | |
| echo "Error: nmcli is not installed. Please install NetworkManager." | |
| exit 1 | |
| fi | |
| } | |
| # Function to validate input is not empty | |
| validate_input() { | |
| local input=$1 | |
| local field_name=$2 | |
| if [ -z "$input" ]; then | |
| echo "Error: $field_name cannot be empty" | |
| exit 1 | |
| fi | |
| } | |
| # Print welcome message | |
| echo "HPI WiFi Setup Script" | |
| echo "====================" | |
| echo "This script will help you configure the HPI WiFi connection." | |
| echo | |
| # Check requirements | |
| check_requirements | |
| # Get username | |
| read -p "Enter your HPI username (without 'hpi\'): " USERNAME | |
| validate_input "$USERNAME" "Username" | |
| # Get password securely | |
| echo "Enter your password: " | |
| read -s PASSWORD | |
| echo | |
| validate_input "$PASSWORD" "Password" | |
| # Confirm password | |
| echo "Confirm your password: " | |
| read -s PASSWORD_CONFIRM | |
| echo | |
| validate_input "$PASSWORD_CONFIRM" "Password confirmation" | |
| # Check if passwords match | |
| if [ "$PASSWORD" != "$PASSWORD_CONFIRM" ]; then | |
| echo "Error: Passwords do not match" | |
| exit 1 | |
| fi | |
| echo | |
| echo "Creating WiFi connection..." | |
| # Delete existing connection if it exists | |
| if nmcli connection show "HPI" &> /dev/null; then | |
| echo "Removing existing HPI connection..." | |
| nmcli connection delete "HPI" | |
| fi | |
| # Create the connection | |
| if ! nmcli connection add \ | |
| type wifi \ | |
| con-name "HPI" \ | |
| ssid "hpi" \ | |
| wifi-sec.key-mgmt wpa-eap \ | |
| 802-1x.eap peap \ | |
| 802-1x.phase2-auth mschapv2 \ | |
| 802-1x.domain-suffix-match "hpi.uni-potsdam.de" \ | |
| 802-1x.system-ca-certs no \ | |
| 802-1x.identity "hpi\\$USERNAME" \ | |
| 802-1x.anonymous-identity "$USERNAME" \ | |
| 802-1x.password "$PASSWORD" \ | |
| 802-1x.password-flags 0 \ | |
| 802-1x.password-raw-flags 0; then | |
| echo "Error: Failed to create connection" | |
| exit 1 | |
| fi | |
| # Set password flags | |
| echo "Setting password flags..." | |
| if ! nmcli connection modify "HPI" 802-1x.password-flags 0; then | |
| echo "Error: Failed to set password flags" | |
| exit 1 | |
| fi | |
| # Enable autoconnect | |
| echo "Enabling autoconnect..." | |
| if ! nmcli connection modify "HPI" connection.autoconnect yes; then | |
| echo "Error: Failed to enable autoconnect" | |
| exit 1 | |
| fi | |
| # Try to connect | |
| echo | |
| echo "Attempting to connect..." | |
| if ! nmcli --ask connection up "HPI"; then | |
| echo "Error: Failed to connect to HPI network" | |
| echo "Please check your credentials and try again" | |
| exit 1 | |
| fi | |
| echo | |
| echo "Success! You should now be connected to the HPI network." | |
| echo "To check connection status, use: nmcli connection show HPI" | |
| echo "To show current WiFi status, use: nmcli device wifi show" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment