123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- import nmap
- import pandas as pd
- from datetime import datetime
- import os
-
- def detailed_scan():
- try:
- active_hosts_df = pd.read_csv('active_hosts.csv')
- except FileNotFoundError:
- print("File active_hosts.csv not found. Please run the network discovery script first.")
- return
-
- nm = nmap.PortScanner()
- scan_results = []
-
- start_time = datetime.now()
- print(f"Scan started at: {start_time.strftime('%Y-%m-%d %H:%M:%S')}")
-
- for index, row in active_hosts_df.iterrows():
- ip = row['IP']
- print(f"Scanning {ip} for detailed information...")
- nm.scan(ip, arguments='-p- -T5 ')
-
- for host in nm.all_hosts():
- hostnames = [x['name'] for x in nm[host].get('hostnames', [])]
- mac_address = nm[host]['addresses'].get('mac', 'N/A')
- scan_timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
-
- for proto in nm[host].all_protocols():
- for port in nm[host][proto]:
- state = nm[host][proto][port]['state']
- name = nm[host][proto][port]['name']
- product = nm[host][proto][port]['product']
- version = nm[host][proto][port]['version']
- extrainfo = nm[host][proto][port]['extrainfo']
- row = [ip, ', '.join(hostnames), mac_address, proto, port, name, state, product, version, extrainfo, scan_timestamp]
- scan_results.append(row)
-
- end_time = datetime.now()
- print(f"Scan finished at: {end_time.strftime('%Y-%m-%d %H:%M:%S')}")
- print(f"Total scan duration: {end_time - start_time}")
-
- columns = ['IP', 'Hostname', 'MAC Address', 'Protocol', 'Port', 'Name', 'State', 'Product', 'Version', 'Extra Info', 'Timestamp']
- detailed_df = pd.DataFrame(scan_results, columns=columns)
-
- if os.path.exists('detailed_scan_results.csv'):
- detailed_df.to_csv('detailed_scan_results.csv', mode='a', header=False, index=False)
- else:
- detailed_df.to_csv('detailed_scan_results.csv', index=False)
-
- print(detailed_df)
-
- if __name__ == "__main__":
- detailed_scan()
|