shell脚本和python脚本实现批量ping IP测试
先建一个存放ip列表的txt文件:
[root@yysslopenvpn01 ~]# cat hostip.txt
192.168.130.1
192.168.130.2
192.168.130.3
192.168.130.4
192.168.130.5
192.168.130.6
192.168.130.7
192.168.130.8
192.168.130.9
192.168.130.10
192.168.130.11
192.168.130.12
192.168.130.13
192.168.130.14
192.168.130.15
192.168.130.16
192.168.130.17
192.168.130.18
192.168.130.19
192.168.130.20
创建shell 脚本
[root@yysslopenvpn01 ~]# vim shell_ping.sh #!/bin/sh for i in `cat hostip.txt`
do
ping -c 4 $i|grep -q 'ttl=' && echo "$i ok" || echo "$i failed"
done
exit()
添加脚本权限
[root@yysslopenvpn01 ~]# chmod +x shell_ping.sh
执行:
[root@yysslopenvpn01 ~]# sh shell_ping.sh
192.168.130.1 ok
192.168.130.2 failed
192.168.130.3 failed
192.168.130.4 failed
192.168.130.5 failed
192.168.130.6 failed
192.168.130.7 failed
192.168.130.8 failed
192.168.130.9 failed
192.168.130.10 failed
192.168.130.11 failed
192.168.130.12 failed
192.168.130.13 failed
192.168.130.14 failed
192.168.130.15 failed
192.168.130.16 failed
192.168.130.17 failed
192.168.130.18 ok
192.168.130.19 ok
192.168.130.20 ok
创建python脚本:
[root@yysslopenvpn01 ~]# vim ping.py #!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:xieshengsen # 实现批量ping IP测试 import re
import subprocess def check_alive(ip,count=4,timeout=1):
cmd = 'ping -c %d -w %d %s'%(count,timeout,ip)
p = subprocess.Popen(cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True
) result=p.stdout.read()
regex=re.findall('100% packet loss',result)
if len(regex)==0:
print "\033[32m%s UP\033[0m" %(ip)
else:
print "\033[31m%s DOWN\033[0m" %(ip) if __name__ == "__main__":
with file('hostip.txt','r') as f:
for line in f.readlines():
ip = line.strip()
check_alive(ip)
执行结果:
[root@yysslopenvpn01 ~]# python ping.py
192.168.130.1 UP
192.168.130.2 DOWN
192.168.130.3 DOWN
192.168.130.4 DOWN
192.168.130.5 DOWN
192.168.130.6 DOWN
192.168.130.7 DOWN
192.168.130.8 DOWN
192.168.130.9 DOWN
192.168.130.10 DOWN
192.168.130.11 DOWN
192.168.130.12 DOWN
192.168.130.13 DOWN
192.168.130.14 DOWN
192.168.130.15 DOWN
192.168.130.16 DOWN
192.168.130.17 DOWN
192.168.130.18 UP
192.168.130.19 UP
192.168.130.20 UP
shell脚本和python脚本实现批量ping IP测试的更多相关文章
- Shell学习笔记之shell脚本和python脚本实现批量ping IP测试
0x00 将IP列表放到txt文件内 先建一个存放ip列表的txt文件: [root@yysslopenvpn01 ~]# cat hostip.txt 192.168.130.1 192.168.1 ...
- 批量Ping IP
刚刚接触Python 想做点什么 听说Python 在网络方便很厉害 后来总结如下: 第一:发现公司都固定IP 每次新来同事都要猜一个没有人用的IP 很费劲 第二:我们公司有的IP可以上QQ 有的不 ...
- python实现本地批量ping多个IP
本文主要利用python的相关模块进行批量ping ,测试IP连通性. 下面看具体代码(python3): #!/usr/bin/env python#-*-coding:utf-8-*- impor ...
- shell 编写脚本批量Ping IP
服务器总是一下子买了很多的段的ip.通过绑定后,也不知道这些ip是否绑定成功,所以就写了一个shell脚本,把ip输好,批量ping一下,看是不是都能ping通. 脚本如下: 此外.还有一个ip文件, ...
- 【原】Gradle调用shell脚本和python脚本并传参
最近由于项目自动化构建的需要,研究了下gradle调用脚本并传参的用法,在此作个总结. Pre build.gradle中定义了$jenkinsJobName $jenkinsBuild两个Jenki ...
- 批量ping IP并检测IP延迟率和丢包率脚本
脚本文件如下: #!/bin/bash #Author:Mr.Ding #Created Time:2018-08-26 07:23:44 #Name:ping.sh #Description: sh ...
- python实现批量ping IP,并将结果写入
最近工作需要,写了一个Python小脚本,分享给大家,因为公司的IP用的差不多了,然后离职人员的IP有没有及时删除,导致没多少IP用了,所以做了一个python脚本跑了跑,清出来一堆ping不通的IP ...
- shell脚本调用python脚本的路径问题
脚本的相互调用中,只有在同一级目录下才可以使用__file__参数去获取路径名,(在shell里使用pwd也同样),否则,使用的就是主文件(最开始运行的脚本程序)的所在位置,是错误路径:一定要注意当期 ...
- 有米实习-用到的shell脚本和Python脚本记录
Shell:LOG_DATE=`date -d "1 day ago" +%Y-%m-%d` #以指定格式设置一天前的年份月份日期 aws s3 ls $LAST5_BASE_PA ...
随机推荐
- K-means &K-medoids 聚类
k-平均值算法对孤立点很敏感!因为具有特别大的值的对象可能显著地影响数据的分布. k-中心点(k-Medoids): 不采用簇中对象的平均值作为参照点, 而是选用簇中位置最中心的对象, 即中心点(me ...
- GENIL_BOL_BROWSER, GENIL_MODEL_BROWSER,BSP_WD_CMPWB 使用方法
一:GENIL_BOL_BROWSER 使用方法 1: 进入x3c系统.输入T-CODE GENIL_BOL_BROWSER 2: 输入一个component set 名称 3: 选择一个对象,双 ...
- echarts 实现tooltip双栏效果
实现效果如下: 代码: //option tooltip: { trigger: 'axis', axisPointer: { label: { show: true, fontSize: 15 } ...
- 多线程——interrupt方法
测试interrupt()方法: package day_12_01_Thread; import java.util.Date; /** * 测试interrupt()方法:结束线程,但是线程还是活 ...
- [py][mx]django处理登录逻辑
浏览器同源策略(same-origin policy) csrf攻击防御核心点总结 django的cookie和session操作-7天免登录 flask操作cookie&django的see ...
- 微信小程序:wx.navigateBack页面返回传参
场景(比如:商城选地址,选优惠券): 在[页面A]中调用wx.navigateTo方法跳转到[页面B] 然后从[页面B]返回[页面A], 并将[页面B]中的一些数据传回[页面A] wx.navigat ...
- confd动态生成配置文件
下载安装confd $ mkdir -p $GOPATH/src/github.com/kelseyhightower $ git clone https://github.com/kelseyhig ...
- keras 分类回归 损失函数与评价指标
1.目标函数 (1)mean_squared_error / mse 均方误差,常用的目标函数,公式为((y_pred-y_true)**2).mean()(2)mean_absolute_error ...
- 查看CPU信息
基础知识 示例: 开发机器是1个物理CPU,4核8线程,Intel(R) Core(TM) i7-3770 CPU @ 3.40GHz 基础 i3:2核模拟4线程,无睿频 i5:4核模拟4线程,有睿频 ...
- 使用 nghttpx 搭建 HTTP/2 代理 (转)
来自http://www.fanyue.info/2015/08/nghttpx-http2.html 使用 nghttpx 搭建 HTTP/2 代理 [转] HTTP/1.1,定义于 1999 年, ...