shell脚本案例(五)利用nmap批量扫描存活主机

利用nmap批量扫描存活主机

知识储备:grep,nmap

一、安装nmap

1.安装编译环境

[root@arppinging nmap-7.01]# yum install gcc g++ gcc-c++ -y

2.使用wget下载nmap

[root@arppinging nmap-7.01]# wget http://nmap.org/dist/nmap-7.01.tar.bz2

3.解压下载的安装包

[root@arppinging nmap-7.01]# tar -vxf nmap-7.01.tar.bz2 

4.进入文件夹编译安装

[root@arppinging nmap-7.01]# cd nmap-7.01
[root@arppinging nmap-7.01]# ./configure 
[root@arppinging nmap-7.01]# make
[root@arppinging nmap-7.01]# make install

5.检查安装是否成功

[root@arppinging nmap-7.01]# nmap -v

使用nmap

1.sn参数
-sn: Ping Scan - disable port scan #ping探测扫描主机, 不进行端口扫描
2.扫描不存在的主机

Starting Nmap 7.01 ( https://nmap.org ) at 2018-05-24 00:30 CST
Warning: File ./nmap-payloads exists, but Nmap is using /usr/local/bin/../share/nmap/nmap-payloads for security and consistency reasons.  set NMAPDIR=. to give priority to files in your local directory (may affect the other data files too).
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.49 seconds
[root@arppinging nmap-7.01]# 

3.扫描存在的主机

[root@arppinging nmap-7.01]# nmap -sn 172.25.65.100
.
Starting Nmap 7.01 ( https://nmap.org ) at 2018-05-24 00:31 CST
Warning: File ./nmap-payloads exists, but Nmap is using /usr/local/bin/../share/nmap/nmap-payloads for security and consistency reasons.  set NMAPDIR=. to give priority to files in your local directory (may affect the other data files too).
Stats: 0:00:00 elapsed; 0 hosts completed (0 up), 1 undergoing ARP Ping Scan
ARP Ping Scan Timing: About 100.00% done; ETC: 00:31 (0:00:00 remaining)
Nmap scan report for 172.25.65.100
Host is up (0.00025s latency).
MAC Address: 2C:FD:A1:E1:EA:DB (Unknown)
Nmap done: 1 IP address (1 host up) scanned in 0.17 seconds

对比发现,存在的主机都有Nmap scan report for字段

创建脚本

1.脚本如下

[root@arppinging scripts]# cat host.sh 
#/bin/bash -
read -p "Please input scan host or network:" host
nmap -sn $host | grep "Nmap scan report for" >/dev/null &>/dev/null
[ $? -ne 0 ] && echo "host $host is down." && exit 1
nmap -sn $host  | grep "Nmap scan report for" | awk '{print $5}' > /scripts/host.txt
while read uphost
do
 echo "host $uphost is up."
done</scripts/host.txt
[root@arppinging scripts]# 

2.运行脚本(真实环境下)

[root@localhost scripts]# bash host.sh
Please input scan host or network:172.25.65.0/24
host 172.25.65.1 is up.
host 172.25.65.2 is up.
host 172.25.65.50 is up.
host 172.25.65.100 is up.
host 172.25.65.101 is up.
host 172.25.65.102 is up.
host 172.25.65.103 is up.
host 172.25.65.104 is up.
host 172.25.65.105 is up.
host 172.25.65.106 is up.
host 172.25.65.107 is up.
host 172.25.65.108 is up.
host 172.25.65.109 is up.
host 172.25.65.110 is up.
host 172.25.65.111 is up.
host 172.25.65.112 is up.
host 172.25.65.113 is up.
host 172.25.65.114 is up.
host 172.25.65.115 is up.
host 172.25.65.116 is up.
host 172.25.65.117 is up.
host 172.25.65.118 is up.
host 172.25.65.119 is up.
host 172.25.65.120 is up.
host 172.25.65.121 is up.
host 172.25.65.122 is up.
host 172.25.65.123 is up.
host 172.25.65.124 is up.
host 172.25.65.125 is up.
host 172.25.65.126 is up.
host 172.25.65.127 is up.
host 172.25.65.128 is up.
host 172.25.65.129 is up.
host 172.25.65.130 is up.
host 172.25.65.131 is up.
host 172.25.65.132 is up.
host 172.25.65.133 is up.
host 172.25.65.134 is up.
host 172.25.65.135 is up.
host 172.25.65.136 is up.
host 172.25.65.137 is up.
host 172.25.65.138 is up.
host 172.25.65.139 is up.
host 172.25.65.141 is up.
host 172.25.65.143 is up.
host 172.25.65.145 is up.
host 172.25.65.146 is up.
host 172.25.65.147 is up.
host 172.25.65.148 is up.
host 172.25.65.149 is up.
host 172.25.65.150 is up.
host 172.25.65.151 is up.
host 172.25.65.152 is up.
host 172.25.65.10 is up.

主机不存在的情况
[root@localhost scripts]# bash host.sh
Please input scan host or network:172.25.65.199
host 172.25.65.199 is down.
[root@localhost scripts]# 

有问题的话请评论吧,谢谢

arppinging技术社区
欢迎关注的我的个人微信公众号

二维码

猜你喜欢

转载自blog.51cto.com/xiaowangzai/2119515