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

先建一个存放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

0x01 使用Shell脚本实现

创建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()

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

# syntax error near unexpected token `do!

添加脚本权限

[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

0x02 使用Python脚本实现

创建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学习笔记之shell脚本和python脚本实现批量ping IP测试的更多相关文章

  1. shell脚本和python脚本实现批量ping IP测试

    先建一个存放ip列表的txt文件: [root@yysslopenvpn01 ~]# cat hostip.txt 192.168.130.1 192.168.130.2 192.168.130.3 ...

  2. 【转】shell学习笔记(三)——引用变量、内部变量、条件测试、字符串比较、整数比较等

    1.env显示当前的环境变量 2.PS1='[\u@\h \w \A] \$' 可以设置bash的命令与提示符. 3.echo $$ 显示当前bash的PID号 4.echo $?显示上一条指令的回传 ...

  3. 鸟书shell 学习笔记(一) shell专注于概念和命令

    变量   variableName=value 等号左右不能有空格 变量内容有空格须要用"或者'括起来,可是 v="hello $name" $保持原有功能,单引號则不行 ...

  4. shell学习笔记2: shell中的四则运算符

    shell中的四则运算符 n1,n2 :常量数字 char:运算符号 加,减,乘,除,取余(+,-,*,/,%) $a,$b:变量a,变量b 方法1 数字与符号之间需要有空格 不支持小数 expr n ...

  5. shell学习笔记1: shell 中的变量与常见符号使用方法

    变量 声明即用 a=2 b="123" 调用 ${varName}或者 $varName echo $b echo ${a} 常见变量 $?:判断上一个语句是否成功 $0:执行脚本 ...

  6. 鸟书shell 学习笔记(二) shell中正則表達式相关

    通配符与正則表達式的差别 通配符是bash原生支持的语法,正則表達式是处理字符串的一种表示方式, 正則表達式须要支持的工具支持才干够 语系设置 : export LANG=C grep alias 设 ...

  7. shell学习笔记汇总

    1.shell脚本中函数使用 函数定义在前,调用在后,顺序反了就没有效果了.函数调用为:函数名 参数列表 函数内部通过以下变量访问函数的参数:shell脚本函数中: $0: 这个脚本的名字 $n: 这 ...

  8. SHELL学习笔记三

    SHELL学习笔记一 SHELL学习笔记二 SHELL学习笔记三 for 命令 读取列表中的复杂值 从变量读取列表 从命令读取值 更改字段分隔符 用通配符读取目录 which 使用多个测试命令 unt ...

  9. shell学习笔记

    shell学习笔记 .查看/etc/shells,看看有几个可用的Shell . 曾经用过的命令存在.bash_history中,但是~/.bash_history记录的是前一次登录前记录的所有指令, ...

随机推荐

  1. HttpClient 学习整理【转】

    转自 http://www.blogjava.net/Alpha/archive/2007/01/22/95216.html HttpClient 是我最近想研究的东西,以前想过的一些应用没能有很好的 ...

  2. 170811、Java获取jdk系统环境变量

    package com.rick.utils; /******************************************************** *@Desc: 系统变量属性工具类 ...

  3. 2018C语言第三次作业

    要求一 2.struct sk{int a; char *str)}*p;   p->str++ 中的++ 加向? ++加向srt的地址. 要求二 题目1-计算平均成绩 1.设计思路 (1)主要 ...

  4. redis两种持久化方式的优缺点

    redis两种持久化的方式 RDB持久化可以在指定的时间间隔内生成数据集的时间点快照 AOF持久化记录服务器执行的所有写操作命令,并在服务器启动时,通过重新执行这些命令来还原数据集,AOF文件中全部以 ...

  5. intptr_t、uintptr_t数据类型的解析

    https://blog.csdn.net/cs_zhanyb/article/details/16973379 2013年11月26日 22:20:09 binggo 阅读数:14066   最近开 ...

  6. PowerSploit: The Easiest Shell You'll Ever Get - Pentest Geek - Penetration Testing - Infosec Professionals

                    PowerSploit: The Easiest Shell You'll Ever Get - Pentest... Sometimes you just want ...

  7. NTLM

    我们介绍Kerberos认证的整个流程.在允许的环境下,Kerberos是首选的认证方式.在这之前,Windows主要采用另一种认证协议——NTLM(NT Lan Manager).NTLM使用在Wi ...

  8. nginx之配置proxy_set_header

    win10客户端请求web服务,win10的ip:192.168.223.1 nginx作为反向代理服务器:192.168.223.136 nginx作为后端web服务器:192.168.223.13 ...

  9. Windows下搭建Git服务器各种问题汇总(一)

    **************************************************************************************************** ...

  10. Chart控件的使用实例

    ChartTest.aspx: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind=&quo ...