How to scan the IP of a user-selected network, and show what is being used and what is available, in order. PYTHON

Asked

Viewed 170 times

0

Hello, I’m trying to develop a basic program, to run both in linux and in WINDOWS, in which I can let the user choose the original ip and the end and show among all of the network which Ips are already being used by some machine and those that are available. to avoid errors wanted tbm the user to identify his operating system example:

print("Escolha seu sistema operacional")
print("(1) WINDOWS")
print("(2) LINUX")
print("(3) MAC")
print("(4) SAIR")

if == 1:
print("Digite o IP inicial")   exemplo digitado pelo usuario 192.168.254.1
i1= input()
print("Digite o IP Final")     exemplo digitado pelo usuario 192.168.254.254
i2 = input() 
nao sei como fazer isso para cada sistema
xxxxxxxxxxxxxx
xxxxxxxxxxx
xxxxxxxxx


if == 2:
print("Digite o IP inicial")   exemplo digitado pelo usuario 192.168.254.1
i1= input()
print("Digite o IP Final")     exemplo digitado pelo usuario 192.168.254.254
i2 = input() 
nao sei como fazer isso para cada sistema
xxxxxxxxxxxxxx
xxxxxxxxxxx
xxxxxxxxx

if == 3:
print("Digite o IP inicial")   exemplo digitado pelo usuario 192.168.254.1
i1= input()
print("Digite o IP Final")     exemplo digitado pelo usuario 192.168.254.254
i2 = input() 
nao sei como fazer isso para cada sistema
xxxxxxxxxxxxxx
xxxxxxxxxxx
xxxxxxxxx

1 answer

3


You should not worry about the operating system on which your program is running unless you have a reason very special!

Python is a language portable and the solution to your problem can also be.

Installing library ping3 via pip:

$ pip install ping3

The idea of this implementation is to send a package ICMP (alias ping) for each IP address within a range of Ips chosen by the user, this will allow us to test the connectivity of each of them, see only:

from ipaddress import IPv4Address
from ping3 import ping

inicial = IPv4Address(input("Digite o IP inicial: "))
final = IPv4Address(input("Digite o IP final: "))

ips = [str(IPv4Address(ip)) for ip in range(int(inicial), int(final))]

try:
    for ip in ips:
        t = ping(ip, timeout=5)
        status = 'OFFLINE' if t is None else 'ONLINE'
        print(f'IP: {ip} [{status}]')
except PermissionError:
    print('Usuario nao possui privilegios de Administrador!')

Testing:

Digite o IP inicial: 192.168.15.1
Digite o IP final: 192.168.15.10
IP: 192.168.15.1 [ONLINE]
IP: 192.168.15.2 [OFFLINE]
IP: 192.168.15.3 [ONLINE]
IP: 192.168.15.4 [ONLINE]
IP: 192.168.15.5 [ONLINE]
IP: 192.168.15.6 [OFFLINE]
IP: 192.168.15.7 [OFFLINE]
IP: 192.168.15.8 [OFFLINE]
IP: 192.168.15.9 [OFFLINE]

If operating system detection is essential in your case, you can do this automatically, without the need to question the user, using the attribute sys.platform, for example:

import sys

if sys.platform.startswith('linux'):
    print('Linux!')
elif sys.platform.startswith('win'):
    print('Windows!')
elif sys.platform.startswith('darwin'):
    print('Mac OS')
else:
    print('Sistema Desconhecido!')
  • friend, as I put on a graphical interface, showing the Ips offline on one side and the ONLINE on the other

Browser other questions tagged

You are not signed in. Login or sign up in order to post.