生产实战案例


    在日常的生产环境中,可能会遇到需要批量检查内网目前在线的主机IP地址有哪些,还可能需要检查这些在线的主机哪些端口是开放状态,因此依靠手工来检查是可以实现,但比较费时费力,所以需要结合shell脚本来实现批量检查的功能,那么今天就来做个小小的实验。


1、开发脚本前准备


一般大家都知道,测试主机是否在线,常用的命令无非就是ping、nmap,因此,首先找一个地址来测试下ping命令的效果


[root@centos6 scripts]# ping 172.16.1.1

PING 172.16.1.1 (172.16.1.1) 56(84) bytes of data.

64 bytes from 172.16.1.1: icmp_seq=1 ttl=255 time=3.43 ms

64 bytes from 172.16.1.1: icmp_seq=2 ttl=255 time=0.699 ms

^C

--- 172.16.1.1 ping statistics ---

9 packets transmitted, 9 received, 0% packet loss, time 8448ms

rtt min/avg/max/mdev = 0.525/1.053/3.436/0.884 ms


好像单纯的这种命令是无法来做批量检查的,必须要带一些参数,否则它们一直ping下去


[root@centos6 scripts]# ping -W 2 -c 2 172.16.1.1

PING 172.16.1.1 (172.16.1.1) 56(84) bytes of data.

64 bytes from 172.16.1.1: icmp_seq=1 ttl=255 time=0.704 ms

64 bytes from 172.16.1.1: icmp_seq=2 ttl=255 time=0.481 ms

--- 172.16.1.1 ping statistics ---

2 packets transmitted, 2 received, 0% packet loss, time 1000ms

rtt min/avg/max/mdev = 0.481/0.592/0.704/0.114 ms


这种方法可以实现,测试发送2个数据包,然后加上超时时间,自动停止,可以达到效果


[root@centos6 scripts]# echo $?

0

[root@centos6 scripts]# ping -W 2 -c 2 172.16.1.100

PING 172.16.1.100 (172.16.1.100) 56(84) bytes of data.

^C

--- 172.16.1.100 ping statistics ---

2 packets transmitted, 0 received, 100% packet loss, time 2836ms

[root@centos6 scripts]# echo $?

1

因此,我们可以通过返回值来判断是否在线



2、开发简单脚本

既然有实现的方法了,那么接下来就开始开发脚本了

[root@centos6 scripts]# vi checkip.sh

#!/bin/sh

. /etc/init.d/functions

#加载系统函数库

CMD="ping -W 2 -c 2"

#定义命令变量

IP="172.16.1.2 172.16.1.3 172.16.1.100"

#定义IP变量

for n in $IP

  #for循环语句

do

$CMD $IP$n >/dev/null 2>&1

#将命令结果不输出

if [ $? -eq 0 ];then

#如果返回值为0就表明在线

action  "$IP$n is online" /bin/true

#在线就打印此信息

else

#否则就表示不在线

action "$IP$n is not online" /bin/false

#不在线就打印此信息

fi

done

执行下脚本看看结果如何

[root@centos6 scripts]# sh checkip.sh

172.16.1.2 is online                  [  OK  ]

172.16.1.3 is online                  [  OK  ]

172.16.1.100 is not online        [FAILED]


此时肯定有小伙伴问了,你这个脚本测试的只有三个IP,如果内网整个网段IP都手工写上去,岂不是更费时费力,因此,如果是整个网段,那么定义IP变量时可以定义成这样IP="172.16.1." 因为前三位是相同的,写for 循环时可以修改成如下

for n in `seq 254`

do

   $CMD $IP$n(将两段数字拼接成IP地地址)

done

具体这里就不再测试了,有兴趣的可以自行测试下



3、开发nmap脚本检查在线IP与在线IP的开放端口情况


   首先得了解下nmap的一些参数,它也是非常实用的命令之一,在日常实际生产环境中,经常用来检查IP、端口、URL地址信息,具体其中的参数这里就不做详细介绍了,后续有时间会分享它的相关参数用法


[root@centos6 scripts]# nmap -sP 172.16.1.1

