-
Notifications
You must be signed in to change notification settings - Fork 1
/
WLAN.py
193 lines (172 loc) · 6.3 KB
/
WLAN.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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import os
import sys
sys.path.append(os.path.dirname(__file__))
import json
from __core import execute
from __core import executeAndGetLines
from __core import executeNonQuery
from __core import xmlToDict
from __core import extractFirstElementfromDict
#----------------------------------------------------------------------
#List Profiles
def getProfilesJSON():
'''
returns a JSON-string with the wlan-profiles saved on this computer
'''
out = executeAndGetLines("netsh wlan show profiles")
OutStructure = __buildProfileStructure(out)
return OutStructure
def getProfilesList():
'''
returns a list of strings with the wlan-profiles saved on this computer
'''
return __getProfilesJSONToList(getProfilesJSON())
def __buildProfileStructure(lines):
Heading = lines[0]
lines = lines[1:]
Structure = {}
while len(lines) > 0:
if lines[1].startswith("-----"):
Structure[lines[0]] = []
cnt = 2
while cnt < len(lines) and not lines[cnt].startswith("-----") :
strLine = lines[cnt]
if strLine.find(":") >= 0: _, strLine = __getValuePairFromLine(strLine)
Structure[lines[0]].append(strLine)
cnt+=1
if cnt != len(lines):
Structure[lines[0]] = Structure[lines[0]][:-1]
cnt -=1
lines = lines[cnt:]
return json.dumps({Heading:Structure})
def __getProfilesJSONToList(getProfilesOutput):
'''
Converts the JSON Output of "getProfiles()" to a list
'''
getProfilesOutput = json.loads(getProfilesOutput)
firstKey = extractFirstElementfromDict(getProfilesOutput)
curStruct = getProfilesOutput[firstKey]
profileList = []
for key in curStruct:
for elem in curStruct[key]:
if elem != "<None>" and elem != "<Kein>":
profileList.append(elem)
return profileList
#----------------------------------------------------------------------
#profile config
def getProfileConfigurations():
'''
returns a list of JSON-encoded strings containing the advanced data of each profile on this computer
'''
return __abstr_getProfileConfig(profiles = getProfilesList())
def getProfileConfiguration(strProfile):
'''
returns a JSON-encoded string containing the advanced data of the specified Profile
'''
return __abstr_getProfileConfig(profiles=[strProfile])[0]
def __abstr_getProfileConfig(profiles):
if not os.path.exists("./tmp/") : os.mkdir("./tmp/")
for profile in profiles:
exportProfile(profile, "./tmp/")
files = os.listdir("./tmp/")
files = [x for x in files if x.endswith(".xml")]
profiles = []
for f in files:
try:
Structure = xmlToDict("./tmp/" + f)
profiles.append(json.dumps(Structure))
os.remove("./tmp/" + f)
except IOError:
pass
os.rmdir("./tmp/")
return profiles
#----------------------------------------------------------------------
#disconnect
def disconnect():
'''
shuts down the WLAN-Connection
'''
executeNonQuery("netsh wlan disconnect")
#-----------------------------------------------------------------------
#connect
def connectTo(strProfile):
'''
tries to establish a WLAN-Connection to the specified Profile
'''
executeNonQuery("netsh wlan connect " + strProfile)
#------------------------------------------------------------------------
#available profiles
def getAvilableNetworksJSON():
'''
returns an JSON-encoded list of Strings containing information of available Networks
'''
output = executeAndGetLines("netsh wlan show networks")
Wifis = []
output = output[1:] #Skip interface name
while output[0].find(":") < 0: output = output[1:]
while len(output) >= 4:
key, name = __getValuePairFromLine(output[0])
if key != None:
Structure = {key:{}}
Structure[key]["name"] = name
for x in range(1,4):
attr, value = __getValuePairFromLine(output[x])
Structure[key][attr] = value
Wifis.append(json.dumps(Structure))
output = output[4:]
return Wifis
def getAvilableNetworksList():
return __getAvailableNetworkJSONToList(getAvilableNetworksJSON())
def __getAvailableNetworkJSONToList(getProfilesOutput):
ProfileList = []
for jsonString in getProfilesOutput:
ssid = json.loads(jsonString)
firstKey = extractFirstElementfromDict(ssid)
ssid = ssid[firstKey]
ProfileList.append(ssid["name"])
return ProfileList
#------------------------------------------------------------------------
#Connection mode
def setConnectionMode(strProfile, setToAuto=True):
'''
updates the connection mode of the specified profile
'''
strMode = "auto" if setToAuto else "manual"
strCmd = 'netsh wlan set profileparameter name="' + strProfile + '" connectionmode=' + strMode
executeNonQuery(strCmd)
#------------------------------------------------------------------------
#profiles
def deleteProfile(strProfile):
'''
CAREFULL!!!! This function permanently deletes a profile
'''
strCmd = 'netsh wlan delete profile name="' + strProfile + '"'
executeNonQuery(strCmd)
def exportProfile(strProfile, strPath, clearKey=True):
'''
exports a profile to a xml-file
'''
strKey = "key=clear " if clearKey else ""
strCorrPath = os.path.normpath(strPath)
strCmd = 'netsh wlan export profile name="' + strProfile + '" ' + strKey + 'folder="' + strCorrPath + '"'
executeNonQuery(strCmd)
def addProfile(strPath):
'''
adds a profile to the saved configurations. Configuration has to be an exported .xml file
'''
strCorrPath = os.path.normpath(strPath)
strCmd = 'netsh wlan add profile filename="' + strCorrPath + '"'
executeNonQuery(strCmd)
#------------------------------------------------------------------------
#profiles
def getDriverReport():
'''
returns a string with all information available for drivers
'''
return execute("netsh wlan show drivers")
#------------------------------------------------------------------------
def __getValuePairFromLine(strLine):
splitter = strLine.split(":")
if len(splitter) == 2:
return splitter[0].strip(), splitter[1].strip()
else: return None, None