You cannot read the ip as whole because it has several .
by the means, which is actually the error that appears in the image you placed.
The easiest is to even treat ip as an integer list. And for that you can use the function split
of string
which separates into several values by means of a separator. In your case it is important to use the .
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 the ip is not actually a number but a string
, however, it has no relevance to what it is doing which is to replace the last block dynamically. Now we’re at the point of building whatever:
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 function join
with ip_formatado = '.'.join(ip)
, which greatly simplifies. See an example of this part only working:
>>> ip = ['192', '168', '1', '1']
>>> ip
['192', '168', '1', '1']
>>> '.'.join(ip)
'192.168.1.1'
For those who voted to close, this question in its last incarnation is a question of how to use the [tag:Python] to do an action, so it is in the scope
– Jefferson Quesado