-
Notifications
You must be signed in to change notification settings - Fork 1
/
autoconnect.py
62 lines (60 loc) · 2.61 KB
/
autoconnect.py
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
#By LoreBadTime
import os,subprocess
from subprocess import PIPE, STDOUT, CREATE_NO_WINDOW
dir_path = os.path.dirname(os.path.realpath(__file__))
#getting all wifi nearby connections
result = subprocess.run(['netsh', 'wlan','show','networks'], stdout=subprocess.PIPE,creationflags=CREATE_NO_WINDOW)
result = result.stdout.decode('utf-8').splitlines()
cont = 0
add = 0
tmplist = []
found = False
listofconnections = []
#getting all open connections and chosing the better one
while cont < len(result):
# Run "netsh wlan show networks",to make it work you need to replace this string below with the line that you see in command output that is like
# " Autentication/Security : Open "
# Otherwise the script can have some problems running since different language
if result[cont] == " Autenticazione : Aperta":
tmplist = []
#getting and storing the name of the open wifi
for char in result[cont - 2]:
if char == ' ':
add = add + 1
if add >= 4 :
tmplist.append(char)
if add == 3 :
add = add + 1
connection = tmplist
found = True
cont = cont + 1
connection = ''.join(connection)
# opening the Windows configuration default open wireless System file and Replacing the template with the name of the open connection
# !!! this scipt NEEDS the "connection.txt" File (provided on github page) into the same folder,or you can just hardcode the path of the file
f = open("connection.txt",'r+')
lines=f.readlines()
listedlines = []
for line in lines:
listedlines.append(line)
newlist = []
for line in listedlines:
if line == '\t<name>example</name>\n':
line = '\t<name>' + str(connection) + '</name>\n'
if line == '\t\t\t<name>example</name>\n':
line = '\t\t\t<name>' + str(connection) + '</name>\n'
newlist.append(line)
#creating a new configuration file,i chose w+ to avoid errors
file = open(str(connection) + ".xml",'w+')
for line in newlist:
file.write(line)
f.close()
file.close()
#final script load
#this will delete the open connection(if already stored in Windows) before loading the new connection
try:
subprocess.run(['netsh', 'wlan', 'delete','profile', 'filename="'+ connection + '.xml"'],creationflags=CREATE_NO_WINDOW)
except:
pass
#adding and connecting the connection
subprocess.run(['netsh', 'wlan', 'add','profile', 'filename="'+ connection + '.xml"'],creationflags=CREATE_NO_WINDOW)
subprocess.run(['netsh', 'wlan', 'connect', 'name=' + connection],creationflags=CREATE_NO_WINDOW)