#!/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的更多相关文章

  1. python多线程在渗透测试中的应用

    难易程度:★★★ 阅读点:python;web安全; 文章作者:xiaoye 文章来源:i春秋 关键字:网络渗透技术 前言 python是门简单易学的语言,强大的第三方库让我们在编程中事半功倍,今天, ...

  2. python 多线程 ping

    python 多线程 ping 多线程操作可按如下例子实现 #!/usr/bin/env python #encoding: utf8 import subprocess from threading ...

  3. python多线程与多进程--存活主机ping扫描以及爬取股票价格

    python多线程与多进程 多线程: 案例:扫描给定网络中存活的主机(通过ping来测试,有响应则说明主机存活) 普通版本: #扫描给定网络中存活的主机(通过ping来测试,有响应则说明主机存活)im ...

  4. python实现批量ping IP,并将结果写入

    最近工作需要,写了一个Python小脚本,分享给大家,因为公司的IP用的差不多了,然后离职人员的IP有没有及时删除,导致没多少IP用了,所以做了一个python脚本跑了跑,清出来一堆ping不通的IP ...

  5. python实现本地批量ping多个IP

    本文主要利用python的相关模块进行批量ping ,测试IP连通性. 下面看具体代码(python3): #!/usr/bin/env python#-*-coding:utf-8-*- impor ...

  6. python多线程的学习

    0x00.前言 学了一下python的多线程,threading模块 感觉挺有意思的,随便练手写了一个很粗陋的windows下多线程扫在线ip的脚本 脚本没什么技术含量,纯粹练手,扫一趟192的局域网 ...

  7. 使用Python实现批量ping操作

    在日常的工作中,我们通常会有去探测目标主机是否存活的应用场景,单个的服务器主机可以通过计算机自带的DOS命令来执行,但是业务的存在往往不是单个存在的,通常都是需要去探测C段的主机(同一个网段下的存活主 ...

  8. python多线程ssh爆破

    python多线程ssh爆破 Python 0x01.About 爆弱口令时候写的一个python小脚本,主要功能是实现使用字典多线程爆破ssh,支持ip表导入,字典数据导入. 主要使用到的是pyth ...

  9. python编写的自动获取代理IP列表的爬虫-chinaboywg-ChinaUnix博客

    python编写的自动获取代理IP列表的爬虫-chinaboywg-ChinaUnix博客 undefined Python多线程抓取代理服务器 | Linux运维笔记 undefined java如 ...

随机推荐

  1. LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)

    145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...

  2. Nginx07---反向代理

    小程序使用nginx反向代理https和wss user www www; worker_processes auto; error_log /www/wwwlogs/nginx_error.log ...

  3. supervisor+daphne+djangochannels

    参照官网配置:https://channels.readthedocs.io/en/latest/deploying.html 1.supervisor 主要是用来管理进程,比如我们想让一个进程一直执 ...

  4. django修改表数据结构后报错的解决办法

    1.进入数据库删除app之前创建的表,如果删除有关联的表报错的话可以先删除其他表然后再删除因关联关系删除不了的表 2.删除django_migrations表中对应的app记录,删除命令delete ...

  5. http GET 和 POST 请求的优缺点、区别以及误区

    原文章:https://blog.csdn.net/qq_28483283/article/details/80207674 请优先参考原文章 Get和Post在面试中一般都会问到,一般的区别: (1 ...

  6. Oracle部分

    做了很多年Oracle,转行到MySQL了,算是借鉴 Oracle日常维护管理指标 一. 基本硬件环境 1. 主机硬件环境 品牌 型号 数量 物理CPU核数及逻辑CPU数 内存大小 本地硬盘大小 光驱 ...

  7. 腾讯云开发微信小程序使用体验

    主体内容 代码构成 数据:JSON 配置文件 结构:WXML 模版文件 样式:WXSS 页面样式 交互:JS 脚本逻辑文件 云开发 云数据库 云函数 云存储 WXML 是小程序框架设计的一套标签语言, ...

  8. vue cli3 项目优化

    vue-cli3 Prefetch (官网内容) <link rel="prefetch"> 是一种 resource hint,用来告诉浏览器在页面加载完成后,利用空 ...

  9. PAT-1107 Social Clusters (30 分) 并查集模板

    1107 Social Clusters (30 分) When register on a social network, you are always asked to specify your ...

  10. jquery easyui form表单一开始就自动启用验证了,修改为form提交的时候在开启验证

    <form method="post" action="<%=path %>" class="easyui-form" d ...