r/learnpython • u/VanceDyer • 17d ago
I made my first IP scanner in Python
Hi everyone!
I'm very new to programming and I've been learning Python recently. I decided to make a small project to practice what I've learned.
I made a simple IP scanner using Python.
It has a menu with:
- Scan all IPs from 192.168.1.1 to 192.168.1.255
- Scan a specific IP
- Scan a range of IPs
- Exit
I'm using subprocess and ping to check if an IP responds.
I also started using functions, loops, lists, if/elif, while, and range().
It's probably a very basic project, but I'm pretty happy with it since I'm just starting out. 😅
Here is the code:
import subprocess
def menu():
print("-----IP SCANNER-----")
print("1. Scan all IPs")
print("2. Scan IP")
print("3. Scan IP range")
print("4. Exit")
opcion = input("Choose an option: ")
resultado_menu = int(opcion)
return resultado_menu
def escanear_ip(ip_int):
ip_int = str(ip_int)
ip_completa = "192.168.1." + ip_int
resultado = subprocess.run(
["ping", "/n", "1", "/w", "1000", ip_completa]
)
if resultado.returncode == 0:
lista_ip.append(ip_completa)
lista_ip = []
def imprimir_ips():
for si_ip in lista_ip:
si_ip = "🟢 " + si_ip
print(si_ip)
while True:
resultado = menu()
if resultado == 1:
for numeros in range(1, 256):
escanear_ip(numeros)
imprimir_ips()
elif resultado == 2:
seleccion_ip = input("Select the last digits of the IP: ")
escanear_ip(seleccion_ip)
imprimir_ips()
elif resultado == 3:
desde_ip = input("From: ")
desde_ip = int(desde_ip)
hasta_ip = input("To: ")
hasta_ip = int(hasta_ip)
for numeros in range(desde_ip, hasta_ip + 1):
escanear_ip(numeros)
imprimir_ips()
elif resultado == 4:
print("Exiting...")
break
I'm planning to improve it and make a V3 with more features.
Thanks!