Bez popisu

portscanner.py 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import nmap
  2. import pandas as pd
  3. from datetime import datetime
  4. import os
  5. def detailed_scan():
  6. try:
  7. active_hosts_df = pd.read_csv('active_hosts.csv')
  8. except FileNotFoundError:
  9. print("File active_hosts.csv not found. Please run the network discovery script first.")
  10. return
  11. nm = nmap.PortScanner()
  12. scan_results = []
  13. start_time = datetime.now()
  14. print(f"Scan started at: {start_time.strftime('%Y-%m-%d %H:%M:%S')}")
  15. for index, row in active_hosts_df.iterrows():
  16. ip = row['IP']
  17. print(f"Scanning {ip} for detailed information...")
  18. nm.scan(ip, arguments='-p- -T5 ')
  19. for host in nm.all_hosts():
  20. hostnames = [x['name'] for x in nm[host].get('hostnames', [])]
  21. mac_address = nm[host]['addresses'].get('mac', 'N/A')
  22. scan_timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  23. for proto in nm[host].all_protocols():
  24. for port in nm[host][proto]:
  25. state = nm[host][proto][port]['state']
  26. name = nm[host][proto][port]['name']
  27. product = nm[host][proto][port]['product']
  28. version = nm[host][proto][port]['version']
  29. extrainfo = nm[host][proto][port]['extrainfo']
  30. row = [ip, ', '.join(hostnames), mac_address, proto, port, name, state, product, version, extrainfo, scan_timestamp]
  31. scan_results.append(row)
  32. end_time = datetime.now()
  33. print(f"Scan finished at: {end_time.strftime('%Y-%m-%d %H:%M:%S')}")
  34. print(f"Total scan duration: {end_time - start_time}")
  35. columns = ['IP', 'Hostname', 'MAC Address', 'Protocol', 'Port', 'Name', 'State', 'Product', 'Version', 'Extra Info', 'Timestamp']
  36. detailed_df = pd.DataFrame(scan_results, columns=columns)
  37. if os.path.exists('detailed_scan_results.csv'):
  38. detailed_df.to_csv('detailed_scan_results.csv', mode='a', header=False, index=False)
  39. else:
  40. detailed_df.to_csv('detailed_scan_results.csv', index=False)
  41. print(detailed_df)
  42. if __name__ == "__main__":
  43. detailed_scan()