-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautoconnect.py
More file actions
executable file
·42 lines (33 loc) · 1.1 KB
/
Copy pathautoconnect.py
File metadata and controls
executable file
·42 lines (33 loc) · 1.1 KB
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
#!/usr/bin/python
import platform # For getting the operating system name
import subprocess # For executing a shell command
import sys
import time
if len(sys.argv) != 3:
print("Usage: python3 autoconnect.py <ip_address> <essid>")
sys.exit(1)
ip_address = str(sys.argv[1])
essid = str(sys.argv[2])
def ping(host):
status,result = subprocess.getstatusoutput("ping -c1 -w2 " + host)
ms = 0
if status == 0:
ms = result.split("time=")[1].split(" ")[0] # get the time in ms
return [status == 0, ms]
def connectToWifi():
"""
Connects to the wifi network with the given essid and password.
Returns True if connection is successful, False otherwise.
"""
command = ['nmcli', 'dev', 'wifi', 'connect', essid]
return subprocess.call(command) == 0
while True:
[connected, ms] = ping(ip_address)
if connected:
print("Connected to " + ip_address, ms + "ms")
else:
print("Could not connect to " + ip_address)
print("Re-connect wifi to " + essid)
if connectToWifi():
print("Connected to " + essid)
time.sleep(5)