You can not read ip as integer because it has several .
in the middle, which is actually the error that appears in the image you placed.
The easiest thing is to treat ip as a list of integers. And for this you can use the split
function of string
that separates into multiple values using a separator. In your case it is important to use .
as a separator.
ip = input('IP INICIAL (192.168.0.1); ').split('.')
See how it's interpreted
>>> ip = input('IP INICIAL (192.168.0.1); ').split('.')
IP INICIAL (192.168.0.1); 192.168.1.1
>>> ip
['192', '168', '1', '1']
Each part of ip is not actually a number but a string
, however, it has no relevance to what you are doing that is to replace the last block dynamically. Now we are at the point of building the for you want:
import os
os.system('clear')
ip = input('IP INICIAL (192.168.0.1); ').split('.')
print ('Testing...')
for i in range(1,101): # faltou range aqui
ip[3] = str(i) # mudar a ultima parte do ip para o numero como string
ip_formatado = '.'.join(ip) # criar o texto que representa o ip
rs = os.system('ping -c 1 {}'.format(ip_formatado)) # executar o comando com o ip certo
if rs == 0:
print ('O {} ta on'.format(ip_formatado))
Note that you had the command with -c1
together when it was supposed to be separated by space.
I built the ip text back using the join
function with ip_formatado = '.'.join(ip)
, which greatly simplifies. Here's an example of this part just working:
>>> ip = ['192', '168', '1', '1']
>>> ip
['192', '168', '1', '1']
>>> '.'.join(ip)
'192.168.1.1'