Нет описания

networkdiscovery.py 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import nmap
  2. import psutil
  3. import socket
  4. import ipaddress
  5. import pandas as pd
  6. from datetime import datetime
  7. def get_ipv4_interfaces():
  8. ipv4_interfaces = []
  9. for name, addrs in psutil.net_if_addrs().items():
  10. for addr in addrs:
  11. if addr.family == socket.AF_INET:
  12. ipv4_interfaces.append((name, addr.address, addr.netmask))
  13. return ipv4_interfaces
  14. def network_discovery():
  15. ipv4_interfaces = get_ipv4_interfaces()
  16. if not ipv4_interfaces:
  17. print("No active IPv4 network interfaces found.")
  18. return
  19. nm = nmap.PortScanner()
  20. active_hosts = []
  21. for name, ip, netmask in ipv4_interfaces:
  22. network = ipaddress.IPv4Network((ip, netmask), strict=False)
  23. if str(network) == "127.0.0.0/8":
  24. continue
  25. print(f"Discovering hosts in IP range {network}...")
  26. nm.scan(hosts=str(network), arguments='-sn -T4 --open')
  27. for host in nm.all_hosts():
  28. active_hosts.append([host, datetime.now().strftime("%Y-%m-%d %H:%M:%S")])
  29. columns = ['IP', 'Discovery Timestamp']
  30. df = pd.DataFrame(active_hosts, columns=columns)
  31. df.to_csv('active_hosts.csv', index=False)
  32. if __name__ == "__main__":
  33. network_discovery()