Python 3.x 我正在寻找一个脚本,将采取IP地址从一个文本填充和ping所有这些

Python 3.x 我正在寻找一个脚本,将采取IP地址从一个文本填充和ping所有这些,python-3.x,Python 3.x,这是正在工作的代码,它创建了两个不同的文件,其中包含可访问和不可访问的iP列表,我希望在iP之前添加一个序列号,以便在每个文件中以以下格式保存iP: 1.八位字节 2.八位字节 等等 不确定您试图实现什么,但似乎您的ping命令是错误的。 如果只想Ping一次,Ping应该使用-c而不是-n response = os.system("ping -c 1 " + ip_address) 我在windows中找到了适用于python的脚本,希望这可以帮助任何网络工程师: 您的序列号在哪里?由于缩

这是正在工作的代码,它创建了两个不同的文件,其中包含可访问和不可访问的iP列表,我希望在iP之前添加一个序列号,以便在每个文件中以以下格式保存iP: 1.八位字节 2.八位字节 等等


不确定您试图实现什么,但似乎您的ping命令是错误的。 如果只想Ping一次,Ping应该使用
-c
而不是
-n

response = os.system("ping -c 1 " + ip_address)

我在windows中找到了适用于python的脚本,希望这可以帮助任何网络工程师:


您的序列号在哪里?由于缩进问题,此代码根本无法按发布的方式运行-您需要确保您发布的内容与您尝试运行的内容完全相同。(粘贴代码,选择所有代码,点击Control-K)你还需要澄清到底出了什么问题。我在windows中使用这个脚本,所以使用-n而不是-c
response = os.system("ping -c 1 " + ip_address)
#Create list from a file with IPs
#ip_list = open("E:\\Python-Scripts\\IPs.txt").read().splitlines()

#Remove duplicate IPs from the list
#ip_list = list(set(ip_list))

# Ping ips from the list
#import os
#for ip_address in ip_list:
# response = os.system("ping -n 1 " + ip_address)


#and then check the response...
# if response == 0:
#         with open('E:\\Python-Scripts\\ping_reachable.txt', 'a') as the_file:
#                 the_file.write('{} " is reachable\n"'.format(ip_address))
          #the_file.write('{1}.{0}  is reachable\n'.format(ip_address, count))

# else:
#         with open('E:\\Python-Scripts\\ping_unreachable.txt', 'a') as 
   the_file:
#             the_file.write('{} " is not reachable\n"'.format(ip_address))
      #the_file.write('{1}.{0}  is not reachable\n'.format(ip_address, count))

ip_list = open("E:\\Python-Scripts\\IPs.txt").read().splitlines()

ip_list = list(set(ip_list)) import os i=1 j=1 reach=[] not_reach=[]
#for n, ip_address in enumerate(ip_list): for ip_address in ip_list:
    response = os.system("ping -n 1 " + ip_address)
    if response == 0:
        reach.append('{}. {} is reachable\n'.format(i,ip_address))
        i+=1
    else:
        not_reach.append('{}. {} is not reachable\n'.format(j,ip_address))
        j+=1

with open('E:\\Python-Scripts\\ping_reachable.txt', 'a') as f:
    for i in reach:
        f.write(i)

with open('E:\\Python-Scripts\\ping_unreachable.txt', 'a') as  f:
    for i in not_reach:
        f.write(i)