@@ -35,13 +35,6 @@ def file_replace_line(file_name, search_pattern, replace_pattern):
3535 line = re .sub (search_pattern , replace_pattern , line )
3636 print (line )
3737
38- def is_regex_in_file (file_name , search_pattern ):
39- the_file = open (file_name , 'r' )
40- return re .findall (search_pattern , the_file .read (), re .MULTILINE )
41-
42- def is_networkmanager ():
43- return subprocess .run (['systemctl' , '-q' , 'is-active' , 'NetworkManager' ]).returncode == 0
44-
4538def is_pi3 ():
4639 """True if this is a Pi 3 B (not Plus!)."""
4740 return "Pi 3 Model B Rev" in open ('/proc/device-tree/model' ).read ()
@@ -51,10 +44,7 @@ def is_pi3():
5144default_channel = '11'
5245default_country = 'CH'
5346default_password = 'moodlebox'
54- if is_networkmanager ():
55- default_ssid = 'MoodleBox'
56- else :
57- default_ssid = '4d6f6f646c65426f78' # This means 'MoodleBox'.
47+ default_ssid = 'MoodleBox'
5848default_ip_address = '10.0.0.1'
5949default_min_range = 10
6050default_max_range = 254
@@ -71,17 +61,8 @@ def is_pi3():
7161kernel_cmdline_file = "/boot/firmware/cmdline.txt"
7262hosts_file = "/etc/hosts"
7363nodogsplash_conf_file = "/etc/nodogsplash/nodogsplash.conf"
74- if is_networkmanager ():
75- dnsmasq_lease_file = "/tmp/dnsmasq.leases"
76- dnsmasq_conf_file = "/etc/NetworkManager/dnsmasq-shared.d/00-dhcp.conf"
77- else :
78- dnsmasq_lease_file = "/var/lib/misc/dnsmasq.leases"
79- if os .path .exists ("/etc/dnsmasq.d/uap0.conf" ):
80- dnsmasq_conf_file = "/etc/dnsmasq.d/uap0.conf"
81- else :
82- dnsmasq_conf_file = "/etc/dnsmasq.conf"
83- hostapd_conf_file = "/etc/hostapd/hostapd.conf"
84- dhcpcd_conf_file = "/etc/dhcpcd.conf"
64+ dnsmasq_lease_file = "/tmp/dnsmasq.leases"
65+ dnsmasq_conf_file = "/etc/NetworkManager/dnsmasq-shared.d/00-dhcp.conf"
8566
8667# New values taken from settings_file.
8768try :
@@ -118,19 +99,15 @@ def do_regulatory_country():
11899 if not re .search (regex , data ):
119100 new_country = default_country
120101 # new_country is now valid.
121- if is_networkmanager ():
122- # Set regulatory country in kernel command line.
123- file_replace_line (kernel_cmdline_file ,
124- r'\s*cfg80211.ieee80211_regdom=\S*' ,
125- r'' )
126- file_replace_line (kernel_cmdline_file ,
127- r'^(.*)$' ,
128- r'\1 cfg80211.ieee80211_regdom=' + new_country )
129- # Set regulatory country with iw.
130- subprocess .run (['sudo' , 'iw' , 'reg' , 'set' , new_country ])
131- else :
132- # Set regulatory country in hostapd config file.
133- file_replace_line (hostapd_conf_file , 'country_code=.*' , 'country_code=' + new_country )
102+ # Set regulatory country in kernel command line.
103+ file_replace_line (kernel_cmdline_file ,
104+ r'\s*cfg80211.ieee80211_regdom=\S*' ,
105+ r'' )
106+ file_replace_line (kernel_cmdline_file ,
107+ r'^(.*)$' ,
108+ r'\1 cfg80211.ieee80211_regdom=' + new_country )
109+ # Set regulatory country with iw.
110+ subprocess .run (['iw' , 'reg' , 'set' , new_country ])
134111
135112def do_channel ():
136113 """Channel setting."""
@@ -142,12 +119,8 @@ def do_channel():
142119 if new_country in ['CA' ,'US' ] and int (new_channel ) > 11 :
143120 new_channel = default_channel
144121 # new_channel is now valid.
145- if is_networkmanager ():
146- # Set channel with nmcli
147- subprocess .run (['sudo' , 'nmcli' , 'con' , 'mod' , 'WifiAP' , 'wifi.channel' , new_channel ])
148- else :
149- # Set channel in hostapd config file.
150- file_replace_line (hostapd_conf_file , 'channel=.*' , 'channel=' + new_channel )
122+ # Set channel with nmcli
123+ subprocess .run (['nmcli' , 'con' , 'mod' , 'WifiAP' , 'wifi.channel' , new_channel ])
151124
152125def do_ssid ():
153126 """SSID setting."""
@@ -159,19 +132,10 @@ def do_ssid():
159132 if not bool (ssid_pattern .search (new_ssid )):
160133 new_ssid = default_ssid
161134 # new_ssid is now valid.
162- if is_networkmanager ():
163- # Convert new_ssid into plain string.
164- new_ssid = binascii .unhexlify (new_ssid ).decode ()
165- # Set SSID with nmcli.
166- subprocess .run (['sudo' , 'nmcli' , 'con' , 'mod' , 'WifiAP' , 'wifi.ssid' , new_ssid ])
167- else :
168- # Set SSID in hostapd config file. We change ssid to ssid2 too.
169- file_replace_line (hostapd_conf_file , '^ssid2?=.*' , 'ssid2=' + new_ssid )
170- # Check if hostapd config file defines a 'utf8_ssid' key and add it when missing.
171- if not is_regex_in_file (hostapd_conf_file , r'^utf8_ssid=\b' ):
172- file_replace_line (hostapd_conf_file ,
173- '^(?P<ssid>ssid2?=(?:[0-9a-fA-F]{2}){1,32}).*$' ,
174- '\\ g<ssid>\n utf8_ssid=1' )
135+ # Convert new_ssid into plain string.
136+ new_ssid = binascii .unhexlify (new_ssid ).decode ()
137+ # Set SSID with nmcli.
138+ subprocess .run (['nmcli' , 'con' , 'mod' , 'WifiAP' , 'wifi.ssid' , new_ssid ])
175139
176140def do_ssid_hidden_state ():
177141 """SSID hidden state setting."""
@@ -180,23 +144,11 @@ def do_ssid_hidden_state():
180144 if ssid_hidden_state not in ['0' ,'1' ]:
181145 ssid_hidden_state = '0'
182146 # SSID hidden status setting is now valid.
183- if is_networkmanager ():
184- # Set ssid_hidden_state with nmcli.
185- if ssid_hidden_state == '1' :
186- subprocess .run (['sudo' , 'nmcli' , 'con' , 'mod' , 'WifiAP' , 'wifi.hidden' , 'yes' ])
187- else :
188- subprocess .run (['sudo' , 'nmcli' , 'con' , 'mod' , 'WifiAP' , 'wifi.hidden' , 'no' ])
147+ # Set ssid_hidden_state with nmcli.
148+ if ssid_hidden_state == '1' :
149+ subprocess .run (['nmcli' , 'con' , 'mod' , 'WifiAP' , 'wifi.hidden' , 'yes' ])
189150 else :
190- # Set ssid_hidden_state in hostapd config file.
191- # Check if line "ignore_broadcast_ssid=..." exist uncommented in hostapd config file.
192- if not is_regex_in_file (hostapd_conf_file , r'^ignore_broadcast_ssid=\b' ):
193- file_replace_line (hostapd_conf_file ,
194- '^(?P<hidden>utf8_ssid=[01]).*$' ,
195- '\\ g<hidden>\n # Show or hide SSID\n ignore_broadcast_ssid=' + ssid_hidden_state )
196- else :
197- file_replace_line (hostapd_conf_file ,
198- 'ignore_broadcast_ssid=.*' ,
199- 'ignore_broadcast_ssid=' + ssid_hidden_state )
151+ subprocess .run (['nmcli' , 'con' , 'mod' , 'WifiAP' , 'wifi.hidden' , 'no' ])
200152
201153def do_password_protected ():
202154 """Password protection setting."""
@@ -206,42 +158,23 @@ def do_password_protected():
206158 password_protected = True
207159 else :
208160 password_protected = (password_protected == '1' )
209- # Check if access point is currently password protected.
210- if is_networkmanager ():
211- # Check with nmcli if access point is currently password protected.
212- output = subprocess .run (
213- ['sudo' , 'nmcli' , '-g' , '802-11-wireless-security.psk' , 'con' , 'show' , 'WifiAP' ],
214- capture_output = True ,
215- text = True ,
216- ).stdout
217- # If output isn't empty (falsy), access point is currently password protected.
218- is_currently_protected = bool (output )
219- else :
220- # Check if line "wpa_passphrase=..." exists uncommented in hostapd config file.
221- # If found, the access point is currently password protected.
222- is_currently_protected = bool (is_regex_in_file (hostapd_conf_file , r'^wpa_passphrase=\b' ))
223- if is_networkmanager ():
224- # Set parameters adequately with nmcli.
225- if not password_protected and is_currently_protected :
226- subprocess .run (['sudo' , 'nmcli' , 'con' , 'mod' , 'WifiAP' , 'remove' , 'wifi-sec' ])
227- elif password_protected and not is_currently_protected :
228- subprocess .run (['sudo' , 'nmcli' , 'con' , 'mod' , 'WifiAP' , 'wifi-sec.key-mgmt' , 'wpa-psk' ])
229- subprocess .run (['sudo' , 'nmcli' , 'con' , 'mod' , 'WifiAP' , 'wifi-sec.psk' , new_password ])
230- subprocess .run (['sudo' , 'nmcli' , 'con' , 'mod' , 'WifiAP' , 'wifi-sec.group' , 'ccmp' ])
231- subprocess .run (['sudo' , 'nmcli' , 'con' , 'mod' , 'WifiAP' , 'wifi-sec.pairwise' , 'ccmp' ])
232- subprocess .run (['sudo' , 'nmcli' , 'con' , 'mod' , 'WifiAP' , 'wifi-sec.proto' , proto ])
233- else :
234- # Set parameters adequately in hostapd config file.
235- if not password_protected and is_currently_protected :
236- file_replace_line (hostapd_conf_file , '^wpa_passphrase=.*' , '# wpa_passphrase=moodlebox' )
237- file_replace_line (hostapd_conf_file , '^wpa=.*' , '# wpa=2' )
238- file_replace_line (hostapd_conf_file , '^wpa_key_mgmt=.*' , '# wpa_key_mgmt=WPA-PSK' )
239- file_replace_line (hostapd_conf_file , '^rsn_pairwise=.*' , '# rsn_pairwise=CCMP' )
240- elif password_protected and not is_currently_protected :
241- file_replace_line (hostapd_conf_file , '^#.*wpa_passphrase=.*' , 'wpa_passphrase=' + new_password )
242- file_replace_line (hostapd_conf_file , '^#.*wpa=.*' , 'wpa=2' )
243- file_replace_line (hostapd_conf_file , '^#.*wpa_key_mgmt=.*' , 'wpa_key_mgmt=WPA-PSK' )
244- file_replace_line (hostapd_conf_file , '^#.*rsn_pairwise=.*' , 'rsn_pairwise=CCMP' )
161+ # Check with nmcli if access point is currently password protected.
162+ output = subprocess .run (
163+ ['nmcli' , '-g' , '802-11-wireless-security.psk' , 'con' , 'show' , 'WifiAP' ],
164+ capture_output = True ,
165+ text = True ,
166+ ).stdout
167+ # If output isn't empty (falsy), access point is currently password protected.
168+ is_currently_protected = bool (output )
169+ # Set parameters adequately with nmcli.
170+ if not password_protected and is_currently_protected :
171+ subprocess .run (['nmcli' , 'con' , 'mod' , 'WifiAP' , 'remove' , 'wifi-sec' ])
172+ elif password_protected and not is_currently_protected :
173+ subprocess .run (['nmcli' , 'con' , 'mod' , 'WifiAP' , 'wifi-sec.key-mgmt' , 'wpa-psk' ])
174+ subprocess .run (['nmcli' , 'con' , 'mod' , 'WifiAP' , 'wifi-sec.psk' , new_password ])
175+ subprocess .run (['nmcli' , 'con' , 'mod' , 'WifiAP' , 'wifi-sec.group' , 'ccmp' ])
176+ subprocess .run (['nmcli' , 'con' , 'mod' , 'WifiAP' , 'wifi-sec.pairwise' , 'ccmp' ])
177+ subprocess .run (['nmcli' , 'con' , 'mod' , 'WifiAP' , 'wifi-sec.proto' , proto ])
245178
246179def do_password ():
247180 """Password setting."""
@@ -254,16 +187,12 @@ def do_password():
254187 if not bool (password_pattern .search (new_password )):
255188 new_password = default_password
256189 # new_password is now valid.
257- if is_networkmanager ():
258- # Set password with nmcli.
259- subprocess .run (['sudo' , 'nmcli' , 'con' , 'mod' , 'WifiAP' , 'wifi-sec.key-mgmt' , 'wpa-psk' ])
260- subprocess .run (['sudo' , 'nmcli' , 'con' , 'mod' , 'WifiAP' , 'wifi-sec.psk' , new_password ])
261- subprocess .run (['sudo' , 'nmcli' , 'con' , 'mod' , 'WifiAP' , 'wifi-sec.group' , 'ccmp' ])
262- subprocess .run (['sudo' , 'nmcli' , 'con' , 'mod' , 'WifiAP' , 'wifi-sec.pairwise' , 'ccmp' ])
263- subprocess .run (['sudo' , 'nmcli' , 'con' , 'mod' , 'WifiAP' , 'wifi-sec.proto' , proto ])
264- else :
265- # Set password in hostapd config file.
266- file_replace_line (hostapd_conf_file , '^wpa_passphrase=.*$' , 'wpa_passphrase=' + new_password )
190+ # Set password with nmcli.
191+ subprocess .run (['nmcli' , 'con' , 'mod' , 'WifiAP' , 'wifi-sec.key-mgmt' , 'wpa-psk' ])
192+ subprocess .run (['nmcli' , 'con' , 'mod' , 'WifiAP' , 'wifi-sec.psk' , new_password ])
193+ subprocess .run (['nmcli' , 'con' , 'mod' , 'WifiAP' , 'wifi-sec.group' , 'ccmp' ])
194+ subprocess .run (['nmcli' , 'con' , 'mod' , 'WifiAP' , 'wifi-sec.pairwise' , 'ccmp' ])
195+ subprocess .run (['nmcli' , 'con' , 'mod' , 'WifiAP' , 'wifi-sec.proto' , proto ])
267196
268197def do_ip_address ():
269198 """Static IP setting."""
@@ -294,28 +223,14 @@ def do_ip_address():
294223 file_replace_line (dnsmasq_conf_file ,
295224 '^address=\\ /home\\ /.*$' ,
296225 'address=/home/' + new_static_ip )
297- if is_networkmanager ():
298- subprocess .run (['sudo' , 'nmcli' , 'con' , 'mod' , 'WifiAP' , 'ipv4.addresses' , new_static_ip + '/24' ])
299- subprocess .run (['sudo' , 'nmcli' , 'con' , 'mod' , 'WifiAP' , 'ipv4.gateway' , new_static_ip ])
300- file_replace_line (dnsmasq_conf_file ,
301- '^dhcp-option=6,' + ip_regex + '(?P<end>.*)$' ,
302- 'dhcp-option=6,' + new_static_ip + '\\ g<end>' )
303- else :
304- file_replace_line (dhcpcd_conf_file ,
305- '^static ip_address=.*$' ,
306- 'static ip_address=' + new_static_ip + '/24' )
307- file_replace_line (dnsmasq_conf_file ,
308- '^listen-address=(?!127).*$' ,
309- 'listen-address=' + new_static_ip )
310- file_replace_line (dnsmasq_conf_file ,
311- '^dhcp-range=wifi,' + ip_regex + ',' + ip_regex + ',(?P<end>.*)$' ,
312- 'dhcp-range=wifi,' + min_range + ',' + max_range + ',\\ g<end>' )
313- file_replace_line (dnsmasq_conf_file ,
314- '^dhcp-option=wifi,6,' + ip_regex + '(?P<end>.*)$' ,
315- 'dhcp-option=wifi,6,' + new_static_ip + '\\ g<end>' )
226+ subprocess .run (['nmcli' , 'con' , 'mod' , 'WifiAP' , 'ipv4.addresses' , new_static_ip + '/24' ])
227+ subprocess .run (['nmcli' , 'con' , 'mod' , 'WifiAP' , 'ipv4.gateway' , new_static_ip ])
228+ file_replace_line (dnsmasq_conf_file ,
229+ '^dhcp-option=6,' + ip_regex + '(?P<end>.*)$' ,
230+ 'dhcp-option=6,' + new_static_ip + '\\ g<end>' )
316231
317232def fix_wrong_kernel_cmdline ():
318- """Fix buggy file produced by buggy script in version 2.17.0 and 2.17.1."""
233+ """Fix buggy file produced by buggy script in versions 2.17.0 and 2.17.1."""
319234 kernel_cmdline_tofix = "/boot/cmdline.txt"
320235 if os .path .exists (kernel_cmdline_tofix ) and not os .path .islink (kernel_cmdline_tofix ):
321236 os .remove (kernel_cmdline_tofix )
@@ -326,8 +241,7 @@ def fix_wrong_kernel_cmdline():
326241
327242# Actions.
328243
329- if is_networkmanager ():
330- fix_wrong_kernel_cmdline ()
244+ fix_wrong_kernel_cmdline ()
331245do_regulatory_country ()
332246do_channel ()
333247do_ssid ()
@@ -346,11 +260,6 @@ def fix_wrong_kernel_cmdline():
346260 pass
347261
348262# Restart networking and hostapd service.
349- if is_networkmanager ():
350- subprocess .call (['systemctl' , 'restart' , 'NetworkManager.service' ])
351- else :
352- subprocess .call (['systemctl' , 'restart' , 'hostapd.service' ])
353- subprocess .call (['systemctl' , 'restart' , 'dnsmasq.service' ])
354- subprocess .call (['systemctl' , 'restart' , 'networking.service' ])
263+ subprocess .call (['systemctl' , 'restart' , 'NetworkManager.service' ])
355264
356265# The end.
0 commit comments