主机存活检测、端口检测

一、ping

  1.主机存活检测

#!/bin/bash                                                                                                                                                                                 
#****************************************************
#Date:        2020-06-25
#Author:      Damon Ye
#FileName:   MailPing.sh
#Description:The test script
#****************************************************
red="33[31m"
green="33[32m"
shutdown="33[0m"
read -p "请输入你的ip地址:" ipadress
ping -W1 -c3 $ipadress &>/dev/null
if [ $? -eq 0 ];then
  echo -e "$ipadress is ${green}running${shutdown}......"
else
  echo -e "$ipadress is ${red}stopped${shutdown}......"
fi

  2.掩码24的局域网主机存活检测

for ip in {1..254}
  do
    ping -w0.1 -W1 -c1 10.0.0.${ip} &>/dev/null
    if [ $? -eq 0 ];then
      echo -e "10.0.0.${ip} is ${green}running${shutdown}......"
    else
      echo -e "10.0.0.${ip} is ${red}stopped${shutdown}......"
    fi
done

二、改进——使用并行ping

上面的脚本是依次测试地址的,每次测试积累下的延迟时间很多。我们可以利用并行方式来提高整体执行速度。

要使ping并行执行,可以将do循环体放入( )&中,( )中的命令会在子shell中运行,而&会将其置入后台运行。

**********************************
red="33[31m"
green="33[32m"
shutdown="33[0m"

for ip in {1..254}
  do
  (
    ping -w0.1 -W1 -c1 10.0.0.${ip} &>/dev/null
    if [ $? -eq 0 ];then
      echo -e "10.0.0.${ip} is ${green}running${shutdown}......"
    else
      echo -e "10.0.0.${ip} is ${red}stopped${shutdown}......"
    fi
   )&
done
wait  

三、改进——使用工具fping

fping可以为多个IP地址生成ICMP分组,然后等待回应,其运行速度要比上面的脚本快得多

http://www.fping.org/

fping选项说明:

  -a显示所有主机

  -u显示所有不可达的主机

  -g指定IP地址范围,格式为“IP地址/子网掩码”或者“IP地址范围”

  2>/dev/null用于将主机不可达所产生的错误信息输出到null设备

  <FileName从文件中传递一组IP地址

[root@localhost package]# wget http://www.fping.org/dist/fping-4.2.tar.gz
[root@localhost package]# tar  -xzvf fping-4.2.tar.gz 
[root@localhost package]# cd fping-4.2/
[root@localhost fping-4.2]# ./configure && make && make install
[root@localhost fping-4.2]# fping -a -g 10.0.0/24
10.0.0.1
10.0.0.10
10.0.0.2
10.0.0.24
10.0.0.21
10.0.0.28
10.0.0.22
10.0.0.41
10.0.0.50
10.0.0.46
ICMP Host Unreachable from 10.0.0.10 for ICMP Echo sent to 10.0.0.4
ICMP Host Unreachable from 10.0.0.10 for ICMP Echo sent to 10.0.0.4
ICMP Host Unreachable from 10.0.0.10 for ICMP Echo sent to 10.0.0.7
ICMP Host Unreachable from 10.0.0.10 for ICMP Echo sent to 10.0.0.7
ICMP Host Unreachable from 10.0.0.10 for ICMP Echo sent to 10.0.0.9
ICMP Host Unreachable from 10.0.0.10 for ICMP Echo sent to 10.0.0.9
ICMP Host Unreachable
[root@localhost fping-4.2]# fping -a -g  10.0.0/24  2>/dev/null
10.0.0.1
10.0.0.10
10.0.0.11
10.0.0.22
10.0.0.21
10.0.0.24
10.0.0.2
10.0.0.28
10.0.0.29
10.0.0.41
10.0.0.50
10.0.0.46
10.0.0.71
[root@localhost fping-4.2]# fping -a -g  10.0.0.1  10.0.0.50  2>/dev/null
10.0.0.1
10.0.0.10
10.0.0.24
10.0.0.21
10.0.0.22
10.0.0.28
10.0.0.2
10.0.0.41
10.0.0.50
10.0.0.46
[root@localhost package]# vim IPadress.txt
10.0.0.1
10.0.0.2
10.0.0.3
10.0.0.4
10.0.0.5                                                                                                                                                                                     
[root@localhost package]# fping -a  < IPadress.txt 
10.0.0.1
10.0.0.2
ICMP Host Unreachable from 10.0.0.10 for ICMP Echo sent to 10.0.0.4
ICMP Host Unreachable from 10.0.0.10 for ICMP Echo sent to 10.0.0.4
ICMP Host Unreachable from 10.0.0.10 for ICMP Echo sent to 10.0.0.4
ICMP Host Unreachable from 10.0.0.10 for ICMP Echo sent to 10.0.0.4
 

四、端口检测

#!/bin/bash
#****************************************************
#Date:        2020-06-25
#Author:      Damon Ye
#FileName:   PortLook.sh
#Description:The test script
#****************************************************
shutdown="33[0m"
green="33[32m"
blue="33[34m"
for ip in {1..254}
 do
   ping -c1 -W1 -w0.1 10.0.0.${ip} &> /dev/null
   if [ $? -eq 0 ];then                                                                                                                                                                     
     echo -e "${green}#################################################${shutdown}"
     echo "Host 10.0.0.${ip} runs on the following ports. "
     nmap 10.0.0.$ip | sed -n '/^PORT/,/^MAC/p'| sed -n '/^[0-9]/p'
     echo -e "${blue}#################################################${shutdown}"
     echo -e "

"
   fi
done
原文地址:https://www.cnblogs.com/ytdyz/p/13192696.html