我想要实现的是从 TXT 文件中读取 IP/域并执行操作系统导入命令,然后在 ping 时添加 IP/域。
但是问题是,由于某种原因,它在从 TXT 文件读取的 IP/域的末尾放置了一个句点,导致在 pining 时请求无效(代码有效,唯一的问题是末尾的句点)
例如:当 ping 行在编译器上执行时,它告诉我“google.com 参数错误”。然而,在它自己的 txt 文件中,只有一个句点是它自己的 .com 句点。
def scanlist():
ipopen = open("IPlist.txt")
#Opens the IPlist.txt file and strips each of the lines so that we can read individually.
with open("IPlist.txt", "r+") as ips_file:
ips = [ip.strip() for ip in ips_file.readlines()]
#Read each line from the IPlist.txt file
with open("IPlist.txt", "r") as available_ips_file:
for ip in ips:
#Pings each line from the IPlist.txt file
response = os.system('ping -a 1 {}'.format(ip))
if response == 0: # 512/DOWN value - 0/UP value
# Up
print("- Ip Address:", ip, 'is up!')
elif response == 512:
#down
print("- IP Address:", ip, 'is down!')
else:
#other error
print("- Bad parameters or other error!")
完整代码请访问 github:https ://github.com/Hontiris1/IPPing/blob/master/Ping.py