Hi all,
This is my first post. Usually I can search out answers to my problems but today I'm having a bit of difficulty finding what I'm doing wrong populating a list of MAC addresses from a callback function, all enclosed within a class. The list is always empty on the very last line where I print it out. If I print the contents of the list from within the callback function each time it is raised, it looks correct. I'm having a feeling it has to do with scope or might need a lambda function but I'm foggy on some aspects there. I apologize for my numerous un-pythonic statements. I'm totally self taught and come from using mainly Visual Basic so I can be a bit of a sloppy coder at times. Here's my code: NOTE: This is normally executed on a Raspberry Pi.
This is my first post. Usually I can search out answers to my problems but today I'm having a bit of difficulty finding what I'm doing wrong populating a list of MAC addresses from a callback function, all enclosed within a class. The list is always empty on the very last line where I print it out. If I print the contents of the list from within the callback function each time it is raised, it looks correct. I'm having a feeling it has to do with scope or might need a lambda function but I'm foggy on some aspects there. I apologize for my numerous un-pythonic statements. I'm totally self taught and come from using mainly Visual Basic so I can be a bit of a sloppy coder at times. Here's my code: NOTE: This is normally executed on a Raspberry Pi.
import nmap, sys, socket, subprocess class IP_Roam(): def __init__(self, root_ip="", display_on=False): self.mac_list = [] self.display_on = display_on self.router_ip = root_ip self.end_ip = "" def callback_result(self, host, scan_result): if self.display_on: sys.stdout.write('\rScanning ' + str(host).ljust(15, ' ')) sys.stdout.flush() for ip in scan_result['scan']: try: mac = scan_result['scan'][u'' + str(ip)]['addresses'][u'mac'] if mac: myscan.mac_list.append(mac) #ipv4 = scan_result['scan'][u'' + str(ip)]['addresses'][u'ipv4'] except: # This happens when it finds itself pass def go(self): if self.router_ip == "": self.who_am_i() nma = nmap.PortScannerAsync() nma.scan(self.router_ip + "/24", arguments='-sP', callback=self.callback_result) while nma.still_scanning(): nma.wait(2) if self.display_on: for mac in self.mac_list: print(mac) def who_am_i(self, ipend=None): my_ips = subprocess.check_output(("hostname", "-I"), stderr=None).decode('utf-8').split(" ") nodes = my_ips[0].split(".") self.router_ip = str(nodes[0]) + "." + str(nodes[1]) + ".1.0" if ipend is not None: self.end_ip = str(nodes[0]) + "." + str(nodes[1]) + ".1." + str(ipend) if self.display_on: print("Using: " + self.router_ip + "-" + self.end_ip) else: if self.display_on: print("Using: " + self.router_ip) if __name__ == "__main__": myscan = IP_Roam(display_on=True) myscan.go() print(myscan.mac_list)