前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Shell学习笔记之shell脚本和python脚本实现批量ping IP测试

Shell学习笔记之shell脚本和python脚本实现批量ping IP测试

作者头像
Jetpropelledsnake21
发布2018-12-19 16:39:15
1.8K0
发布2018-12-19 16:39:15
举报
文章被收录于专栏:JetpropelledSnakeJetpropelledSnake

0x00 将IP列表放到txt文件内

先建一个存放ip列表的txt文件:

代码语言:javascript
复制
[root@yysslopenV**01 ~]# 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

0x01 使用Shell脚本实现

创建shell 脚本:

代码语言:javascript
复制
[root@yysslopenV**01 ~]# 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()

注意:请不要直接粘贴复制,如果使用以上shell请在linux主机的vim中自己手动编写,不然会出现换行符报错!

代码语言:javascript
复制
# syntax error near unexpected token `do!

添加脚本权限

代码语言:javascript
复制
[root@yysslopenV**01 ~]#  chmod +x shell_ping.sh

执行:

代码语言:javascript
复制
[root@yysslopenV**01 ~]#  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

0x02 使用Python脚本实现

创建python脚本:

代码语言:javascript
复制
[root@yysslopenV**01 ~]# 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)

执行结果:

代码语言:javascript
复制
[root@yysslopenV**01 ~]# 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

参考

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-11-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 0x00 将IP列表放到txt文件内
  • 0x01 使用Shell脚本实现
  • 0x02 使用Python脚本实现
  • 参考
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档