CNSA-266-Personal/network.py

51 lines
1.4 KiB
Python
Raw Normal View History

2024-04-24 07:28:56 -07:00
import socket
import time
2024-04-25 07:40:45 -07:00
import platform
import subprocess
2024-04-24 07:28:56 -07:00
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: ")
2024-04-25 07:40:45 -07:00
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)): ")
2024-04-24 07:28:56 -07:00
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")
2024-04-25 07:40:45 -07:00
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)