1. 程式人生 > 實用技巧 >主機存活檢測、區域網主機存活檢測、埠檢測

主機存活檢測、區域網主機存活檢測、埠檢測

零、使用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
 

一、主機檢測命令ping

#!/bin/bash                                                                                                                                                                                 
#****************************************************
#Date:        2020-06-25
#Author:      Damon Ye
#FileName:   MailPing.sh
#Description:The test script
#****************************************************
red="\033[31m"
green="\033[32m"
shutdown="\033[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

二、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

三、埠檢測

#!/bin/bash
#****************************************************
#Date:        2020-06-25
#Author:      Damon Ye
#FileName:   PortLook.sh
#Description:The test script
#****************************************************
shutdown="\033[0m"
green="\033[32m"
blue="\033[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 "\n\n"
   fi
done