打开文件时遇到ping_reply == 0的问题。当我使用ip_list变量的列表时,它没有问题返回0(其中0表示成功)
import subprocess
ip_list = []
def ip_is_valid():
check = False
#Global exposes outside the local function
global ip_list
while True:
#Prompting user for input
print "\n" + "# " * 20 + "\n"
ip_file = raw_input("# Enter IP file name followed by extension: ")
print "\n" + "# " * 20 + "\n"
#Changing exception message
try:
selected_ip_file = open(ip_file, 'r')
#Start from the beginning of the file
selected_ip_file.seek(0)
ip_list = selected_ip_file.readlines()
selected_ip_file.close()
except IOError:
print "\n* File %s does not exist. Please check and try again\n" % ip_file
for ip in ip_list:
a = ip.split('.')
if (len(a) == 4) and (1 <= int(a[0]) <= 223) and (int(a[0]) != 127) and (int(a[0]) != 169 or int(a[1]) != 254) and (0 <= int(a[1]) <= 255 and 0 <= int(a[2]) <= 255 and 0 <= int(a[3]) <= 255):
check = True
break
else:
print "\n* There was an invalid IP address. Please check and try again.\n"
check = False
continue
if check == False:
continue
elif check == True:
break
check2 = False
#Check IP Reachability
print "\n* Checking IP reachability. Please wait...\n"
while True:
for ip in ip_list:
ping_reply = subprocess.call(['ping', '-n', '2', '-w', '2', ip])
if ping_reply == 0:
check2 = True
continue
elif ping_reply == 2:
print "\n* No response from device %s." % ip
check2 = False
break
else:
print "\n* Ping to the following device has failed:", ip
check2 = False
break
#Evaluating the check flag
if check2 == False:
print "* Please re-check IP address list or device.\n"
ip_is_valid()
elif check2 == True:
print "\n* All devices are reachable."
break
我收到以下错误:
# # # # # # # # # # # # # # # # # # # #
# Enter IP file name followed by extension: ipaddrlist.txt
# # # # # # # # # # # # # # # # # # # #
* Checking IP reachability. Please wait...
Ping request could not find host 192.168.1.1
. Please check the name and try again.
* Ping to the following device has failed: 192.168.1.1
* Please re-check IP address list or device.
如果我使用列表:
Pinging 192.168.1.1 with 32 bytes of data:
Reply from 192.168.1.1: bytes=32 time=2ms TTL=63
Reply from 192.168.1.1: bytes=32 time=2ms TTL=63
Ping statistics for 192.168.1.1:
Packets: Sent = 2, Received = 2, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 2ms, Maximum = 40ms, Average = 21ms
Pinging 192.168.1.2 with 32 bytes of data:
Reply from 192.168.1.2: bytes=32 time=2ms TTL=63
Reply from 192.168.1.2: bytes=32 time=2ms TTL=63
Ping statistics for 192.168.1.2:
Packets: Sent = 2, Received = 2, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 2ms, Maximum = 2ms, Average = 2ms
>>> ping_reply == 0
True
答案 0 :(得分:0)
尝试删除ip:
之前和之后的空格 ip_file = raw_input("# Enter IP file name followed by extension: ").strip()
答案 1 :(得分:0)
啊,是的,我几乎忘了这一点,马克。这是固定代码:
import subprocess
ip_list = []
def ip_is_valid():
check = False
#Global exposes outside the local function
global ip_list
while True:
#Prompting user for input
print "\n" + "# " * 20 + "\n"
ip_file = raw_input("# Enter IP file name followed by extension: ")
print "\n" + "# " * 20 + "\n"
#Changing exception message
try:
selected_ip_file = open(ip_file, 'r')
#Start from the beginning of the file
selected_ip_file.seek(0)
ip_list = selected_ip_file.readlines()
selected_ip_file.close()
except IOError:
print "\n* File %s does not exist. Please check and try again\n" % ip_file
for ip in ip_list:
a = ip.split('.')
if (len(a) == 4) and (1 <= int(a[0]) <= 223) and (int(a[0]) != 127) and (int(a[0]) != 169 or int(a[1]) != 254) and (0 <= int(a[1]) <= 255 and 0 <= int(a[2]) <= 255 and 0 <= int(a[3]) <= 255):
check = True
break
else:
print "\n* There was an invalid IP address. Please check and try again.\n"
check = False
continue
if check == False:
continue
elif check == True:
break
check2 = False
#Check IP Reachability
print "\n* Checking IP reachability. Please wait...\n"
while True:
for ip in ip_list:
ping_reply = subprocess.call(['ping', '-n', '2', '-w', '2', ip.rstrip('\n')])
if ping_reply == 0:
check2 = True
continue
elif ping_reply == 2:
print "\n* No response from device %s." % ip
check2 = False
break
else:
print "\n* Ping to the following device has failed:", ip
check2 = False
break
#Evaluating the check flag
if check2 == False:
print "* Please re-check IP address list or device.\n"
ip_is_valid()
elif check2 == True:
print "\n* All devices are reachable."
break