我不明白为什么我得到这样的输出

时间:2016-11-08 17:18:13

标签: python-3.x ip subprocess output ping

我正在尝试编写一个脚本,在两个给定范围之间发送ping到ip,并试图了解它们是否可用。我做了一些进步,但有些东西我仍然无法解决。例如,我得到输出两次。如果你可以帮助我,那么我的代码就是完美的:

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)

如果我运行这个代码我得到并输出像icant理解为什么它ping每次ip除了第一个

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

1 个答案:

答案 0 :(得分:0)

问题在于你的while循环附加到ip_address_list,它追加添加1个附加,循环然后再添加相同的ip,添加1追加,依此类推,这就是为什么你得到双打,只需移动循环外的第一个附加修复它。

你的for循环真的是多余的我在2行中做了同样的事情lol

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)