@@ -13,7 +13,7 @@ import subprocess
13
13
import sys
14
14
15
15
16
- ## vars ##### ###################################################################
16
+ # vars ###################################################################
17
17
iface = "wlan0"
18
18
net_conf_dir = "/etc/systemd/network/"
19
19
net_conf_file = net_conf_dir + "/20-net.network"
@@ -28,16 +28,20 @@ network={
28
28
}
29
29
'''
30
30
31
- ## functions ###################################################################
31
+ # functions ##############################################################
32
+
33
+
32
34
def pretty_print (line , prefix = "" ):
33
35
print (prefix + line .decode ('utf-8' ))
34
36
37
+
35
38
def print_ssid (line , prefix = "" ):
36
39
if line .find (b'SSID:' ) >= 0 :
37
40
_ = line .split (b'SSID:' )
38
41
if len (_ ) > 1 and len (_ [1 ].strip ()) > 0 :
39
42
pretty_print (_ [1 ], prefix )
40
43
44
+
41
45
def call (command , interrupt = False , pp = pretty_print ):
42
46
print ("\033 [0;32m>>> %s\033 [0m" % command )
43
47
proc = subprocess .Popen (command .split (), stdout = subprocess .PIPE )
@@ -50,12 +54,18 @@ def call(command, interrupt=False, pp=pretty_print):
50
54
print ("!!! Failure" )
51
55
sys .exit (1 )
52
56
57
+
53
58
def wpa_psk (ssid , password ):
54
- dk = hashlib .pbkdf2_hmac ('sha1' , str .encode (password ), str .encode (ssid ), 4096 , 32 )
59
+ dk = hashlib .pbkdf2_hmac (
60
+ 'sha1' ,
61
+ str .encode (password ),
62
+ str .encode (ssid ),
63
+ 4096 ,
64
+ 32 )
55
65
return (binascii .hexlify (dk )).decode ('utf8' )
56
66
57
67
58
- ## command line options parser ##### ############################################
68
+ # command line options parser ############################################
59
69
parser = argparse .ArgumentParser ()
60
70
parser .add_argument ("-i" , "--interface" , dest = "iface" , default = iface ,
61
71
help = " (default: %s)" % iface )
@@ -67,13 +77,13 @@ iface = args.iface
67
77
disable = args .disable
68
78
wait = args .wait
69
79
70
- ## main ####################################################################
80
+ # main # ####################################################################
71
81
if __name__ == "__main__" :
72
82
if os .geteuid () != 0 :
73
83
print ("\033 [0;31mYou need root permissions to run this command.\033 " )
74
84
sys .exit (2 )
75
85
76
- if False : # Deprecated code
86
+ if False : # Deprecated code
77
87
call ("rm -rf %s/*" % net_conf_dir )
78
88
config = configparser .ConfigParser ()
79
89
config ['Match' ] = {}
@@ -90,8 +100,11 @@ if __name__ == "__main__":
90
100
call ("rm %s" % wpa_suplicant_conf_file % iface )
91
101
else :
92
102
call ("iw dev %s scan" % iface , pp = print_ssid )
93
- ssid = input ("\033 [0;37mEnter the WiFi SSID (in iface %s):\033 [0m " % iface )
94
- passphrase = input ("\033 [0;37mEnter the WiFi passphrase (Min. 8 chars):\033 [0m " )
103
+ ssid = input (
104
+ "\033 [0;37mEnter the WiFi SSID (in iface %s):\033 [0m " %
105
+ iface )
106
+ passphrase = input (
107
+ "\033 [0;37mEnter the WiFi passphrase (Min. 8 chars):\033 [0m " )
95
108
96
109
if not os .path .exists (wpa_suplicant_conf_dir ):
97
110
call ("mkdir %s" % wpa_suplicant_conf_dir )
@@ -103,10 +116,11 @@ if __name__ == "__main__":
103
116
call ("systemctl restart -l wpa_supplicant@%s.service" % iface )
104
117
call ("systemctl status -l wpa_supplicant@%s.service" % iface )
105
118
106
- ssid = print ("\033 [1;32mWaiting %s seconds to get IP in %s ...\033 [0m " % (wait_seconds , iface ))
119
+ ssid = print (
120
+ "\033 [1;32mWaiting %s seconds to get IP in %s ...\033 [0m " %
121
+ (wait_seconds , iface ))
107
122
time .sleep (wait_seconds )
108
123
call ("ip address show %s" % iface )
109
124
if wait :
110
125
print ("\033 [1;32mPress any key to exit ...\033 [0m " )
111
126
input ()
112
-
0 commit comments