-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwpa_password.py
executable file
·134 lines (107 loc) · 4.03 KB
/
wpa_password.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
#! /usr/bin/python
import argparse
import os
import setproctitle
import psutil
import re
import shutil
from ConfigParser import SafeConfigParser
# Make sure we're the only version of this running
PROCNAME = "wpa_password.py"
for proc in psutil.process_iter():
# check whether the process name matches
if proc.name() == PROCNAME:
proc.kill()
setproctitle.setproctitle(PROCNAME)
re.UNICODE
class lineMangler:
"""Hold stuff for doing pattern matching"""
def __init__(self, regex, newline):
#if isinstance(regex, basestring):
# regex = re.compile(regex)
self.regex = re.compile(regex)
self.newline = newline
self.found = False
def findreplace(self, line, shouldReplace):
if self.regex.match(line):
if (shouldReplace and (not self.found)):
line = self.newline
self.found = True
else:
line = '# ' + line
return line
def missingLine(self):
if not self.found:
self.found = True
return self.newline
else:
return ""
#holy shit do not do this from a command line
#parser = argparse.ArgumentParser(description='Change the wifi password.')
#parser.add_argument('pass1', nargs='?', default='', help='Password')
#parser.add_argument('pass2', nargs='?', default='', help='Confirm Password')
#args = parser.parse_args()
#pass1 = args.pass1
#pass2 = args.pass2
# did we get a config from the CL?
parser = argparse.ArgumentParser(description='Change the wifi password.')
parser.add_argument('config', nargs='?', default='data/conductor/config.ini', help='Path to a config.ini file')
args = parser.parse_args()
filename = args.config
# read config
config = SafeConfigParser()
config.read(filename)
tmp_dir = config.get('working', 'tmp')
if tmp_dir is None:
tmp_dir = '/tmp/'
if tmp_dir.endswith('/') == False:
tmp_dir = tmp_dir +'/'
pass_file = tmp_dir + 'newpass.txt'
with open (pass_file) as f:
passwd = f.readline()
passwd = passwd.rstrip()
length = len(passwd)
if ((length == 0) or ((length >=8) and (length <=63) )):
# adequate length
check = re.compile('.*[^a-zA-Z0-9]+.*') #not letters and numbers
match = check.match(passwd)
#print match
if ((match is None) or (length == 0)):
# so far so good
havePass = False
if passwd != "": #Non blank password
havePass=True
pass_obj = lineMangler('^wpa_passphrase\s*=.*',
'wpa_passphrase='+passwd)
wpa2_obj = lineMangler('^wpa\s*=\s*[0-9]+', 'wpa=2')
key_obj = lineMangler('^wpa_key_mgmt\s*=\s*.*','wpa_key_mgmt=WPA-PSK')
rsn_obj = lineMangler('^rsn_pairwise\s*=\s*.*','rsn_pairwise=CCMP')
with open(tmp_dir + 'hostapd.conf', 'w') as result: #file to write
with open('/etc/hostapd/hostapd.conf') as conf: #original file
for line in conf:
line = line.rstrip()
line = pass_obj.findreplace(line, havePass)
line = wpa2_obj.findreplace(line, havePass)
line = key_obj.findreplace(line, havePass)
line = rsn_obj.findreplace(line, havePass)
result.write(line+'\n')
# end for
#end with
if havePass:
#make sure we get our lines into the file
result.write(pass_obj.missingLine() + '\n')
result.write(wpa2_obj.missingLine() + '\n')
result.write(key_obj.missingLine() + '\n')
result.write(rsn_obj.missingLine() + '\n')
#end with
# copy file to real location
shutil.copy('/tmp/hostapd.conf', '/etc/hostapd/hostapd.conf')
os.system('/etc/init.d/hostapd restart')
if havePass:
print "Success: Password Changed"
else:
print "Success: Wifi is passwordless"
else:
print "Failure: Password not set. Use only letters and numbers."
else :
print "Failure: Password not set. Must be between 8-63 characters."