使用Python实现批量ping操作
在日常的工作中,我们通常会有去探测目标主机是否存活的应用场景,单个的服务器主机可以通过计算机自带的DOS命令来执行,但是业务的存在往往不是单个存在的,通常都是需要去探测C段的主机(同一个网段下的存活主机),这样使用DOS来进行操作是不可取,探测的速度太慢了,不满足实际需要。一般批量的操作需要使用脚本进行一键部署执行,本文主要通过使用Python语言来实现批量ping的操作(使用多线程实现Python批量处理)
Python版本 :Python3
使用的第三方库:subprocess, logging, threading, queue
日志导出模块功能:
def set_logging_format():
logging.basicConfig(level=logging.INFO,
format='%(message)s',
filename='ping_host.log',
filemode='w'
)
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter('%(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)
多线程实现批量操作:
threads = []
THREAD_NUM = 20
user_iput = input('please input modren: ')
if user_iput == 'addr':
IP_L = IP_LIST
if user_iput == 'file':
IP_L = IP_QUEUE
for i in range (THREAD_NUM):
t = threading.Thread(target = ping_IP,args = (IP_L,))
threads.append(t)
for i in range (THREAD_NUM):
threads[i].start()
for i in range (THREAD_NUM):
threads[i].join()
完整代码部分:
import subprocess
import logging
import datetime
import time
import threading
from queue import Queue
import sys # 实现日志导出
def set_logging_format():
logging.basicConfig(level=logging.INFO,
format='%(message)s',
filename='ping_host.log',
filemode='w'
)
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter('%(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console) # 将需要ping 连接的IP加入队列 def insert_ip_queue(ip_list_path):
IP_QUEUE = Queue()
with open (ip_list_path,'r') as f:
for ip in f.readlines():
IP_QUEUE.put(ip)
f.close()
return IP_QUEUE def IP_list ():
ip_list = Queue()
for i in range (1,255):
ip = '192.168.1.' + str(i)
ip_list.put(ip)
return ip_list
# print (IP_list()) #print (IP_list()) #定义 ping 函数 def ping_IP (IP_QUEUE):
while not IP_QUEUE.empty():
ip = IP_QUEUE.get().strip('\n')
#print (ip)
res = subprocess.call('ping -w 1000 -n 1 %s' % ip , stdout=subprocess.PIPE,shell=True)
#print (res)
if res == 0:
h =subprocess.getoutput('ping' + ' ' + ip)
#print (h) if 'TTL=' in h:
res = ('网络可以正常连通平均延时 = %s' % h.split('平均 = ')[1])
else: res = '网络连接失败!'
today = datetime.datetime.now().strftime("%Y - %m - %d %H : %M : %S")
logging.info("%s IP = %s %s" % (today,ip,res)) def main ():
set_logging_format()
ip_list_path = './hostip.txt'
IP_QUEUE = insert_ip_queue(ip_list_path)
IP_LIST = IP_list() threads = []
THREAD_NUM = 20
user_iput = input('please input modren: ')
if user_iput == 'addr':
IP_L = IP_LIST
if user_iput == 'file':
IP_L = IP_QUEUE
for i in range (THREAD_NUM):
t = threading.Thread(target = ping_IP,args = (IP_L,))
threads.append(t)
for i in range (THREAD_NUM):
threads[i].start()
for i in range (THREAD_NUM):
threads[i].join() if __name__ == '__main__':
main()
使用Python实现批量ping操作的更多相关文章
- python实现批量ping IP,并将结果写入
最近工作需要,写了一个Python小脚本,分享给大家,因为公司的IP用的差不多了,然后离职人员的IP有没有及时删除,导致没多少IP用了,所以做了一个python脚本跑了跑,清出来一堆ping不通的IP ...
- linux,windows下检测指定的IP地址是否可用或者检测IP地址冲突的3种方式(批处理程序,python程序,linux shell 批量ping)
本文中的脚本适用范围: 1)检测某些IP地址是否被占用: 2)检测网络中某些设备是否存活: 3)在分配新的ip地址之前,批量检测环境中是否存在冲突的机器 以上检测基于ICMP Ping报文,要求所有的 ...
- shell脚本和python脚本实现批量ping IP测试
先建一个存放ip列表的txt文件: [root@yysslopenvpn01 ~]# cat hostip.txt 192.168.130.1 192.168.130.2 192.168.130.3 ...
- Shell学习笔记之shell脚本和python脚本实现批量ping IP测试
0x00 将IP列表放到txt文件内 先建一个存放ip列表的txt文件: [root@yysslopenvpn01 ~]# cat hostip.txt 192.168.130.1 192.168.1 ...
- python实现本地批量ping多个IP
本文主要利用python的相关模块进行批量ping ,测试IP连通性. 下面看具体代码(python3): #!/usr/bin/env python#-*-coding:utf-8-*- impor ...
- python 批量ping服务器
最近在https://pypi.python.org/pypi/mping/0.1.2找到了一个python包,可以用它来批量ping服务器,它是中国的大神写的,支持单个服务器.将服务器IP写在txt ...
- 批量Ping IP
刚刚接触Python 想做点什么 听说Python 在网络方便很厉害 后来总结如下: 第一:发现公司都固定IP 每次新来同事都要猜一个没有人用的IP 很费劲 第二:我们公司有的IP可以上QQ 有的不 ...
- 使用PSSH批量SSH操作Linux服务器
http://www.opstool.com/article/266 服务器多了,有一个烦恼就是如何批量快速操作一堆服务器.这里我推荐一下经常使用利器pssh.这个工具给我的工作带来了莫大的帮助. 简 ...
- python作业03-文件操作&函数
一.文件处理相关 1.编码问题 (1)请说明python2 与python3中的默认编码是什么?答:Python2默认的字符编码是ASCII,默认的文件编码也是ASCII :python3默认的字符编 ...
随机推荐
- 34.1 字符流-- FileRead FileWrite
一次读取一个字符 FileReader fr = new FileReader("aa.txt"); // System.out.println(fr.read()); // Sy ...
- C# 基础知识系列- 9 字符串的更多用法(一)
0. 前言 在前面的文章里简单介绍了一下字符串的相关内容,并没有涉及到更多的相关内容,这一篇将尝试讲解一下在实际开发工作中会遇到的字符串的很多操作. 1. 创建一个字符串 这部分介绍一下如何创建一个字 ...
- SQL基础系列(3)-变量、函数、存储过程等
1. 变量 定义变量 DECLARE @a INT 赋值 PRINT @a ) --select 赋值 SELECT @name='zcx' PRINT @name SELECT @name=F ...
- AJ学IOS 之ipad开发Popover的基本使用
AJ分享,必须精品 一:效果图 二:注意 对于方法[UIPopoverController dealloc] reached while popover is still visible. 当popo ...
- Cyclic Nacklace 杭电3746
CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, ...
- python调用小豆机器人实现自己的机器人!
大家好,人工智能是不是很酷呢? 今天我们用python调用小豆机器人实现自己的机器人(可以结合往期的语音识别更酷哦) 好,废话不多说直接上代码 import requests i=input(&quo ...
- 嵌入css方式
总体见思维导图 . 嵌入css方式 1 内联式 内联式css样式表就是把css代码直接写在现有的HTML标签中,如下面代码: <p style="color:red"> ...
- [转]PHP利用PCRE回溯次数限制绕过某些安全限制
这次Code-Breaking Puzzles中我出了一道看似很简单的题目pcrewaf,将其代码简化如下: <?php function is_php($data){ return preg_ ...
- orcale 树形结构查询
接到需求是要在一个表中(表结构为主键id和父id)循环显示数据,类似于省市县++这种情况 也可能不只有三级子菜单 id name parentid 1 a 0 2 ...
- 微信小程序填坑---小程序支付
因为公司刚刚重新做了网站,所以也吧公众号和小程序提上了日程,在公众号里面没有什么问题,直接按照官方文档进行代码编写.调试,然后就解决了公众号内支付的问题. 因为小程序提供了<webview> ...