ファイルを開くときにping_reply == 0を得ることに問題があります。私はip_list変数のリストを使用すると、それは(成功表す0)0を返す何も問題がありませんブールは、リストを使用していてもテキストファイルではない場合にTrueを表示します。
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
あなたのコードは、私たちが問題を迅速に特定するのには大きすぎます! [最小限の動作例](http://stackoverflow.com/help/mcve)を提供してください。 – Ian
ファイルから読み込むと、IPの末尾に改行があります。 –