python多线程实现ping多个ip
#!/usr/bin/env python
# -*- coding:utf-8 -*- import subprocess
import logging
import datetime
import time
import threading
try:
# Python3
from queue import Queue
except ImportError:
# Python2
from Queue import 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) # 将需要 ping 的 ip 加入队列
def insert_ip_queue(hosts_list_path):
IP_QUEUE = Queue()
with open(hosts_list_path, "r") as f:
for host in f.readlines():
IP_QUEUE.put(host)
return IP_QUEUE # 定义一个执行 ping 的函数
def ping_host(IP_QUEUE):
while not IP_QUEUE.empty():
ip = IP_QUEUE.get().strip("\n")
popen = subprocess.Popen('ping -c 1 -w 1 %s' %ip, stdout=subprocess.PIPE,shell=True)
popen.wait()
res = popen.stdout.read()
if "1 received" in res:
res = "success"
else:
res = "fail"
today = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
logging.info("%s %s %s" % (today,ip,res )) if __name__ == '__main__':
set_logging_format()
hosts_list_path = "./sgdev-hostip.txt" # 定义工作线程
WORD_THREAD_NUM = 30 while True:
IP_QUEUE = insert_ip_queue(hosts_list_path)
threads = []
for i in range(WORD_THREAD_NUM):
thread = threading.Thread(target=ping_host,args=(IP_QUEUE,))
thread.start()
threads.append(thread) for thread in threads:
thread.join()
#print("******next run************************************")
time.sleep(30)
python多线程实现ping多个ip的更多相关文章
- python多线程在渗透测试中的应用
难易程度:★★★ 阅读点:python;web安全; 文章作者:xiaoye 文章来源:i春秋 关键字:网络渗透技术 前言 python是门简单易学的语言,强大的第三方库让我们在编程中事半功倍,今天, ...
- python 多线程 ping
python 多线程 ping 多线程操作可按如下例子实现 #!/usr/bin/env python #encoding: utf8 import subprocess from threading ...
- python多线程与多进程--存活主机ping扫描以及爬取股票价格
python多线程与多进程 多线程: 案例:扫描给定网络中存活的主机(通过ping来测试,有响应则说明主机存活) 普通版本: #扫描给定网络中存活的主机(通过ping来测试,有响应则说明主机存活)im ...
- python实现批量ping IP,并将结果写入
最近工作需要,写了一个Python小脚本,分享给大家,因为公司的IP用的差不多了,然后离职人员的IP有没有及时删除,导致没多少IP用了,所以做了一个python脚本跑了跑,清出来一堆ping不通的IP ...
- python实现本地批量ping多个IP
本文主要利用python的相关模块进行批量ping ,测试IP连通性. 下面看具体代码(python3): #!/usr/bin/env python#-*-coding:utf-8-*- impor ...
- python多线程的学习
0x00.前言 学了一下python的多线程,threading模块 感觉挺有意思的,随便练手写了一个很粗陋的windows下多线程扫在线ip的脚本 脚本没什么技术含量,纯粹练手,扫一趟192的局域网 ...
- 使用Python实现批量ping操作
在日常的工作中,我们通常会有去探测目标主机是否存活的应用场景,单个的服务器主机可以通过计算机自带的DOS命令来执行,但是业务的存在往往不是单个存在的,通常都是需要去探测C段的主机(同一个网段下的存活主 ...
- python多线程ssh爆破
python多线程ssh爆破 Python 0x01.About 爆弱口令时候写的一个python小脚本,主要功能是实现使用字典多线程爆破ssh,支持ip表导入,字典数据导入. 主要使用到的是pyth ...
- python编写的自动获取代理IP列表的爬虫-chinaboywg-ChinaUnix博客
python编写的自动获取代理IP列表的爬虫-chinaboywg-ChinaUnix博客 undefined Python多线程抓取代理服务器 | Linux运维笔记 undefined java如 ...
随机推荐
- Vue.js 2.x 混入
Vue.js 2.x mixins 混入 混入(mixins)是一种分发vue组件中可复用功能的非常灵活的方式.混入对象可以包含任意组件选项.当组件使用混入对象时,所有混入对象的选项将被混入该组件本身 ...
- BUG:WSL 的 ssh server 无法启动
BUG 使用 sudo service ssh start 启动 ssh 服务,提示: * Restarting OpenBSD Secure Shell server sshd Could not ...
- NIO-FileChannel源码分析
目录 NIO-FileChannel源码分析 目录 前言 RandomAccessFile 接口 创建实例 获取文件通道 FileChannelImpl 创建 写文件 读文件 修改起始位置 获取文件长 ...
- C#ModBus Tcp的学习及Master的实现
Modbus已经成为工业领域通信协议的业界标准(De facto),并且现在是工业电子设备之间常用的连接方式. 所以这也是我们工控领域软件开发的所必懂的通讯协议,我也是初次学习,先贴上我的学习笔记 一 ...
- 数据结构与算法(周测3-Huffman树)
判断题 1.Given a Huffman tree for N (≥2) characters, all with different weights. The weight of any non- ...
- 炫酷的可视化工具包——cufflinks
前言 学过Python数据分析的朋友都知道,在可视化的工具中,有很多优秀的三方库,比如matplotlib,seaborn,plotly,Boken,pyecharts等等.这些可视化库都有自己的特点 ...
- 【转载】Request对象的作用以及常见属性
Request对象是Asp.Net应用程序中非常重要的一个内置对象,其作用主要用于服务器端获取客户端提交过来的相应信息,比较常用的有使用Requset对象获取用户提交的html表单信息,Request ...
- Linux查找文件之Find命令
Linux系统文件中常用属性包括以下内容:名称,大小,权限,属主,属组,修改时间,访问时间等.在庞大的Linux系统中查询文件,需要借助查找工具来实现,依此可以查询相同或指定属性的文件,本文所讲的查询 ...
- stm32 SD卡
容量等级 SD容量有8MB.16MB.32MB.64MB.128MB.256MB.512MB.1GB.2GB SDHC容量有2GB .4GB.8GB.16GB.32GB SDXC容量有32GB.48G ...
- 从SAP社区上的一篇博客开始,聊聊SAP产品命名背后的那份情怀
最近Jerry在SAP社区上看到一篇博客:It's Steampunk now. 博客原文:https://blogs.sap.com/2019/08/20/its-steampunk-now/ 什么 ...