regex - readlines() error in python -
i'm new scapy , python, , have modelled below code on code example violent python: hacker's cookbook. after running program on wlan0 interface error attributeerror: readlines
my understanding readlines() converts capture list, findall() able operate on string line.
can help? cheers.
#!/usr/bin/python import optparse import logging import re # provides support regular expressions logging.getlogger("scapy.runtime").setlevel(logging.error) # suppresses messages have lower level of seriousness error messages scapy.all import * def findguest(capture): line in capture.splitlines(): ipaddr = re.findall("(?:\d{1,3}\.){3}\d{1,3}",line) # findall returns list # must use ,raw if ipaddr: print 'found ip addr '+ipaddr[0] def main(): parser = optparse.optionparser() # create parser object parser.add_option('-i', dest='interface', type='string', help='specify interface listen on') (opts, args) = parser.parse_args() # below if-else clause error handling.. if no interface given after -i print usage menu again # conf.iface standard interface used program, set whatever interface user gives if opts.interface == none: print parser.usage exit(0) else: conf.iface = opts.interface try: sniff(filter='tcp', prn=findguest, store=0) except keyboardinterrupt: exit(0) if __name__ == "__main__": main()
with following traceback:
lanix@lanix ~/python/wirelessmayhem $ sudo ./hotelguest2.py -i wlan0 ['__class__', '__contains__', '__delattr__', '__delitem__', '__dict__', '__div__', '__doc__', '__eq__', '__format__', '__getattr__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__len__', '__lt__', '__metaclass__', '__module__', '__mul__', '__ne__', '__new__', '__nonzero__', '__rdiv__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_do_summary', 'add_payload', 'add_underlayer', 'aliastypes', 'answers', 'build', 'build_done', 'build_padding', 'build_ps', 'canvas_dump', 'clone_with', 'command', 'copy', 'decode_payload_as', 'default_fields', 'default_payload_class', 'delfieldval', 'dispatch_hook', 'display', 'dissect', 'dissection_done', 'do_build', 'do_build_payload', 'do_build_ps', 'do_dissect', 'do_dissect_payload', 'do_init_fields', 'explicit', 'extract_padding', 'fields', 'fields_desc', 'fieldtype', 'firstlayer', 'fragment', 'from_hexcap', 'get_field', 'getfield_and_val', 'getfieldval', 'getlayer', 'guess_payload_class', 'hashret', 'haslayer', 'hide_defaults', 'init_fields', 'initialized', 'lastlayer', 'libnet', 'lower_bonds', 'mysummary', 'name', 'overload_fields', 'overloaded_fields', 'packetfields', 'payload', 'payload_guess', 'pdfdump', 'post_build', 'post_dissect', 'post_dissection', 'post_transforms', 'pre_dissect', 'psdump', 'remove_payload', 'remove_underlayer', 'route', 'self_build', 'sent_time', 'setfieldval', 'show', 'show2', 'show_indent', 'sprintf', 'summary', 'time', 'underlayer', 'upper_bonds'] traceback (most recent call last): file "./hotelguest2.py", line 41, in <module> main() file "./hotelguest2.py", line 35, in main sniff(filter='tcp', prn=findguest, store=0) file "/usr/lib/python2.7/dist-packages/scapy/sendrecv.py", line 586, in sniff r = prn(p) file "./hotelguest2.py", line 12, in findguest line in capture.splitlines(): file "/usr/lib/python2.7/dist-packages/scapy/packet.py", line 176, in __getattr__ fld,v = self.getfield_and_val(attr) file "/usr/lib/python2.7/dist-packages/scapy/packet.py", line 172, in getfield_and_val return self.payload.getfield_and_val(attr) file "/usr/lib/python2.7/dist-packages/scapy/packet.py", line 172, in getfield_and_val return self.payload.getfield_and_val(attr) file "/usr/lib/python2.7/dist-packages/scapy/packet.py", line 172, in getfield_and_val return self.payload.getfield_and_val(attr) file "/usr/lib/python2.7/dist-packages/scapy/packet.py", line 1057, in getfield_and_val raise attributeerror(attr) attributeerror: splitlines
traceback without using splitlines()
traceback (most recent call last): file "./hotelguest2.py", line 38, in <module> main() file "./hotelguest2.py", line 32, in main sniff(filter='tcp', prn=findguest, store=0) file "/usr/lib/python2.7/dist-packages/scapy/sendrecv.py", line 586, in sniff r = prn(p) file "./hotelguest2.py", line 11, in findguest ipaddr = re.findall("(?:\d{1,3}\.){3}\d{1,3}",capture) # findall returns list file "/usr/lib/python2.7/re.py", line 177, in findall return _compile(pattern, flags).findall(string) typeerror: expected string or buffer
readlines() not convert capture list capture string
.
it works on file object rather.use capture.split("\n")
instead or capture.splitlines()
Comments
Post a Comment