def killapservices(): writedatedlogmsg(networklog, 'Killing AP Services. ', 1, networkloglevel) try: subprocess.call(['service', 'hostapd', 'stop']) except: writedatedlogmsg(networklog, 'Error killing hostapd. ', 0, networkloglevel) else: writedatedlogmsg(networklog, 'Killed hostapd. ', 3, networkloglevel) try: subprocess.call(['service', 'isc-dhcp-server', 'stop']) except: writedatedlogmsg(networklog, 'Error killing dhcp server. ', 0, networkloglevel) else: writedatedlogmsg(networklog, 'Successfully killed dhcp server. ', 3, networkloglevel)
def resetwlan(): from time import sleep writedatedlogmsg(networklog, 'Resetting wlan. ', 3, networkloglevel) try: subprocess.call(['ifdown', '--force', 'wlan0']) except: writedatedlogmsg(networklog, 'Error bringing down wlan0. ', 0, networkloglevel) else: writedatedlogmsg(networklog, 'Completed bringing down wlan0 ', 3, networkloglevel) sleep(2) try: subprocess.call(['ifup', 'wlan0']) except: writedatedlogmsg(networklog, 'Error bringing up wlan0. ', 0, networkloglevel) else: writedatedlogmsg(networklog, 'Completed bringing up wlan0 ', 3, networkloglevel)
def replaceifaceparameters(iffilein, iffileout, iface, parameternames, parametervalues): file = open(iffilein) lines = file.readlines() writestring = '' ifacename = None for line in lines: if line.find('iface') >= 0 > line.find('default'): # we are at an iface stanza beginning foundname = line[6:11].strip() ifacename = foundname if ifacename == iface: # do our replace for parametername, parametervalue in zip(parameternames, parametervalues): if line.find(parametername) > 0: split = line.split(' ') # We find where the parameter is, then assume the value is the # next position. We then trim everything past the parameter # This safeguards against whitespace at the end of lines creating problems. index = split.index(parametername) split[index + 1] = str(parametervalue) + '\n' line = ' '.join(split[0:index + 2]) writestring += line try: myfile = open(iffileout, 'w') except: writedatedlogmsg(networklog, 'Error opening interface file. ', 0, networkloglevel) else: writedatedlogmsg(networklog, 'Interface file read successfully. ', 3, networkloglevel) try: myfile.write(writestring) except: writedatedlogmsg(networklog, 'Error writing interface file. ', 0, networkloglevel) else: writedatedlogmsg(networklog, 'Interface file written successfully. ', 3, networkloglevel) writedatedlogmsg(networklog, 'Write string: ' + writestring, 3, networkloglevel)
def runping(pingAddress, numpings=1): pingtimes = [] from cupid import pilib for i in range(numpings): # Perform the ping using the system ping command (one ping only) try: rawPingFile = os.popen('ping -c 1 %s' % (pingAddress)) except: failed = True latency = 0 else: # Extract the ping time rawPingData = rawPingFile.readlines() rawPingFile.close() if len(rawPingData) < 2: # Failed to find a DNS resolution or route failed = True latency = 0 else: index = rawPingData[1].find('time=') if index == -1: # Ping failed or timed-out failed = True latency = 0 else: # We have a ping time, isolate it and convert to a number failed = False latency = rawPingData[1][index + 5:] latency = latency[:latency.find(' ')] latency = float(latency) # Set our outputs if failed: # Could not ping pingtimes.append(0) else: # Ping stored in latency in milliseconds #print '%f ms' % (latency) pingtimes.append(latency) pilib.writedatedlogmsg(pilib.networklog, 'ping times: ' + str(pingtimes), 3, pilib.networkloglevel) return pingtimes
def getwpasupplicantconfig(conffile='/etc/wpa_supplicant/wpa_supplicant.conf'): class data(): pass file = open(conffile) lines = file.readlines() header = '' tail = '' datalines = [] readheader = True readbody = False readtail = False for line in lines: if readheader: header = header + line if '}' in line: writedatedlogmsg(networklog, 'Ending supplicant parse. ', 5, networkloglevel) readbody = False readtail = True if readbody: datalines.append(line) if readtail: tail = tail + line if '{' in line: writedatedlogmsg(networklog, 'Beginning supplicant parse. ', 5, networkloglevel) readheader = False readbody = True datadict = {} for line in datalines: split = line.split('=') datadict[split[0].strip()] = split[1].strip() returndict = data() returndict.header = header returndict.tail = tail returndict.data = datadict return returndict
def startapservices(): from time import sleep try: subprocess.call(['hostapd', '-B', '/etc/hostapd/hostapd.conf']) except: writedatedlogmsg(networklog, 'Error starting hostapd. ', 0, networkloglevel) else: writedatedlogmsg(networklog, 'Started hostapd without error. ', 3, networkloglevel) sleep(1) try: subprocess.call(['service', 'isc-dhcp-server', 'start']) except: writedatedlogmsg(networklog, 'Error starting dhcp server. ', 0, networkloglevel) else: writedatedlogmsg(networklog, 'Started dhcp server without error. ', 3, networkloglevel)
def setapmode(netconfig=None): writedatedlogmsg(networklog, 'Setting ap mode. ', 1, networkloglevel) try: subprocess.call(['cp', '/etc/network/interfaces.ap', '/etc/network/interfaces']) except: writedatedlogmsg(networklog, 'Error copying network configuration file. ', 0, networkloglevel) else: writedatedlogmsg(networklog, 'Copied network configuration file successfully. ', 3, networkloglevel) killapservices() resetwlan() startapservices()
def writesupplicantfile(filedata, filepath='/etc/wpa_supplicant/wpa_supplicant.conf'): writestring = '' writestring += filedata.header for key, value in filedata.data.items(): writestring += key + '=' + value + '\n' writestring += filedata.tail writedatedlogmsg(networklog, 'Writing supplicant file. ', 1, networkloglevel) try: myfile = open(filepath, 'w') myfile.write(writestring) except: writedatedlogmsg(networklog, 'Error writing supplicant file. ', 1, networkloglevel) else: writedatedlogmsg(networklog, 'Supplicant file written. ', 3, networkloglevel)
def setapmode(netconfig=None): writedatedlogmsg(networklog, 'Setting ap mode. ', 1, networkloglevel) try: subprocess.call( ['cp', '/etc/network/interfaces.ap', '/etc/network/interfaces']) except: writedatedlogmsg(networklog, 'Error copying network configuration file. ', 0, networkloglevel) else: writedatedlogmsg(networklog, 'Copied network configuration file successfully. ', 3, networkloglevel) killapservices() resetwlan() startapservices()
def getwpaclientstatus(): import subprocess try: writedatedlogmsg(networklog, 'Attempting WPA client status read. ', 4, networkloglevel) result = subprocess.Popen(['/sbin/wpa_cli', 'status'], stdout=subprocess.PIPE) except: writedatedlogmsg(networklog, 'Error reading wpa client status. ', 0, networkloglevel) else: writedatedlogmsg(networklog, 'Completed WPA client status read. ', 4, networkloglevel) # prune interface ID resultdict = {} for result in result.stdout: if result.find('=') > 0: split = result.split('=') resultdict[split[0]] = split[1].strip() return resultdict
def runconfig(onboot=False): import subprocess try: netconfigdata = readonedbrow(systemdatadatabase, 'netconfig')[0] except: writedatedlogmsg(networklog, 'Error reading netconfig data. ', 0, networkloglevel) else: writedatedlogmsg(networklog, 'Successfully read netconfig data', 3, networkloglevel) if netconfigdata['enabled']: writedatedlogmsg(networklog, 'Netconfig is enabled', 3, networkloglevel) # This will grab the specified SSID and the credentials and update # the wpa_supplicant file updatewpasupplicant() # Copy the correct interfaces file if netconfigdata['mode'] == 'station': setstationmode(netconfigdata) elif netconfigdata['mode'] in ['ap', 'tempap']: setapmode() # Unfortunately, we currently need to reboot prior to setting # ap mode to get it to stick unless we are doing it at bootup if not onboot: writedatedlogmsg(networklog, 'Rebooting after set ap mode', 0, networkloglevel) writedatedlogmsg(systemstatuslog, 'Rebooting after set ap mode ', 0, systemstatusloglevel) # subprocess.call(['reboot']) else: writedatedlogmsg(networklog, 'Netconfig is disabled', 3, networkloglevel)
def updatewpasupplicant(): try: suppdata = getwpasupplicantconfig() except: writedatedlogmsg(networklog, 'Error getting supplicant data. ', 0, networkloglevel) else: writedatedlogmsg(networklog, 'Supplicant data retrieved successfully. ', 3, networkloglevel) try: updateddata = updatesupplicantdata(suppdata) except: writedatedlogmsg(networklog, 'Error updating supplicant data. ', 0, networkloglevel) else: writedatedlogmsg(networklog, 'Supplicant data retrieved successfully. ', 3, networkloglevel) try: writesupplicantfile(updateddata) except: writedatedlogmsg(networklog, 'Error writing supplicant data. ', 0, networkloglevel) else: writedatedlogmsg(networklog, 'Supplicant data written successfully. ', 3, networkloglevel)
def setstationmode(netconfigdata=None): writedatedlogmsg(networklog, 'Setting station mode. ', 3, networkloglevel) from time import sleep if not netconfigdata: writedatedlogmsg(networklog, 'Retrieving unfound netconfig data. ', 3, networkloglevel) try: netconfigdata = readonedbrow(systemdatadatabase, 'netconfig')[0] except: writedatedlogmsg(networklog, 'Error reading netconfig data. ', 0, networkloglevel) else: writedatedlogmsg(networklog, 'Read netconfig data. ', 4, networkloglevel) killapservices() if netconfigdata['addtype'] == 'static': writedatedlogmsg(networklog, 'Configuring static address. ', 3, networkloglevel) subprocess.call([ 'cp', '/usr/lib/iicontrollibs/misc/interfaces/interfaces.sta.static', '/etc/network/interfaces' ]) # update IP from netconfig writedatedlogmsg( networklog, 'Updating netconfig with ip ' + netconfigdata['address'], 3, networkloglevel) replaceifaceparameters( '/etc/network/interfaces', '/etc/network/interfaces', 'wlan0', ['address', 'gateway'], [netconfigdata['address'], netconfigdata['gateway']]) elif netconfigdata['addtype'] == 'dhcp': writedatedlogmsg(networklog, 'Configuring dhcp. ', 3, networkloglevel) subprocess.call([ 'cp', '/usr/lib/iicontrollibs/misc/interfaces/interfaces.sta.dhcp', '/etc/network/interfaces' ]) writedatedlogmsg(networklog, 'Resetting wlan. ', 3, networkloglevel) resetwlan() sleep(1) resetwlan()
def setstationmode(netconfigdata=None): writedatedlogmsg(networklog, 'Setting station mode. ', 3, networkloglevel) from time import sleep if not netconfigdata: writedatedlogmsg(networklog, 'Retrieving unfound netconfig data. ', 3, networkloglevel) try: netconfigdata = readonedbrow(systemdatadatabase, 'netconfig')[0] except: writedatedlogmsg(networklog, 'Error reading netconfig data. ', 0, networkloglevel) else: writedatedlogmsg(networklog, 'Read netconfig data. ', 4, networkloglevel) killapservices() if netconfigdata['addtype'] == 'static': writedatedlogmsg(networklog, 'Configuring static address. ', 3, networkloglevel) subprocess.call(['cp', '/usr/lib/iicontrollibs/misc/interfaces/interfaces.sta.static', '/etc/network/interfaces']) # update IP from netconfig writedatedlogmsg(networklog, 'Updating netconfig with ip ' + netconfigdata['address'], 3, networkloglevel) replaceifaceparameters('/etc/network/interfaces', '/etc/network/interfaces', 'wlan0', ['address', 'gateway'], [netconfigdata['address'], netconfigdata['gateway']]) elif netconfigdata['addtype'] == 'dhcp': writedatedlogmsg(networklog, 'Configuring dhcp. ', 3, networkloglevel) subprocess.call(['cp', '/usr/lib/iicontrollibs/misc/interfaces/interfaces.sta.dhcp', '/etc/network/interfaces']) writedatedlogmsg(networklog, 'Resetting wlan. ', 3, networkloglevel) resetwlan() sleep(1) resetwlan()
def updatesupplicantdata(configdata): from pilib import readalldbrows, safedatabase, systemdatadatabase try: netconfig = readalldbrows(systemdatadatabase, 'netconfig')[0] except: writedatedlogmsg(networklog, 'Error reading netconfig data. ', 0, networkloglevel) else: writedatedlogmsg(networklog, 'Read netconfig data. ', 4, networkloglevel) try: wirelessauths = readalldbrows(safedatabase, 'wireless') except: writedatedlogmsg(networklog, 'Error reading wireless data. ', 0, networkloglevel) else: writedatedlogmsg(networklog, 'Read wireless data. ', 4, networkloglevel) password = '' writedatedlogmsg(networklog, 'Netconfig data: ' + str(netconfig), 2, networkloglevel) # we only update if we find the credentials for auth in wirelessauths: if auth['SSID'] == netconfig['SSID']: password = '******' + auth['password'] + '"' ssid = '"' + auth['SSID'] + '"' writedatedlogmsg(networklog, 'SSID ' + auth['SSID'] + 'found. ', 1, networkloglevel) configdata.data['psk'] = password configdata.data['ssid'] = ssid return configdata