-
Notifications
You must be signed in to change notification settings - Fork 0
/
register_network.sh
executable file
·77 lines (59 loc) · 1.92 KB
/
register_network.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/bin/bash
# Prompt the user for the SSID
read -p "Enter the SSID: " ssid
# Exit if the SSID is empty
if [ -z "$ssid" ]; then
echo "SSID cannot be empty"
exit 1
fi
# Exit if the SSID is called hotspot
if [ "$ssid" == "hotspot" ]; then
echo "SSID cannot be called hotspot"
exit 1
fi
# Prompt the user for the PSK (password)
read -sp "Enter the PSK: " psk
echo # To move to a new line after the password input
# Exit if the PSK is empty
if [ -z "$psk" ]; then
echo "PSK cannot be empty"
exit 1
fi
wifi_interface="wlan0" # Replace this with your WiFi interface name if different
echo "Creating connection $ssid"
# Delete any existing connection with the same SSID
nmcli connection delete id "$ssid" 2>/dev/null
# Check if deletion was successful or if the connection did not exist
if [ $? -eq 0 ]; then
echo "Deleted existing connection with SSID $ssid"
else
echo "No existing connection with SSID $ssid found, continuing with creation"
fi
# Add new connection profile
nmcli connection add type wifi ifname "$wifi_interface" con-name "$ssid" ssid "$ssid"
# Check if the connection profile was created successfully
if [ $? -ne 0 ]; then
echo "Failed to add new connection profile for SSID $ssid"
exit 1
fi
# Modify the connection to add security settings
nmcli connection modify "$ssid" wifi-sec.key-mgmt wpa-psk
# Check if the security settings were added successfully
if [ $? -ne 0 ]; then
echo "Failed to set key management for SSID $ssid"
exit 1
fi
nmcli connection modify "$ssid" wifi-sec.psk "$psk"
# Check if the passkey was set successfully
if [ $? -ne 0 ]; then
echo "Failed to set passkey for SSID $ssid"
exit 1
fi
echo "Connection $ssid created successfully"
# Display the connection profile for verification
connection_file="/etc/NetworkManager/system-connections/$ssid.nmconnection"
if [ -f "$connection_file" ]; then
cat "$connection_file"
else
echo "Connection file $connection_file not found"
fi