-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner.py
More file actions
62 lines (50 loc) · 1.86 KB
/
Copy pathscanner.py
File metadata and controls
62 lines (50 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import socket
import sys
import csv
from datetime import datetime
target_host = "scanme.nmap.org"
ports_to_scan = [21,22,23,25,53,80,110,139,443,445,3306,8080]
scan_results = []
print("-" * 60)
print(f"Scanning Target: {target_host}")
print(f"Time Started: {str(datetime.now())}")
print("-" * 60)
try:
for port in ports_to_scan:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(3)
result = s.connect_ex((target_host, port))
if result == 0:
banner = "Unknown"
try:
s.send(b"HEAD / HTTP/1.1\r\nHost: scan.nmap.org\r\n\r\n")
banner_data = s.recv(1024).decode('utf-8', errors='ignore').strip()
if banner_data:
banner = banner_data.split("\n")[0]
except Exception:
banner = "No banner response / timed out"
print(f"[+] Port {port:<5} : OPEN | Service: {banner}")
scan_results.append({"Port" : port, "Status": "OPEN", "Banner": banner})
else:
print(f"[-] Port {port:<5} : CLOSED")
scan_results.append({"Port" : port, "Status": "CLOSED", "Banner" : "N/A"})
s.close()
except KeyboardInterrupt:
print("\n[!] Scan interrupted by user.")
sys.exit()
except socket.gaierror:
print("\n[!] hostname could not be resolved.")
sys.exit()
except socket.error:
print("\n[!] Could not connect to server.")
sys.exit()
csv_filename = "scan_results.csv"
with open(csv_filename, mode='w', newline='', encoding='utf-8') as csv_file:
fieldnames = ["Port", "Status", "Banner"]
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(scan_results)
print("-" * 60)
print(f"Scan Completed: {str(datetime.now())}")
print(f"Results saved successfully to {csv_filename}")
print("-" * 60)