forked from crackallcode/Python-Course-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetwork_scanner.py
More file actions
38 lines (24 loc) · 888 Bytes
/
network_scanner.py
File metadata and controls
38 lines (24 loc) · 888 Bytes
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
import socket
import subprocess
from datetime import datetime
subprocess.call('clear', shell=True)
target = input("Enter the target IP address or hostname: ")
def port_scan(target):
try:
ip = socket.gethostbyname(target)
print("-" * 50)
print("Scanning target:", ip)
print("Time started:", datetime.now())
print("-" * 50)
for port in range(1, 65635):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(1) # Set a timeout for the connection attempt
result = sock.connect_ex((ip, port))
if result == 0:
print("Port {}: Open".format(port))
sock.close()
except socket.gaierror:
print("Hostname could not be resolved.")
except socket.error:
print("Could not connect to the server.")
port_scan(target)