CNSA-266-Personal/network.py
2024-04-25 10:40:45 -04:00

51 lines
1.4 KiB
Python

import socket
import time
import platform
import subprocess
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
print("Your IP address is: " + str(s.getsockname()[0]))
s.close()
import socket
openPorts = []
closedPorts = []
host = input("Please enter the IP address of the host you would like to scan: ")
start = input("Please enter the starting port you would like to scan: ")
end = input("Please enter the ending port you would like to scan: ")
timeout = input("Please enter the timeout in seconds you would like to set for each port scan (0.1 for external hosts "
"(WAN), 0.01 for internal hosts (LAN)): ")
for i in range(int(start), int(end)):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(float(timeout))
print("Scanning port " + str(i) + " on host " + host)
result = sock.connect_ex((host, i))
if result == 0:
openPorts.append(i)
else:
closedPorts.append(i)
# time.sleep(1)
sock.close()
for port in closedPorts:
print("Port " + str(port) + " is closed")
for port in openPorts:
print("Port " + str(port) + " is open")
print(str(len(closedPorts)) + " ports are closed; " + str(len(openPorts)) + " ports are open")
print("Starting Ping Monitor (x8): ")
time.sleep(2)
parameter = "-c"
command = ["ping", parameter, "8", host]
response = subprocess.call(command)
print(response == 0)