-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautohotspot
106 lines (80 loc) · 2.8 KB
/
autohotspot
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/bash
#This script was created and based on the script made from RaspberryConnect.com
#must be included in copies or derivatives of this script
#A Script to switch between a wifi network or a non internet routed Hotspot
#Works on startup or manually without a reboot or with a timer
#This script is targeted to work with NetworkManager with nmcli commands instead of the traditional raspberry pi network managment
#Device interface name to use, the Default is the wlan0
wifidev="wlan0"
KillHotspot()
{
echo "Autohotspot shutting down"
nmcli connection down Hotspot
}
ChkConnectionFiles()
{
#Checks if there are files on the directory
if [ -n "$(ls -A /etc/NetworkManager/system-connections/ 2>/dev/null)" ]
then
echo "contains Files (or is a file)"
else
echo "empty (or does not exist)"
fi
conReply =$((ls -A /etc/NetworkManager/system-connection | grep -v "Hotspot.nmconnection") 2>&1) >/dev/null 2>&1
printf '%s\n' "${conReply}"
}
TestSSIDConnection()
{
echo "AutoHotspot"
echo "Initial Testing...."
echo "Testing SSID connection and testing internet connection"
#Check to see what SSID's and MAC addresses are in range
ssidChk=('NoSSid')
sleep 20
nmcli dev wifi rescan
ssidscanreply = $((nmcli -t dev wifi list ) 2>&1 ) > /dev/null 2>&1
printf "%s" "${ssidscanreply[@]}"
ssidreply=$((nmcli -t -f NAME c show --active) 2>&1 ) >/dev/null 2>&1
echo "SSID in range: " $ssidreply
ip4_gateway=$((nmcli -g ip4.GATEWAY c s "$ssidreply") 2>&1) >/dev/null 2>&1
echo "The Gateway: " $ip4_gateway
if [ -z "$ip4_gateway" ] >/dev/null 2>&1 #Means this ssid connection has a gateway
then
echo "There is no ip4 gateway, therefore no ssid at all"
ssidChk="NoSSid"
return 0
elif [ "$ssidreply" == "Hotspot" ] >/dev/null 2>&1
then
echo "The Hotspot is active, the ssid is Hotspot, but ofcourse there is no internet connection"
ssidChk="Hotspot"
return 0
else
echo "There is a ip4 gateway therefore there must exist a valid ssid with internet connection"
ssidChk="$ssidreply"
return 0
fi
}
TestSSIDConnection
echo "SSID STATE :" $ssidChk
# Create Hotspot if no connection is available
if [ "$ssidChk" == "NoSSid" ]
then #No ssid in range, no internet connection, Connect to the Hotspot
echo "Here Hotspot is not running and there is no internet connection"
sleep 2
if nmcli connection show Hotspot >/dev/null 2>&1
then
# A connection with the name Hotspot exists
# Maybe try and connect to it
echo "Activating Hotspot"
nmcli con up Hotspot
sleep 2
else
#No connection Named Hotspot exists, Creating a Hotspot connection
echo "Creating a Hotspot connection"
nmcli dev wifi hotspot iframe wlan0 ssid OctoPrintHotspot password "1234567890"
sleep 20
echo "Activating the connection"
nmcli con up Hotspot
sleep 2
fi
fi