def sniffer_on(self, channel=None): ''' Turns the sniffer on such that pnext() will start returning observed data. @type channel: Integer @param channel: Sets the channel, optional @rtype: None ''' self.capabilities.require(KBCapabilities.SNIFF) # Because the Sewio just toggles, we have to only hit the page # if we need to go from off to on state. self.__sync_status() if self.__stream_open == False: if channel != None: self.set_channel(channel) if not self.__make_rest_call('status.cgi?p=2', fetch=False): raise KBInterfaceError( "Error instructing sniffer to start capture.") #This makes sure the change actually happened self.__sync_status() if not self.__stream_open: raise KBInterfaceError("Sniffer did not turn on capture.")
def sniffer_off(self): ''' Turns the sniffer off. @rtype: None ''' # Because the Sewio just toggles, we have to only hit the page # if we need to go from on to off state. self.__sync_status() if self.__stream_open == True: if not self.__make_rest_call('status.cgi?p=2', fetch=False): raise KBInterfaceError( "Error instructing sniffer to stop capture.") #This makes sure the change actually happened self.__sync_status() if self.__stream_open: raise KBInterfaceError("Sniffer did not turn off capture.")
def __sniffer_status(self): ''' Because the firmware accepts only toggle commands for sniffer on/off, we need to check what state it's in before taking action. It's also useful to make sure our command worked. @rtype: Boolean ''' html = self.__make_rest_call('') # Yup, we're going to have to steal the status out of a JavaScript variable res = re.search(r'<!--#pindex-->([A-Z]+),', html) if res is None: raise KBInterfaceError( "Unable to parse the sniffer's current status.") # RUNNING means it's sniffing, STOPPED means it's not. return (res.group(1) == "RUNNING")
def __sniffer_channel(self): ''' Because the firmware accepts only toggle commands for sniffer on/off, we need to check what state it's in before taking action. It's also useful to make sure our command worked. @rtype: Boolean ''' html = self.__make_rest_call('') # Yup, we're going to have to steal the channel number out of a JavaScript variable # var values = removeSSItag('<!--#pindex-->RUNNING,00:1a:b6:00:0a:a4,10.10.10.2,0,High,0x0000,OFF,0,0').split(","); res = re.search(r'<!--#pindex-->[A-Z]+,[0-9a-f:]+,[0-9.]+,([0-9]+),', html) if res is None: raise KBInterfaceError( "Unable to parse the sniffer's current channel.") return int(res.group(1))
def __make_rest_call(self, path, fetch=True): ''' Wrapper to the sniffer's RESTful services. Reports URL/HTTP errors as KBInterfaceErrors. @rtype: If fetch==True, returns a String of the page. Otherwise, it returns True if an HTTP 200 code was received. ''' try: html = urllib2.urlopen("http://{0}/{1}".format(self.dev, path)) if fetch: return html.read() else: return (html.getcode() == 200) except Exception as e: raise KBInterfaceError( "Unable to preform a call to {0}/{1} (error: {2}).".format( self.dev, path, e))
def getMacAddr(ip): ''' Returns a string for the MAC address of the sniffer. ''' try: html = urllib2.urlopen("http://{0}/".format(ip)) # Yup, we're going to have to steal the status out of a JavaScript variable #var values = removeSSItag('<!--#pindex-->STOPPED,00:1a:b6:00:0a:a4,... res = re.search( r'<!--#pindex-->[A-Z]+,((?:[0-9a-f]{2}:){5}[0-9a-f]{2})', html.read()) if res is None: raise KBInterfaceError( "Unable to parse the sniffer's MAC address.") return res.group(1) except Exception as e: print("Unable to connect to IP {0} (error: {1}).".format(ip, e)) return None