ITPub博客

首页 > 应用开发 > Python > 批量ping主机

批量ping主机

原创 Python 作者:haohaobro 时间:2022-06-09 17:24:58 0 删除 编辑

1. 修改 pingdemo.py代码中相关文件路径和名字


pingdemo.py

import os,time
import sys
start_Time=int(time.time())
ip_True = open(r'C:\Users\28942\Desktop\ip_True.txt','w+')
ip_False = open(r'C:\Users\28942\Desktop\ip_False.txt','w+')
count_True,count_False = 0,0
with open(r'C:\Users\28942\Desktop\ping.txt', 'r') as f:
        for ip in f:
            return1 = os.system('ping -n 1 -w 1 %s' % ip)
            if return1:
                print('ping %s is fail' % ip)
                ip_False.write(ip+'\n')
                count_False += 1
            else:
                print('ping %s is ok' % ip)
                ip_True.write(ip+'\n')
                count_True += 1
ip_True.close()
ip_False.close()
end_Time = int(time.time())
print("time:", end_Time - start_Time, "s")
print("count ping is ok", count_True, "   count ping is fail", count_False)



2. 将主机列表添加到 hosts.txt 文件中,每个主机一行


hosts.txt

192.168.202.20
192.168.202.30
192.168.202.24
192.168.202.72
192.168.202.73
192.168.240.141
192.168.240.150
192.168.240.131
192.168.240.143
192.168.240.140
192.168.240.155
192.168.240.156
192.168.202.76
192.168.202.77


3. 执行 pingdemo.py


python3 pingdemo.py



4. 能够生成 ping 通和为 ping 通的主机列表清单

在指定目录生成ping通(ip_True.txt)和未ping通 ( ip_False.txt )的txt列表



来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70018804/viewspace-2899740/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 没有了~
下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2022-06-09

  • 博文量
    1
  • 访问量
    552