我不明白为什么我会得到那样的输出
I cant understand why i get output like that
我正在尝试编写一个脚本,向 2 个给定范围内的 ip 发送 ping,并尝试了解它们是否可用。我取得了一些进展,但有些事情我仍然无法解决。例如,我得到两次输出。如果你能帮助我,那将是完美的这是我的代码:
import subprocess
ipfirst=input("1st ip:")
iplast=input("2nd ip:")
currentip=""
ip_adresses_list=[]
"""this function may be my problem because i didnt understand the response,result structure if anyone can explain this i'd be appreciated"""
def ip_checker(ip):
response,result=subprocess.getstatusoutput("ping -c1 -w0.5 "+ip)
if (response==0):
print(ip,"ALIVE")
else:
print(ip,"NOT ALIVE")
return ip
""""splitting in order to increase"""
ipfirst=ipfirst.split(".")
ipfirst=list(map(int,ipfirst))
iplast=iplast.split(".")
iplast=list(map(int,iplast))
"""here while loop increases the ipfirst and appends to the list (i used just append at first but it didnt work , it only added the last ipn umber times all ip numbers like ([1.1.1.5],[1.1.1.5],[1.1.1.5],[1.1.1.5])) and my problem may be occuring because of that structure ".copy()" """
while (iplast>ipfirst):
ip_adresses_list.append(ipfirst.copy())
ipfirst[3] = ipfirst[3]+1
ip_adresses_list.append(ipfirst.copy())
if (ipfirst[3]>254):
ipfirst[3]=0
ipfirst[2]=ipfirst[2]+1
ip_adresses_list.append(ipfirst.copy())
if (ipfirst[2]>254):
ipfirst[2]=0
ipfirst[1]=ipfirst[1]+1
ip_adresses_list.append(ipfirst.copy())
if(ipfirst[1]>254):
ipfirst[1]=0
ipfirst[0]=ipfirst[0]+1
ip_adresses_list.append(ipfirst.copy())
"""i rearrange the list in order to get ip structure(num1.num2.num3.num4 like that) and i mixed it with ping function(ip_checker())"""
for i in ip_adresses_list:
ip_indice1=i[0]
ip_indice2=i[1]
ip_indice3=i[2]
ip_indice4=i[3]
currentip=str(str(ip_indice1)+"."+str(ip_indice2)+"."+str(ip_indice3)+"."+str(ip_indice4))
ip_checker(currentip)
如果我 运行 我得到并输出了这段代码,我无法理解为什么除了第一个之外每个 ip 都会 ping 两次
144.122.152.10 NOT ALIVE
144.122.152.11 ALIVE
144.122.152.11 ALIVE
144.122.152.12 ALIVE
144.122.152.12 ALIVE
144.122.152.13 ALIVE
144.122.152.13 ALIVE
问题出在附加到 ip_address_list 的 while 循环中,它附加添加 1 个附加项,循环然后再次附加相同的 ip,添加 1 个附加项,依此类推,这就是为什么你得到双打,只需将第一个追加移到循环外即可修复它。
另外你的 for 循环真的是多余的我在两行中做了同样的事情哈哈
import subprocess
ipfirst=input("1st ip:")
iplast=input("2nd ip:")
currentip="144.122.152.13"
ip_adresses_list=[]
"""this function may be my problem because i didnt understand the response,result structure if anyone can explain this i'd be appreciated"""
def ip_checker(ip):
response,result=subprocess.getstatusoutput("ping -c1 -w0.5 "+ip)
if (response==0):
print(ip,"ALIVE")
else:
print(ip,"NOT ALIVE")
return ip
""""splitting in order to increase"""
ipfirst=ipfirst.split(".")
ipfirst=list(map(int,ipfirst))
iplast=iplast.split(".")
iplast=list(map(int,iplast))
"""here while loop increases the ipfirst and appends to the list (i used just append at first but it didnt work , it only added the last ipn umber times all ip numbers like ([1.1.1.5],[1.1.1.5],[1.1.1.5],[1.1.1.5])) and my problem may be occuring because of that structure ".copy()" """
ip_adresses_list.append(ipfirst.copy())
while (iplast>ipfirst):
ipfirst[3] = ipfirst[3]+1
ip_adresses_list.append(ipfirst.copy())
if (ipfirst[3]>254):
ipfirst[3]=0
ipfirst[2]=ipfirst[2]+1
ip_adresses_list.append(ipfirst.copy())
if (ipfirst[2]>254):
ipfirst[2]=0
ipfirst[1]=ipfirst[1]+1
ip_adresses_list.append(ipfirst.copy())
if(ipfirst[1]>254):
ipfirst[1]=0
ipfirst[0]=ipfirst[0]+1
ip_adresses_list.append(ipfirst.copy())
"""i rearrange the list in order to get ip structure(num1.num2.num3.num4 like that) and i mixed it with ping function(ip_checker())"""
for ip_indice in ip_adresses_list:
currentip=str(str(ip_indice[0])+"."+str(ip_indice[1])+"."+str(ip_indice[2])+"."+str(ip_indice[3]))
ip_checker(currentip)
我正在尝试编写一个脚本,向 2 个给定范围内的 ip 发送 ping,并尝试了解它们是否可用。我取得了一些进展,但有些事情我仍然无法解决。例如,我得到两次输出。如果你能帮助我,那将是完美的这是我的代码:
import subprocess
ipfirst=input("1st ip:")
iplast=input("2nd ip:")
currentip=""
ip_adresses_list=[]
"""this function may be my problem because i didnt understand the response,result structure if anyone can explain this i'd be appreciated"""
def ip_checker(ip):
response,result=subprocess.getstatusoutput("ping -c1 -w0.5 "+ip)
if (response==0):
print(ip,"ALIVE")
else:
print(ip,"NOT ALIVE")
return ip
""""splitting in order to increase"""
ipfirst=ipfirst.split(".")
ipfirst=list(map(int,ipfirst))
iplast=iplast.split(".")
iplast=list(map(int,iplast))
"""here while loop increases the ipfirst and appends to the list (i used just append at first but it didnt work , it only added the last ipn umber times all ip numbers like ([1.1.1.5],[1.1.1.5],[1.1.1.5],[1.1.1.5])) and my problem may be occuring because of that structure ".copy()" """
while (iplast>ipfirst):
ip_adresses_list.append(ipfirst.copy())
ipfirst[3] = ipfirst[3]+1
ip_adresses_list.append(ipfirst.copy())
if (ipfirst[3]>254):
ipfirst[3]=0
ipfirst[2]=ipfirst[2]+1
ip_adresses_list.append(ipfirst.copy())
if (ipfirst[2]>254):
ipfirst[2]=0
ipfirst[1]=ipfirst[1]+1
ip_adresses_list.append(ipfirst.copy())
if(ipfirst[1]>254):
ipfirst[1]=0
ipfirst[0]=ipfirst[0]+1
ip_adresses_list.append(ipfirst.copy())
"""i rearrange the list in order to get ip structure(num1.num2.num3.num4 like that) and i mixed it with ping function(ip_checker())"""
for i in ip_adresses_list:
ip_indice1=i[0]
ip_indice2=i[1]
ip_indice3=i[2]
ip_indice4=i[3]
currentip=str(str(ip_indice1)+"."+str(ip_indice2)+"."+str(ip_indice3)+"."+str(ip_indice4))
ip_checker(currentip)
如果我 运行 我得到并输出了这段代码,我无法理解为什么除了第一个之外每个 ip 都会 ping 两次
144.122.152.10 NOT ALIVE
144.122.152.11 ALIVE
144.122.152.11 ALIVE
144.122.152.12 ALIVE
144.122.152.12 ALIVE
144.122.152.13 ALIVE
144.122.152.13 ALIVE
问题出在附加到 ip_address_list 的 while 循环中,它附加添加 1 个附加项,循环然后再次附加相同的 ip,添加 1 个附加项,依此类推,这就是为什么你得到双打,只需将第一个追加移到循环外即可修复它。
另外你的 for 循环真的是多余的我在两行中做了同样的事情哈哈
import subprocess
ipfirst=input("1st ip:")
iplast=input("2nd ip:")
currentip="144.122.152.13"
ip_adresses_list=[]
"""this function may be my problem because i didnt understand the response,result structure if anyone can explain this i'd be appreciated"""
def ip_checker(ip):
response,result=subprocess.getstatusoutput("ping -c1 -w0.5 "+ip)
if (response==0):
print(ip,"ALIVE")
else:
print(ip,"NOT ALIVE")
return ip
""""splitting in order to increase"""
ipfirst=ipfirst.split(".")
ipfirst=list(map(int,ipfirst))
iplast=iplast.split(".")
iplast=list(map(int,iplast))
"""here while loop increases the ipfirst and appends to the list (i used just append at first but it didnt work , it only added the last ipn umber times all ip numbers like ([1.1.1.5],[1.1.1.5],[1.1.1.5],[1.1.1.5])) and my problem may be occuring because of that structure ".copy()" """
ip_adresses_list.append(ipfirst.copy())
while (iplast>ipfirst):
ipfirst[3] = ipfirst[3]+1
ip_adresses_list.append(ipfirst.copy())
if (ipfirst[3]>254):
ipfirst[3]=0
ipfirst[2]=ipfirst[2]+1
ip_adresses_list.append(ipfirst.copy())
if (ipfirst[2]>254):
ipfirst[2]=0
ipfirst[1]=ipfirst[1]+1
ip_adresses_list.append(ipfirst.copy())
if(ipfirst[1]>254):
ipfirst[1]=0
ipfirst[0]=ipfirst[0]+1
ip_adresses_list.append(ipfirst.copy())
"""i rearrange the list in order to get ip structure(num1.num2.num3.num4 like that) and i mixed it with ping function(ip_checker())"""
for ip_indice in ip_adresses_list:
currentip=str(str(ip_indice[0])+"."+str(ip_indice[1])+"."+str(ip_indice[2])+"."+str(ip_indice[3]))
ip_checker(currentip)