def test_pass_network_interface(self, mock_popen): self.stdout_mock.write( b""" Interface: wlp4s0, datalink type: EN10MB (Ethernet) Starting arp-scan 1.9 with 256 hosts (http://www.nta-monitor.com/tools/arp-scan/) 10.192.202.1\t54:a0:50:e4:89:c0 10.192.202.4\tb8:27:eb:05:5d:d1 """ ) self.stdout_mock.seek(0) mock_popen.return_value.stdout = self.stdout_mock do_arp_scan("networkiface") mock_popen.assert_called_with( ["arp-scan", "--interface", "networkiface", "--localnet", "-q"], stdout=-1 )
def scan_network(): app.logger.info("Scanning network for printers...") for line in do_arp_scan(settings.get_val("network_interface")): (ip, _) = line hostname = get_avahi_hostname(ip) # it's communicating, let's sniff it for a printer sniff_printer.delay(hostname, ip)
def discover_printers(): if not settings.get_val("network_discovery"): return app.logger.debug("Discovering network printers...") now = datetime.utcnow() to_deactivate = printers.get_printers() to_skip_ip = [ device["ip"] for device in network_devices.get_network_devices() if (device["retry_after"] and device["retry_after"] > now) or device["disabled"] ] for line in do_arp_scan(settings.get_val("network_interface")): (ip, _) = line to_deactivate = [ printer for printer in to_deactivate if printer["ip"] != ip ] if ip in to_skip_ip: continue hostname = get_avahi_hostname(ip) # it's communicating, let's sniff it for a printer sniff_printer.delay(hostname, ip) for printer in to_deactivate: app.logger.debug( "%s (%s) was not encountered on the network, deactivating" % (printer["hostname"], printer["ip"])) printer["client_props"]["connected"] = False printers.update_printer(**printer)
def scan_network(org_uuid, network_interface="wlan0"): app.logger.info("Scanning network %s for printers..." % network_interface) for line in do_arp_scan(network_interface): (ip, _) = line hostname = get_avahi_hostname(ip) # it's communicating, let's sniff it for a printer sniff_printer.delay(org_uuid, hostname, ip)
def test_parse_lines(self, mock_popen): self.stdout_mock.write( b""" Interface: wlp4s0, datalink type: EN10MB (Ethernet) Starting arp-scan 1.9 with 256 hosts (http://www.nta-monitor.com/tools/arp-scan/) 10.192.202.1\t54:a0:50:e4:89:c0 10.192.202.4\tb8:27:eb:05:5d:d1 """ ) self.stdout_mock.seek(0) mock_popen.return_value.stdout = self.stdout_mock self.assertEqual(len(do_arp_scan("wlp4s0")), 2)
def test_err(self, mock_popen, mock_logger): self.stderr_mock.write( dedent(""" ioctl: No such device """).encode("utf-8")) self.stdout_mock.seek(0) mock_popen.return_value.stdout = self.stdout_mock self.stderr_mock.seek(0) mock_popen.return_value.stderr = self.stderr_mock results = do_arp_scan("networkiface") self.assertEqual(len(results), 0) self.assertTrue(mock_logger.call_count, 1) mock_logger.called_with("arp-scan error: ioctl: No such device")
def test_regex(self, mock_popen): self.stdout_mock.write( b""" Interface: wlp4s0, datalink type: EN10MB (Ethernet) Starting arp-scan 1.9 with 256 hosts (http://www.nta-monitor.com/tools/arp-scan/) 10.192.202.1\t54:a0:50:e4:89:c0 10.192.202.4\tb8:27:eb:05:5d:d1 17 packets received by filter, 0 packets dropped by kernel Ending arp-scan 1.9: 256 hosts scanned in 2.443 seconds (104.79 hosts/sec). 17 responded """ ) self.stdout_mock.seek(0) mock_popen.return_value.stdout = self.stdout_mock results = do_arp_scan("networkiface") self.assertEqual(len(results), 2) self.assertEqual(results[0], ("10.192.202.1", "54:a0:50:e4:89:c0")) self.assertEqual(results[1], ("10.192.202.4", "b8:27:eb:05:5d:d1"))