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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell脚本案例利用nmap批量扫描存活主机相关的知识,希望对你有一定的参考价值。

利用nmap批量扫描存活主机

知识储备:grep,nmap

一、安装nmap

1.安装编译环境

[[email protected] nmap-7.01]# yum install gcc g++ gcc-c++ -y

2.使用wget下载nmap

[[email protected] nmap-7.01]# wget http://nmap.org/dist/nmap-7.01.tar.bz2

3.解压下载的安装包

[[email protected] nmap-7.01]# tar -vxf nmap-7.01.tar.bz2 

4.进入文件夹编译安装

[[email protected] nmap-7.01]# cd nmap-7.01
[[email protected] nmap-7.01]# ./configure 
[[email protected] nmap-7.01]# make
[[email protected] nmap-7.01]# make install

5.检查安装是否成功

[[email protected] 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
[[email protected] nmap-7.01]# 

3.扫描存在的主机

[[email protected] 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.脚本如下

[[email protected] 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
[[email protected] scripts]# 

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

[[email protected] 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.

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

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

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

技术分享图片

以上是关于shell脚本案例利用nmap批量扫描存活主机的主要内容,如果未能解决你的问题,请参考以下文章

nmap之快速探测内网的存活主机

Python 扫描存活主机

扫描神器--nmap

Linux下nmap扫描端口shell脚本

网络扫描工具nmap

kali之nmap