0
新手在這裏,請原諒我的不確定性。IndexError:列表索引超出解析腳本的範圍
我正在嘗試創建一個腳本,它在分析輸出時創建一個名爲ARPips.prn的IP地址文件,以便隨後可以創建結果圖形。我一直在研究這個網站和其他網站上的這個問題,我無法將可能的解決方案應用於我的案例。
這裏是我的腳本:
def ping2():
plat = platform.system()
with open('C:\Python26\ARPips.prn', 'r') as f:
for line in f:
#while True:
if plat == "Windows":
pingArgs = ["ping", "-n", "1", "-l", "1", "-w", "100", line]
elif plat == "Linux":
pingArgs = ["ping", "-c", "1", "-l", "1", "-s", "1", "-W", "1"]
else:
raise ValueError("Unknown platform")
ping = subprocess.Popen(pingArgs,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE,
shell=True)
out, error = ping.communicate()
print out
lost = int(re.findall(r"Lost = (\d+)", out)[0])
min = int(re.findall(r"Minimum = (\d+)", out)[0])
max = int(re.findall(r"max = (\d+)", out)[0])
print "Packet loss = %s" % lost
print "Maximum = %s" % max
print "Minimum = %s" % min
x = line.split(";")
if len(x) < 14:
continue
else:
print"Invalid line l =", x
我得到的輸出如下:
Pinging 10.10.10.1 with 1 bytes of data:
Reply from 10.10.10.1: bytes = 1, time = 3 ms, TTL=255
Ping statistics for 10.10.10.1:
Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
Approximate round trip times in milliseconds:
Minimum = 3ms, Maximum = 3ms, Average = 3ms
Packet loss = 0
Maximum = 3
Minimum = 3
Pinging 10.10.10.41 with 1 bytes of data:
Request timed out
Ping statistics for 10.10.10.41:
Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
錯誤消息:
Traceback (most recent call last):
File "C:\Python26\ScapyScripts\main.py", line 114, in ping2
min = int(re.findall(r"Minimum = (\d+)", out)[0])
IndexError: list index out of range
現在,如果我註釋掉以下行:
min = int(re.findall(r"Minimum = (\d+)", out)[0])
max = int(re.findall(r"max = (\d+)", out)[0])
print "Maximum = %s" % max
print "Minimum = %s" % min
錯誤消失,代碼正常運行。如果有人能幫助我解決這個問題,我會很感激!提前致謝!
首先感謝您的關注,我非常感謝您的幫助! 我已將輸出添加到我的問題中,您怎麼看? –
謝謝! 分鐘= INT(re.findall(R 「最小值=(\ d +)」,下)[0]) 到這一行:: 重象你說,通過改變這一行 問題是。 findall(r「Minimum =(\ d +)」,out) 再次感謝! :d –