def create_visible_networks(): """Find all broadcasting SSIDs and save in visible_networks file. Return True if we got a good file""" network_good = False for do_resets in range(3): reset_network() net_list = [] if network_exists('wlan0'): if network_up('wlan0'): network_good = True for get_nets in range(3): try: net_list = collect_networks() if len(net_list) == 0: time.sleep(3) continue else: logger.warning('Found valid wifi connection.') break except: time.sleep(3) continue elif network_up('eth0'): network_good = True logger.warning('Found valid ethernet connection.') net_list = ['Irricloud valid eth0'] # see boiler_net_finish.py else: network_good = False self.logger.warning('found no valid network. Retrying') time.sleep(3) continue if len(net_list) == 0: net_list.append('No Networks Found') network_good = False else: network_good = True break with open('data/visible_networks','w') as f: json.dump(net_list, f) return network_good
def create_visible_networks(): """Find all broadcasting SSIDs and save in visible_networks file. Return True if we got a good file""" network_good = False for do_resets in range(3): reset_network() net_list = [] if network_exists('wlan0'): if network_up('wlan0'): network_good = True for get_nets in range(3): try: net_list = collect_networks() if len(net_list) == 0: time.sleep(3) continue else: logger.warning('Found valid wifi connection.') break except: time.sleep(3) continue elif network_up('eth0'): network_good = True logger.warning('Found valid ethernet connection.') net_list = ['Irricloud valid eth0'] # see boiler_net_finish.py else: network_good = False self.logger.warning('found no valid network. Retrying') time.sleep(3) continue if len(net_list) == 0: net_list.append('No Networks Found') network_good = False else: network_good = True break with open('data/visible_networks', 'w') as f: json.dump(net_list, f) return network_good
def POST(self): global error_msg if not been_through_subconfig: self.logger.info('tried Net POST....back to /su') raise web.seeother('/su') self.logger.debug('in Net POST') save_last_get('Net POST') form = web.input() error_msg = '' outdata = [] for f in ['Static IP', 'Netmask', 'Gateway', 'DNS Nameservers', 'SSID', 'Password', 'Hidden SSID', 'UPnP Refresh Rate', 'Station Name', 'Station Port', 'External Station Port', 'Remote Substation Access Port', 'Extender SSID', 'Extender Password']: if f in form: form[f] = form[f].strip() # use hidden SSID if provided using_hidden_ssid = False if 'Hidden SSID' in form and form['Hidden SSID'] != '': using_hidden_ssid = True form['SSID'] = form['Hidden SSID'] self.logger.info('using Hidden SSID') use_eth0 = 'SSID' not in form or form['SSID'] == 'ethernet' if gv.sd['remote_radio_substation']: form['SSID'] = 'using radio' net = 'radio' elif not use_eth0: net = 'wlan0' else: form['SSID'] = 'using eth0' net = 'eth0' self.logger.debug('in Net POST: ' + form['SSID']) gv.sd['light_ip'] = 1 try: if form['DNS Nameservers'][0:1] == 'X' and \ (len(form['DNS Nameservers']) == 1 or form['DNS Nameservers'][1:2] == ' '): gv.sd['light_ip'] = 0 # hack to disable blinking led on startup with leading X in nameservers form['DNS Nameservers'] = form['DNS Nameservers'][1:].strip() except: pass if not gv.sd['remote_radio_substation'] and 'Use DHCP' in form: for d in ['DNS Search', 'DNS Nameservers']: if d in form and form[d] != '': error_msg = 'Error: "' + d + '" must be blank with "Use DHCP".' raise web.seeother('/cn') # back to form pass_len = 0 if 'Extender Password' not in form else len(form['Extender Password']) if 'Extender SSID' in form and form['Extender SSID'] != '': if pass_len < 8 or pass_len > 63: error_msg = 'Error: "Extender Password" must be between 8 and 63 characters.' raise web.seeother('/cn') # back to form else: # force data to be present and safe form['Extender SSID'] = '' form['Extender Password'] = '' pass_len = 0 if 'Password' not in form else len(form['Password']) if not gv.sd['remote_radio_substation'] and not using_eth0 and (pass_len < 8 or pass_len > 63): error_msg = 'Error: "Password" must be between 8 and 63 characters.' raise web.seeother('/cn') # back to form if 'Station Name' in form: sn = validate_fqdn(form['Station Name']) if sn == 'Irricloud': # error condition error_msg = 'Error: "Station Name" must have only letter and numbers.' raise web.seeother('/cn') # back to form elif len(sn) > 50 or len(sn) < 1: error_msg = 'Error: "Station Name" must be at most 50 characters.' raise web.seeother('/cn') # back to form form['Station Name'] = sn for portn in ['Station Port', 'External Station Port', 'Remote Substation Access Port', 'UPnP Refresh Rate']: if portn in form: try: port = int(form[portn]) except: error_msg = 'Error: "' + portn + '" must be integer.' raise web.seeother('/cn') # back to form if port < 0 or port > 65535: error_msg = 'Error: "' + portn + '" must be between 0 and 65535.' raise web.seeother('/cn') # back to form if portn == 'Station Port' and port == 9080: # specially reserved for proxy error_msg = 'Error: "' + portn + '" cannot be 9080.' raise web.seeother('/cn') # back to form try: drop = 0 with open('/etc/network/interfaces','r') as infile: iface = infile.readlines() for line in iface: if 'dns-nameservers' in line: continue # delete any old dns stuff did_iface_wlan0 = False if drop > 0: drop -= 1 continue if 'iface ' + net + ' inet' in line: if net == 'wlan0': did_iface_wlan0 = True if 'Use DHCP' in form: self.logger.info('Using dhcp: ' + form['SSID']) if 'static' in line: drop = 3 line = 'iface ' + net + ' inet manual\n' else: self.logger.info('Using static ip: ' + form['Static IP'] + ' ' + form['Netmask'] + ' ' + form['Gateway']) if 'static' in line: drop = 3 outdata.append('iface ' + net + ' inet static\n') outdata.append(' address ' + form['Static IP'] + '\n') outdata.append(' netmask ' + form['Netmask'] + '\n') line = ' gateway ' + form['Gateway'] +'\n' outdata.append(line) if 'DNS Nameservers' in form and form['DNS Nameservers'] != '': line = ' dns-nameservers ' + form['DNS Nameservers'] +'\n' else: # use gateway line = ' dns-nameservers ' + form['Gateway'] +'\n' elif 'iface ' + 'wlan0' + ' inet' in line: # using eth0 or radio only, but need to clean up 10.0.0.1 entry if 'static' in line: drop = 3 line = 'iface ' + 'wlan0' + ' inet manual\n' did_iface_wlan0 = True outdata.append(line) if did_iface_wlan0: outdata.append(' wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf\n') except Exception as ex: self.logger.exception('Unexpected exception trying to configure network: ' + str(ex)) raise web.seeother('/cn') if len(outdata) == 0: self.logger.error('Expected data in /etc/network/interfaces') raise web.seeother('/cn') self.logger.info('stopping daemons before updating network interfaces') stop_daemons() try: with open('/etc/network/interfaces','w') as outfile: outfile.writelines(outdata) # Do not let dhcpcd get a dhcp address (and dns info?) if we are using a static config if 'Use DHCP' in form: shutil.copy2('/etc/dhcpcd.conf.yeswlan0', '/etc/dhcpcd.conf') else: shutil.copy2('/etc/dhcpcd.conf.nowlan0', '/etc/dhcpcd.conf') # if we are going to broadcast our own ssid to extend reach, save ssid and password broad_wpa_info = wpa_passphrase(form['Extender SSID'], form['Extender Password']) if len(broad_wpa_info['ssid']) > 2 and broad_wpa_info['ssid'][0] == '"': # strip off quotes for hostapd.conf broad_wpa_info['ssid'] = broad_wpa_info['ssid'][1:len(broad_wpa_info['ssid'])-1] gv.sd['extender_ssid'] = broad_wpa_info['ssid'] gv.sd['extender_psk'] = broad_wpa_info['psk'] if not gv.sd['remote_radio_substation'] and not use_eth0: wpa_supp_lines = ['ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev\n'] wpa_supp_lines.append('update_config=1\n\n') if form['SSID'] != '' and form['Password'] != '': wpa_info = wpa_passphrase(form['SSID'], form['Password']) wpa_supp_lines.append('network={\n') wpa_supp_lines.append('\tssid=' + wpa_info['ssid'] + '\n') if using_hidden_ssid: wpa_supp_lines.append('\tscan_ssid=1\n') wpa_supp_lines.append('\tpsk=' + wpa_info['psk'] + '\n') wpa_supp_lines.append('}\n') else: self.logger.warning('missing ssid or password') with open('/etc/wpa_supplicant/wpa_supplicant.conf', 'w') as outfile: outfile.writelines(wpa_supp_lines) # check if network connection was made # self.logger.debug('wpa_action stopping wlan0') # rc = subprocess.call(['wpa_action', 'wlan0', 'stop']) # self.logger.debug( 'wpa_action wlan0 stop return: ' + str(rc)) # time.sleep(1) # self.logger.debug('ifup wlan0') # rc = subprocess.call(['ifup', 'wlan0']) # self.logger.debug( 'ifup return: ' + str(rc)) # time.sleep(1) # rc = subprocess.call(['wpa_action', 'wlan0', 'reload']) # self.logger.debug( 'wpa_action wlan0 reload return: ' + str(rc)) # time.sleep(1) # reset_networking() if True or 'Do not validate network connection' in form or network_up(net, self.logger): #successful network connection. Finalize # copy the current versions to the save version cur_ip = get_ip(net) self.logger.info('success...cur_ip: ' + cur_ip) if gv.sd['enable_upnp']: # was enabled? Then cleanup. If network still points to 10.0.0.1 network cant cleanup! deletes = [] if gv.sd['external_htp'] != 0: deletes.append(gv.sd['external_htp']) if gv.sd['remote_support_port'] != 0: deletes.append(gv.sd['remote_support_port']) if gv.sd['external_proxy_port'] != 0: deletes.append(gv.sd['external_proxy_port']) update_upnp(cur_ip, self.logger, deletes) gv.sd['enable_upnp'] = 1 if 'Enable UPnP' in form else 0 if gv.sd['enable_upnp']: if 'UPnP Refresh Rate' in form: gv.sd['upnp_refresh_rate'] = int(form['UPnP Refresh Rate']) if 'Station Name' in form: gv.sd['name'] = form['Station Name'] update_hostname(gv.sd['name']) if 'Station Port' in form: gv.sd['htp'] = int(form['Station Port']) if gv.sd['master']: gv.sd['master_ip'] = 'localhost' # update local master_port gv.sd['master_port'] == gv.sd['htp'] else: gv.sd['master_port'] = 0 gv.sd['external_htp'] = int(form['External Station Port']) if 'External Station Port' in form else 0 gv.sd['external_proxy_port'] = int(form['Remote Substation Access Port']) if 'Remote Substation Access Port' in form else 0 jsave(gv.sd, 'sd') self.logger.info('success....copying back interfaces and wpa') try: os.remove('/etc/resolv.conf') except: pass shutil.copy('/etc/network/interfaces', '/etc/network/interfaces.save') shutil.copy('/etc/wpa_supplicant/wpa_supplicant.conf', '/etc/wpa_supplicant/wpa_supplicant.conf.save') # self.logger.info('success....enabling sip') # subprocess.call(['systemctl', 'enable', 'sip.service']) ## subprocess.call(['update-rc.d', 'sip', 'enable']) ## subprocess.call(['update-rc.d', 'sip', 'defaults']) ## self.logger.info('disabling sip_net_finish') # subprocess.call(['systemctl', 'disable', 'sip_net_finish.service']) ## subprocess.call(['update-rc.d', '-f', 'sip_net_finish', 'remove']) # light_ip(cur_ip) self.logger.info('rebooting') subprocess.call(['reboot', '-h']) exit(0) else: raise Exception('Network Inaccessible') except Exception as ex: self.logger.exception('failed: ' + str(ex)) # restore saved /etc/network/interfaces and wpa_suplicant.conf self.logger.info('restore network files and exit. Exception: ' + str(ex)) shutil.move('/etc/network/interfaces.save', '/etc/network/interfaces') shutil.move('/etc/wpa_supplicant/wpa_supplicant.conf.save', '/etc/wpa_supplicant/wpa_supplicant.conf') exit(1) self.logger.error('sip_net_config: should have exited above. sip_monitor will restart') raise web.seeother('/cn')
def POST(self): global error_msg if not been_through_subconfig: self.logger.info('tried Net POST....back to /su') raise web.seeother('/su') self.logger.debug('in Net POST') save_last_get('Net POST') form = web.input() error_msg = '' outdata = [] for f in [ 'Static IP', 'Netmask', 'Gateway', 'DNS Nameservers', 'SSID', 'Password', 'Hidden SSID', 'UPnP Refresh Rate', 'Station Name', 'Station Port', 'External Station Port', 'Remote Substation Access Port' ]: if f in form: form[f] = form[f].strip() # use hidden SSID if provided using_hidden_ssid = False if 'Hidden SSID' in form and form['Hidden SSID'] != '': using_hidden_ssid = True form['SSID'] = form['Hidden SSID'] self.logger.info('using Hidden SSID') use_eth0 = 'SSID' not in form or form['SSID'] == 'ethernet' if not use_eth0: net = 'wlan0' else: form['SSID'] = 'using eth0' net = 'eth0' self.logger.debug('in Net POST: ' + form['SSID']) gv.sd['light_ip'] = 1 try: if form['DNS Nameservers'][0:1] == 'X' and \ (len(form['DNS Nameservers']) == 1 or form['DNS Nameservers'][1:2] == ' '): gv.sd[ 'light_ip'] = 0 # hack to disable blinking led on startup with leading X in nameservers form['DNS Nameservers'] = form['DNS Nameservers'][1:].strip() except: pass if 'Use DHCP' in form: for d in ['DNS Search', 'DNS Nameservers']: if d in form and form[d] != '': error_msg = 'Error: "' + d + '" must be blank with "Use DHCP".' raise web.seeother('/cn') # back to form if 'Station Name' in form: sn = validate_fqdn(form['Station Name']) if sn == 'Irricloud': # error condition error_msg = 'Error: "Station Name" must have only letter and numbers.' raise web.seeother('/cn') # back to form elif len(sn) > 50 or len(sn) < 1: error_msg = 'Error: "Station Name" must be at most 50 characters.' raise web.seeother('/cn') # back to form form['Station Name'] = sn try: drop = 0 with open('/etc/network/interfaces', 'r') as infile: iface = infile.readlines() for line in iface: if 'dns-nameservers' in line: continue # delete any old dns stuff did_iface_wlan0 = False if drop > 0: drop -= 1 continue if 'iface ' + net + ' inet' in line: if net == 'wlan0': did_iface_wlan0 = True if 'Use DHCP' in form: self.logger.info('Using dhcp: ' + form['SSID']) if 'static' in line: drop = 3 line = 'iface ' + net + ' inet manual\n' else: self.logger.info('Using static ip: ' + form['Static IP'] + ' ' + form['Netmask'] + ' ' + form['Gateway']) if 'static' in line: drop = 3 outdata.append('iface ' + net + ' inet static\n') outdata.append(' address ' + form['Static IP'] + '\n') outdata.append(' netmask ' + form['Netmask'] + '\n') line = ' gateway ' + form['Gateway'] + '\n' outdata.append(line) if 'DNS Nameservers' in form and form[ 'DNS Nameservers'] != '': line = ' dns-nameservers ' + form[ 'DNS Nameservers'] + '\n' else: # use gateway line = ' dns-nameservers ' + form[ 'Gateway'] + '\n' elif 'iface ' + 'wlan0' + ' inet' in line: # using eth0 but need to clean up 10.0.0.1 entry if 'static' in line: drop = 3 line = 'iface ' + 'wlan0' + ' inet manual\n' did_iface_wlan0 = True outdata.append(line) if did_iface_wlan0: outdata.append( ' wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf\n' ) except Exception as ex: self.logger.exception( 'Unexpected exception trying to configure network: ' + str(ex)) raise web.seeother('/cn') if len(outdata) == 0: self.logger.error('Expected data in /etc/network/interfaces') raise web.seeother('/cn') for portn in [ 'Station Port', 'External Station Port', 'Remote Substation Access Port', 'UPnP Refresh Rate' ]: if portn in form: try: port = int(form[portn]) except: error_msg = 'Error: "' + portn + '" must be integer.' raise web.seeother('/cn') # back to form if port < 0 or port > 65535: error_msg = 'Error: "' + portn + '" must be between 0 and 65535.' raise web.seeother('/cn') # back to form if portn == 'Station Port' and port == 9080: # specially reserved for proxy error_msg = 'Error: "' + portn + '" cannot be 9080.' raise web.seeother('/cn') # back to form self.logger.info('stopping daemons before updating network interfaces') if not use_eth0: stop_daemons() else: stop_daemons() try: with open('/etc/network/interfaces', 'w') as outfile: outfile.writelines(outdata) # Do not let dhcpcd get a dhcp address (and dns info?) if we are using a static config if 'Use DHCP' in form: shutil.copy2('/etc/dhcpcd.conf.yeswlan0', '/etc/dhcpcd.conf') else: shutil.copy2('/etc/dhcpcd.conf.nowlan0', '/etc/dhcpcd.conf') if not use_eth0: wpa_supp_lines = [ 'ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev\n' ] wpa_supp_lines.append('update_config=1\n\n') if form['SSID'] != '' and form['Password'] != '': # wpa_passphrase cannot handle blanks so quote strings ssid = form['SSID'].replace('"', '\"') passw = form['Password'].replace('"', '\"') cmd = '/usr/bin/wpa_passphrase "' + ssid + '" "' + passw + '" > passphrase' subprocess.call(cmd, shell=True) with open('passphrase', 'r') as f: pl = f.readlines() for line in pl: if '#psk=' in line: # drop cleartext password continue wpa_supp_lines.append(line) if using_hidden_ssid and 'ssid' in line: wpa_supp_lines.append('\tscan_ssid=1\n') subprocess.call(['rm', 'passphrase']) else: self.logger.warning('missing ssid or password') with open('/etc/wpa_supplicant/wpa_supplicant.conf', 'w') as outfile: outfile.writelines(wpa_supp_lines) # check if network connection was made # self.logger.debug('wpa_action stopping wlan0') # rc = subprocess.call(['wpa_action', 'wlan0', 'stop']) # self.logger.debug( 'wpa_action wlan0 stop return: ' + str(rc)) # time.sleep(1) # self.logger.debug('ifup wlan0') # rc = subprocess.call(['ifup', 'wlan0']) # self.logger.debug( 'ifup return: ' + str(rc)) # time.sleep(1) # rc = subprocess.call(['wpa_action', 'wlan0', 'reload']) # self.logger.debug( 'wpa_action wlan0 reload return: ' + str(rc)) # time.sleep(1) # reset_networking() if True or 'Do not validate network connection' in form or network_up( net, self.logger): #successful network connection. Finalize # copy the current versions to the save version cur_ip = get_ip(net) self.logger.info('success...cur_ip: ' + cur_ip) if gv.sd[ 'enable_upnp']: # was enabled? Then cleanup. If network still points to 10.0.0.1 network cant cleanup! deletes = [] if gv.sd['external_htp'] != 0: deletes.append(gv.sd['external_htp']) if gv.sd['remote_support_port'] != 0: deletes.append(gv.sd['remote_support_port']) if gv.sd['external_proxy_port'] != 0: deletes.append(gv.sd['external_proxy_port']) update_upnp(cur_ip, self.logger, deletes) gv.sd['enable_upnp'] = 1 if 'Enable UPnP' in form else 0 if gv.sd['enable_upnp']: if 'UPnP Refresh Rate' in form: gv.sd['upnp_refresh_rate'] = int( form['UPnP Refresh Rate']) if 'Station Name' in form: gv.sd['name'] = form['Station Name'] update_hostname(gv.sd['name']) if 'Station Port' in form: gv.sd['htp'] = int(form['Station Port']) if gv.sd['master']: gv.sd[ 'master_ip'] = 'localhost' # update local master_port gv.sd['master_port'] == gv.sd['htp'] else: gv.sd['master_port'] = 0 gv.sd['external_htp'] = int( form['External Station Port'] ) if 'External Station Port' in form else 0 gv.sd['external_proxy_port'] = int( form['Remote Substation Access Port'] ) if 'Remote Substation Access Port' in form else 0 jsave(gv.sd, 'sd') self.logger.info('success....copying back interfaces and wpa') try: os.remove('/etc/resolv.conf') except: pass shutil.copy('/etc/network/interfaces', '/etc/network/interfaces.save') shutil.copy('/etc/wpa_supplicant/wpa_supplicant.conf', '/etc/wpa_supplicant/wpa_supplicant.conf.save') # self.logger.info('success....enabling sip') # subprocess.call(['systemctl', 'enable', 'sip.service']) ## subprocess.call(['update-rc.d', 'sip', 'enable']) ## subprocess.call(['update-rc.d', 'sip', 'defaults']) ## self.logger.info('disabling sip_net_finish') # subprocess.call(['systemctl', 'disable', 'sip_net_finish.service']) ## subprocess.call(['update-rc.d', '-f', 'sip_net_finish', 'remove']) # light_ip(cur_ip) self.logger.info('rebooting') subprocess.call(['reboot', '-h']) exit(0) else: raise Exception('Network Inaccessible') except Exception as ex: self.logger.exception('failed: ' + str(ex)) # restore saved /etc/network/interfaces and wpa_suplicant.conf self.logger.info('restore network files and exit. Exception: ' + str(ex)) shutil.move('/etc/network/interfaces.save', '/etc/network/interfaces') shutil.move('/etc/wpa_supplicant/wpa_supplicant.conf.save', '/etc/wpa_supplicant/wpa_supplicant.conf') exit(1) self.logger.error( 'sip_net_config: should have exited above. sip_monitor will restart' ) raise web.seeother('/cn')
# kill any hostapd or dnsmasq processes hanging around if not config_mode: for cmd in ['hostapd', 'dnsmasq']: try: cmd_pid = program_pid(cmd) if cmd_pid != 0: logger.critical('found ' + cmd + ' running....kill it: ' + str(cmd_pid)) subprocess.call(['/bin/kill', '-9', str(cmd_pid)], stderr=subprocess.STDOUT) except: pass if not network_up('wlan0') and not network_up('eth0'): if reset_skip == 0: logger.warning('sip_monitor: resetting network') network_up('wlan0', logger) # save network info when resetting reset_skip = 10 # throw in some delay reset_networking(logger) elif reset_skip > 0: reset_skip -= 1 else: reset_skip = 10 # throw in some delay except Exception as ex: logger.exception('in except' + str(ex)) time.sleep(5)
def POST(self): self.logger.debug('in Net POST') save_last_get('Net POST') form = web.input() outdata = [] net = 'eth0' if 'Use Hardwired eth0' in form else 'wlan0' # use hidden SSID if provided using_hidden_ssid = False if 'Hidden SSID' in form and form['Hidden SSID'].strip() != '': using_hidden_ssid = True form['SSID'] = form['Hidden SSID'] self.logger.info('using Hidden SSID') use_eth0 = 'SSID' not in form if not use_eth0: form['SSID'] = form['SSID'].strip() form['Password'] = form['Password'].strip() else: form['SSID'] = 'using eth0' self.logger.debug('in Net POST: ' + form['SSID']) try: drop = 0 with open('/etc/network/interfaces','r') as infile: iface = infile.readlines() for line in iface: did_iface_wlan0 = False if drop > 0: drop -= 1 continue if 'iface ' + net + ' inet' in line: if net == 'wlan0': did_iface_wlan0 = True if 'Use DHCP' in form: self.logger.debug('Using dhcp: ' + form['SSID']) if 'static' in line: drop = 3 line = 'iface ' + net + ' inet manual\n' else: self.logger.debug('Using static ip: ' + form['Static IP'] + ' ' + form['Netmask'] + ' ' + form['Gateway']) if 'static' in line: drop = 3 outdata.append('iface ' + net + ' inet static\n') outdata.append(' address ' + form['Static IP'] + '\n') outdata.append(' netmask ' + form['Netmask'] + '\n') line = ' gateway ' + form['Gateway'] +'\n' outdata.append(line) if did_iface_wlan0: outdata.append(' wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf\n') except Exception as ex: self.logger.exception('Unexpected exception trying to configure network: ' + str(ex)) raise web.seeother('/cn') if len(outdata) == 0: self.logger.error('Expected data in /etc/network/interfaces') raise web.seeother('/cn') self.logger.debug('stopping daemons before updating network interfaces') # if not use_eth0: # stop_daemons() try: with open('/etc/network/interfaces','w') as outfile: outfile.writelines(outdata) if not use_eth0: wpa_supp_lines = ['ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev\n'] wpa_supp_lines.append('update_config=1\n\n') if form['SSID'] != '' and form['Password'] != '': # wpa_passphrase cannot handle blanks cmd = 'wpa_passphrase ' + form['SSID'] + ' ' + form['Password'] + ' > passphrase' subprocess.call(cmd, shell=True) with open('passphrase', 'r') as f: pl = f.readlines() for line in pl: wpa_supp_lines.append(line) if using_hidden_ssid and 'ssid' in line: wpa_supp_lines.append('\tscan_ssid=1\n') subprocess.call(['rm', 'passphrase']) else: self.logger.warning('missing ssid or password') with open('/etc/wpa_supplicant/wpa_supplicant.conf', 'w') as outfile: outfile.writelines(wpa_supp_lines) # check if network connection was made self.logger.debug('wpa_action stopping wlan0') rc = subprocess.call(['wpa_action', 'wlan0', 'stop']) self.logger.debug( 'wpa_action return: ' + str(rc)) time.sleep(1) self.logger.debug('ifup wlan0') rc = subprocess.call(['ifup', net]) self.logger.debug( 'ifup return: ' + str(rc)) time.sleep(2) if network_up(net): #successful network connection. Finalize # copy the current versions to the save version if gv.sd['enable_upnp']: # was enabled? Then cleanup cur_ip = get_ip(net) deletes = [] if gv.sd['external_htp'] != 0: deletes.append(gv.sd['external_htp']) if gv.sd['remote_support_port'] != 0: deletes.append(gv.sd['remote_support_port']) update_upnp(cur_ip, deletes) gv.sd['enable_upnp'] = 1 if 'Enable UPnP' in form else 0 if gv.sd['enable_upnp']: if 'UPnP Refresh Rate' in form: gv.sd['upnp_refresh_rate'] = int(form['UPnP Refresh Rate']) if 'System Name' in form: gv.sd['name'] = form['System Name'] jsave(gv.sd, 'sd') if 'System Port' in form: gv.sd['htp'] = int(form['System Port']) jsave(gv.sd, 'sd') if 'External System Port' in form: gv.sd['external_htp'] = int(form['External System Port']) self.logger.info('success....copying back interfaces and wpa') shutil.copy('/etc/network/interfaces', '/etc/network/interfaces.save') shutil.copy('/etc/wpa_supplicant/wpa_supplicant.conf', '/etc/wpa_supplicant/wpa_supplicant.conf.save') # self.logger.info('success....enabling boiler') # subprocess.call(['update-rc.d', 'boiler', 'defaults']) # self.logger.info('disabling boiler_net_finish') # subprocess.call(['update-rc.d', 'boiler_net_finish', 'remove']) self.logger.info('rebooting') subprocess.call(['reboot', '-h']) exit(0) else: raise Exception('Network Inaccessible') except Exception as ex: self.logger.exception('failed: ' + str(ex)) # restore saved /etc/network/interfaces and wpa_suplicant.conf self.logger.info('restore network files and exit. Exception: ' + str(ex)) shutil.move('/etc/network/interfaces.save', '/etc/network/interfaces') shutil.move('/etc/wpa_supplicant/wpa_supplicant.conf.save', '/etc/wpa_supplicant/wpa_supplicant.conf') exit(1) self.logger.error('boiler_net_config: should have exited above. boiler_monitor will restart') raise web.seeother('/')
elif found_net_conf != 0: logger.critical('unexpected boiler_net_finish running with boiler') if boiler_net_finish_skip == 0: logger.info('boiler_net_finish rebooting') subprocess.call(['reboot', '-h']) else: boiler_net_finish_skip -= 1 # logger.info('removing boiler_net_finish') # subprocess.call(['update-rc.d', 'boiler_net_finish', 'remove']) logger.info('stopping boiler_net_finish') subprocess.call("/etc/init.d/boiler_net_finish stop", shell=True, stderr=subprocess.STDOUT) logger.info('stopped boiler_net_finish') else: # make sure network is up as best as possible # logger.debug('sip running....check network, then do nothing') attempt_reset = not network_up('wlan0') and not network_up('eth0') if attempt_reset and reset_skip == 0: logger.warning('boiler_monitor: resetting network') reset_skip = 2 # throw in some delay try: logger.debug('down/up') subprocess.call(['wpa_action', 'wlan0', 'stop']) subprocess.call(['ifdown', 'eth0']) time.sleep(2) subprocess.call(['ifup', 'wlan0']) subprocess.call(['ifup', 'eth0']) except: logger.error('boiler_monitor: reset failed') pass elif reset_skip > 0: reset_skip -= 1
subprocess.call(['/etc/init.d/substation_proxy', 'start'], stderr=subprocess.STDOUT) except: pass # kill any hostapd or dnsmasq processes hanging around # if not config_mode: # for cmd in ['hostapd', 'dnsmasq']: # try: # cmd_pid = program_pid(cmd) # if cmd_pid != 0: # logger.critical('found ' + cmd + ' running....kill it: ' + str(cmd_pid)) # subprocess.call(['/bin/kill', '-9', str(cmd_pid)], stderr=subprocess.STDOUT) # except: # pass if not gv.sd['remote_radio_substation'] and not network_up('wlan0') and not network_up('eth0'): if reset_skip == 0: logger.warning('sip_monitor: resetting network') network_up('wlan0', logger) # save network info when resetting reset_skip = 10 # throw in some delay reset_networking(logger) elif reset_skip > 0: reset_skip -= 1 else: reset_skip = 10 # throw in some delay except Exception as ex: logger.exception('in except' + str(ex)) time.sleep(5)
def POST(self): self.logger.debug('in Net POST') save_last_get('Net POST') form = web.input() outdata = [] net = 'eth0' if 'Use Hardwired eth0' in form else 'wlan0' # use hidden SSID if provided using_hidden_ssid = False if 'Hidden SSID' in form and form['Hidden SSID'].strip() != '': using_hidden_ssid = True form['SSID'] = form['Hidden SSID'] self.logger.info('using Hidden SSID') use_eth0 = 'SSID' not in form if not use_eth0: form['SSID'] = form['SSID'].strip() form['Password'] = form['Password'].strip() else: form['SSID'] = 'using eth0' self.logger.debug('in Net POST: ' + form['SSID']) try: drop = 0 with open('/etc/network/interfaces', 'r') as infile: iface = infile.readlines() for line in iface: did_iface_wlan0 = False if drop > 0: drop -= 1 continue if 'iface ' + net + ' inet' in line: if net == 'wlan0': did_iface_wlan0 = True if 'Use DHCP' in form: self.logger.debug('Using dhcp: ' + form['SSID']) if 'static' in line: drop = 3 line = 'iface ' + net + ' inet manual\n' else: self.logger.debug('Using static ip: ' + form['Static IP'] + ' ' + form['Netmask'] + ' ' + form['Gateway']) if 'static' in line: drop = 3 outdata.append('iface ' + net + ' inet static\n') outdata.append(' address ' + form['Static IP'] + '\n') outdata.append(' netmask ' + form['Netmask'] + '\n') line = ' gateway ' + form['Gateway'] + '\n' outdata.append(line) if did_iface_wlan0: outdata.append( ' wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf\n' ) except Exception as ex: self.logger.exception( 'Unexpected exception trying to configure network: ' + str(ex)) raise web.seeother('/cn') if len(outdata) == 0: self.logger.error('Expected data in /etc/network/interfaces') raise web.seeother('/cn') self.logger.debug( 'stopping daemons before updating network interfaces') # if not use_eth0: # stop_daemons() try: with open('/etc/network/interfaces', 'w') as outfile: outfile.writelines(outdata) if not use_eth0: wpa_supp_lines = [ 'ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev\n' ] wpa_supp_lines.append('update_config=1\n\n') if form['SSID'] != '' and form['Password'] != '': # wpa_passphrase cannot handle blanks cmd = 'wpa_passphrase ' + form['SSID'] + ' ' + form[ 'Password'] + ' > passphrase' subprocess.call(cmd, shell=True) with open('passphrase', 'r') as f: pl = f.readlines() for line in pl: wpa_supp_lines.append(line) if using_hidden_ssid and 'ssid' in line: wpa_supp_lines.append('\tscan_ssid=1\n') subprocess.call(['rm', 'passphrase']) else: self.logger.warning('missing ssid or password') with open('/etc/wpa_supplicant/wpa_supplicant.conf', 'w') as outfile: outfile.writelines(wpa_supp_lines) # check if network connection was made self.logger.debug('wpa_action stopping wlan0') rc = subprocess.call(['wpa_action', 'wlan0', 'stop']) self.logger.debug('wpa_action return: ' + str(rc)) time.sleep(1) self.logger.debug('ifup wlan0') rc = subprocess.call(['ifup', net]) self.logger.debug('ifup return: ' + str(rc)) time.sleep(2) if network_up(net): #successful network connection. Finalize # copy the current versions to the save version if gv.sd['enable_upnp']: # was enabled? Then cleanup cur_ip = get_ip(net) deletes = [] if gv.sd['external_htp'] != 0: deletes.append(gv.sd['external_htp']) if gv.sd['remote_support_port'] != 0: deletes.append(gv.sd['remote_support_port']) update_upnp(cur_ip, deletes) gv.sd['enable_upnp'] = 1 if 'Enable UPnP' in form else 0 if gv.sd['enable_upnp']: if 'UPnP Refresh Rate' in form: gv.sd['upnp_refresh_rate'] = int( form['UPnP Refresh Rate']) if 'System Name' in form: gv.sd['name'] = form['System Name'] jsave(gv.sd, 'sd') if 'System Port' in form: gv.sd['htp'] = int(form['System Port']) jsave(gv.sd, 'sd') if 'External System Port' in form: gv.sd['external_htp'] = int(form['External System Port']) self.logger.info('success....copying back interfaces and wpa') shutil.copy('/etc/network/interfaces', '/etc/network/interfaces.save') shutil.copy('/etc/wpa_supplicant/wpa_supplicant.conf', '/etc/wpa_supplicant/wpa_supplicant.conf.save') # self.logger.info('success....enabling boiler') # subprocess.call(['update-rc.d', 'boiler', 'defaults']) # self.logger.info('disabling boiler_net_finish') # subprocess.call(['update-rc.d', 'boiler_net_finish', 'remove']) self.logger.info('rebooting') subprocess.call(['reboot', '-h']) exit(0) else: raise Exception('Network Inaccessible') except Exception as ex: self.logger.exception('failed: ' + str(ex)) # restore saved /etc/network/interfaces and wpa_suplicant.conf self.logger.info('restore network files and exit. Exception: ' + str(ex)) shutil.move('/etc/network/interfaces.save', '/etc/network/interfaces') shutil.move('/etc/wpa_supplicant/wpa_supplicant.conf.save', '/etc/wpa_supplicant/wpa_supplicant.conf') exit(1) self.logger.error( 'boiler_net_config: should have exited above. boiler_monitor will restart' ) raise web.seeother('/')