Starting Nmap 5.51 ( http://nmap.org ) at 2016-12-03 21:09 CST

Nmap scan report for 172.16.1.1

Host is up (0.0091s latency).

MAC Address: 04:BD:70:FB:A9:B7 (Unknown)

Nmap done: 1 IP address (1 host up) scanned in 0.04 seconds

[root@centos6 scripts]# nmap -sP 172.16.1.100

Starting Nmap 5.51 ( http://nmap.org ) at 2016-12-03 21:09 CST

Note: Host seems down. If it is really up, but blocking our ping probes, try -Pn

Nmap done: 1 IP address (0 hosts up) scanned in 0.41 seconds


从上面的结果来看,很容易发现在线与不在线返回的信息不同,但是我们需要取得在线的IP地址信息,那到就只能取 Nmap scan report for 172.16.1.1 因为所有在线的IP返回的信息中都会有这一行信息,所以取相同的信息。


[root@centos6 scripts]# nmap -sS 172.16.1.1|grep "Nmap scan report for"

Nmap scan report for 172.16.1.1

[root@centos6 scripts]#

nmap -sS 172.16.1.1|grep "Nmap scan report for"|awk '{print $5}'

172.16.1.1

#取出IP信息


[root@centos6 scripts]# nmap -sS 172.16.1.1

Starting Nmap 5.51 ( http://nmap.org ) at 2016-12-03 20:56 CST

Nmap scan report for 172.16.1.1

Host is up (0.041s latency).

Not shown: 994 closed ports

PORT    STATE    SERVICE

21/tcp  open     ftp

22/tcp  filtered ssh

23/tcp  open     telnet

80/tcp  open     http

179/tcp filtered bgp

443/tcp open     https

MAC Address: 04:BD:70:FB:A9:B7 (Unknown)

Nmap done: 1 IP address (1 host up) scanned in 8.74 seconds


检查开启端口,我们可以通过过滤关键字 open 来实现,通过上面的信息很容易观察出来


[root@centos6 scripts]# nmap -sS 172.16.1.1|grep "open"

21/tcp  open     ftp

23/tcp  open     telnet

80/tcp  open     http

443/tcp open     https

[root@centos6 scripts]# nmap -sS 172.16.1.1|grep "open"|awk '{print $1}'

21/tcp

23/tcp

80/tcp

443/tcp



4、编写脚本并测试效果

[root@centos6 scripts]# vi checkip_namp01.sh

#!/bin/sh

. /etc/init.d/functions

 #加载系统函数库

FCMD="nmap -sP "

#定义第一个命令变量

IP="172.16.1.1 172.16.1.2 172.16.1.100"

 #定义IP变量

TCMD="nmap -sS"

#定义第一个命令变量

UPIP=`$FCMD $IP|grep "Nmap scan report for"|awk '{print $5}'`

#定义获取在线IP的变量

for ip in ${UPIP}

#for手环语句

do

action "$ip is on line"  /bin/true

#打印信息

UPPORT=`$TCMD $ip|grep "open"|awk '{print $1}'`

#定义获取在线IP的开放端口变量

for port in ${UPPORT}

#二层循环检查端口

do

action "$ip $port is open"  /bin/true

     #将上面在线IP开放的端口信息打印输出

done

done

注:UPPORT=`$TCMD $ip|grep "open"|awk '{print $1}'` 定义这个变量时,取的IP地址一定要是上一个循环取出的IP地址,否则会有问题


执行脚本,测试效果如何?


[root@centos6 scripts]# sh checkip_namp01.sh

172.16.1.1 is on line                   [  OK  ]

172.16.1.1 21/tcp is open           [  OK  ]

172.16.1.1 23/tcp is open           [  OK  ]

172.16.1.1 80/tcp is open           [  OK  ]

172.16.1.1 443/tcp is open         [  OK  ]

172.16.1.2 is on line                   [  OK  ]

172.16.1.2 23/tcp is open           [  OK  ]

172.16.1.100没有出现的原因是它不在线


接下来测试下脚本检查的端口是否正确


[root@centos6 scripts]# telnet 172.16.1.1 443

Trying 172.16.1.1...

Connected to 172.16.1.1.

Escape character is '^]'.

^]

telnet> quit

Connection closed.

[root@centos6 scripts]# telnet 172.16.1.1 21

Trying 172.16.1.1...

Connected to 172.16.1.1.

Escape character is '^]'.

220 FTP service ready.

^]

telnet> quit

Connection closed.

[root@centos6 scripts]# telnet 172.16.1.2 23

Trying 172.16.1.2...

Connected to 172.16.1.2.

Escape character is '^]'.

TL-AP301C login:

telnet> quit

Connection closed.


从上面的结果来看,脚本检查的结果是正确,如果需要检查整个网段只需要将定义IP变量时定义成“IP="172.16.1.0/24"”即可

shell脚本编程——生产实战案例的更多相关文章

  1. Linux Shell脚本编程while语句案例

    1,每隔3秒,打印一次系统负载 #!/bin/bash while true do uptime done 2,把监控结果保存到文件,在后台执行,然后用tail -f监控文件变化 ghostwu@de ...

  2. Linux Shell脚本编程while语句

    Linux Shell脚本编程while语句案例 1,每隔3秒,打印一次系统负载 #!/bin/bash while truedo    uptime    sleep 3done 2,把监控结果保存 ...

  3. SHELL脚本编程-expect

    SHELL脚本编程-expect 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.expect概述 1>.expect介绍 expect 是由Don Libes基于Tcl( ...

  4. SHELL脚本编程基础知识

    SHELL脚本编程基础知识 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. Linux之父Linus有一句话很经典:"Talk is cheap, show me the ...

  5. Shell脚本编程30分钟入门

    Shell脚本编程30分钟入门 转载地址: Shell脚本编程30分钟入门 什么是Shell脚本 示例 看个例子吧: #!/bin/sh cd ~ mkdir shell_tut cd shell_t ...

  6. 30分钟快速学习Shell脚本编程

    什么是Shell脚本 示例 看个例子吧: #!/bin/sh cd ~ mkdir shell_tut cd shell_tut for ((i=0; i<10; i++)); do touch ...

  7. centos shell脚本编程2 if 判断 case判断 shell脚本中的循环 for while shell中的函数 break continue test 命令 第三十六节课

    centos  shell脚本编程2 if 判断  case判断   shell脚本中的循环  for   while   shell中的函数  break  continue  test 命令   ...

  8. 《Linux命令行与shell脚本编程大全 第3版》

    第一部分 Linux 命令行 第1章  初识Linux she1.1   什么是Linux 21.1.1 深入探究Linux 内核 31.1.2 GNU 工具 61.1.3 Linux 桌面环境 81 ...

  9. SHELL脚本编程-字符串处理

    SHELL脚本编程-字符串处理 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.字符串切片 [root@node101.yinzhengjie.org.cn ~]# echo { ...

随机推荐

  1. 【Linux内核】CPU和线程

    首先明确一个概念,Linux系统中甚至没有真正的线程.不过,可以认为Linux是系统的线程是内核线程,所有调度是基于线程的. 1.线程分类 一个进程由于其运行空间的不同, 从而有内核线程和用户进程的区 ...

  2. python day 9: xlm模块,configparser模块,shutil模块,subprocess模块,logging模块,迭代器与生成器,反射

    目录 python day 9 1. xml模块 1.1 初识xml 1.2 遍历xml文档的指定节点 1.3 通过python手工创建xml文档 1.4 创建节点的两种方式 1.5 总结 2. co ...

  3. day27-python之迭代器协议

    1.item系列方法 # class Foo: # def __getitem__(self, item): # print('getitem',item) # return self.__dict_ ...

  4. Android笔记(六十三) android中的动画——逐帧动画( frame-by-frame animation)

    就好像演电影一样,播放实现准备好的图片,来实现动画效果. 逐帧动画需要用到AnimationDrawable类,该类主要用于创建一个逐帧动画,然后我们把这个动画设置为view的背景即可. androi ...

  5. Android笔记(六十二)网络框架volley

    什么是Volley 很多时候,我们的APP都需要用到网络技术,使用HTTP协议来发送接收数据,谷歌推出了一个网络框架——volley,该框架适合进行数据量不大,但通信频繁的网络操作. 它的优点: (1 ...

  6. JQuery EasyUI treegrid展开与折叠,以及数据加载两次的问题

    问题:做项目的时候遇到代码生成的页面,只默认展开了一级节点,每次操作之后刷新还要手动一级一级展开,太麻烦了 官方API:http://www.jeasyui.net/plugins/186.html ...

  7. spice在桌面虚拟化中的应用系列之二(Linux平台spice客户端的编译安装,支持USB映射)

    1.系统环境 1.1 测试环境 centos6.4最小化安装(centos6.x桌面版也适用) 使用yum源为163源加EPEL源 1.2 spice客户端介绍 spice作为远程连接工具,可以支持远 ...

  8. java安全相关知识

    基本概念 JVM:java虚拟机,Java编译程序将生成Java虚拟机上可运行的目标代码,使得Java程序可以再不同平台不加修改的运行.JVM包含完善的硬件架构,主要分为五大模块-类装载器子系统.运行 ...

  9. Redis的竞争并发该如何解决?

    这个也是线上非常常见的一个问题,就是多客户端同时并发写一个key,可能本来应该先到的数据后到了,导致数据版本错了.或者是多客户端同时获取一个key,修改值之后再写回去,只 要顺序错了,数据就错了. 而 ...

  10. tensorflow LSTM+CTC使用详解

    最近用tensorflow写了个OCR的程序,在实现的过程中,发现自己还是跳了不少坑,在这里做一个记录,便于以后回忆.主要的内容有lstm+ctc具体的输入输出,以及TF中的CTC和百度开源的warp ...