Small function Big use----One click to detect if each server is alive

Source: Internet
Author: User
Tags server port telnet program egrep

At work, you will encounter a scenario that determines if the server is alive, such as whether the host or virtual machine starts successfully after a batch restart of the server.

The principle of detecting whether a server is alive is simple: first check if the server is on (ping), if the general rule checks the service on the server, if the service is not available, the server is not turned on. The tools you can use are: Ping, Telnet, nmap, or tcping.

Nmap is powerful and widely used; tcping can check if the machine is alive if the server is not pinging (for example, the Windows machine defaults to ping is forbidden). Here, I'm using a combination of ping and telnet.

About the Ping tool, reasonable use of relevant parameters is helpful to troubleshoot problems, such as: "-C" parameter can be used to limit the number of pings sent Ping, "-i" parameter can be used to limit the frequency of ping and so on. Example:

ping-c2-i0.02 www.baidu.com >/dev/null 2>&1

The previous code means: Ping www.baidu.com 2 times at 0.02-second intervals and introduce the result (right or wrong) to the "Black hole" (discard), and reasonable use of parameters can help us to quickly determine whether the target host is up or down. Of course, someone will ask: "You put the test results into a black hole, how do I know if the target host is a pass or not?" This time can be judged by the operation state that the command is:

echo $?

If the above results return a value of 0, indicating that www.baidu.com can be reached, otherwise www.baidu.com may not pass, need to proceed to the next judgment. Some people say that by manually executing command trouble, can you be more sophisticated? Then write it as a script, as follows:

#!/bin/bash# test.ping-c2-i0.02 www.baidu.com >/dev/null 2>&1if [$?-eq 0];then    echo "Host can access. ... "Else    ... fi

Explain the above script, if the host is not available, then perform subsequent actions, these follow-up actions in the "Else" code block, follow-up action can use Nmap, Telnet and other tools, here to introduce Telnet.

Some would say that Telnet is easy, if the Telnet program is stuck there, it means that the server port is not working, and then the "CTRL + C" Termination is OK, but how to use Telnet in the script? Simple, through the pipeline function of Linux ("|" ), allowing the script to sleep for a period of time, during which Telnet executes the task, and the Telnet program terminates until the sleep time expires. That being said, how to achieve it? As follows:

Determine if a server is alive, you can ping the IP, then telnet port, if you are testing an IP, you can write the script as follows:

#!/bin/bash# test.host= www.baidu.comport=80telnet_file=/tmp/telnet_file.txtping-c2-i0.02 $host >/dev/null 2> &1if [$?-eq 0];then    echo "$host can access ..." Else    (sleep 1;) | Telnet $host $port > $telnet _file    grep-b 1 \] $telnet _file >/dev/null 2>&1    if [$?-eq 0];then
  
   echo "$host can access ..."    else        echo "$host can not access!!!"
   
Fifi

The above script is for an IP to judge, to tell the truth if it is to determine whether a server can reach the direct terminal knocking command to know, do not need "big hassle" to write scripts, but if you test multiple machines at the same time need to use the script. In order to write a script to determine whether multiple hosts can be reached, it is only needed to sort out the above ideas and use the Linux basic commands flexibly. Of course, if the target machine is very numerous, it is recommended to use ansible or saltstack, or simply write a Python multi-process script to judge.

The following is a list of the scripts I wrote earlier based on the ping and Telnet tools, and the approximate principle is similar to the above, before you execute the script, edit the server configuration file.

Execute script:

renxiaowei$ Cat check.sh#!/bin/bash# Features: One-click detection of host, Virtual machine survival # Any small # version: V1.0file=server.txtresult_base=/tmp/check_host_ NETRM-RF $result _basemkdir-p $result _basesuper_host () {super_ip_list=${result_base}/super_ip_list.txt super_alive= ${result_base}/super_alive.txt Super_die=${result_base}/super_die.txt Super_telnet=${result_base}/super_ Telnet.txt echo "ECHO-E" \033[44;36m detects if the host is alive ... \033[0m "egrep-v" #|^$ "$file | awk ' {print $1,$2} ' | Uniq > $super _ip_list count=0 for IP in $ (cat ${super_ip_list} | awk ' {print $} ');d o let count++ E  Cho "count= $count" ping-c 2-i 0.02 $ip >/dev/null 2>&1 if [$?-eq 0];then Echo $ip >> ${super_alive} else super_port= ' grep $ip ${super_ip_list} | awk ' {print $} ' (sleep 1;) | telnet $ip ${super_port} >> ${super_telnet} fi done if [-e $ {super_telnet}]; Then Super_ip=${result_base}/super_ip.txt cat ${super_iP_list} | awk ' {print '} ' > ${super_ip} cat ${super_telnet} | Grep-b 1 \] | grep [0-9] | awk ' {print $} ' | Cut-d '. '-F 1,2,3,4 >> ${super_alive} Cat ${super_ip} ${super_alive} | Sort | Uniq-u > ${super_die} fi echo "" Echo-e "\033[32;49;1m Live Host: \033[31;49;0m" Echo-e "' Cat ${super_alive} ' \ n ' If [-s ${super_die}];then echo-e ' \033[31;49;1m detected host: \033[31;49;0m ' ECHO-E ' \033[31;49;1m ' Cat ${super_die} ' \033[31;49;0m\n ' else echo-e ' \033[44;36m all host hosts are alive. \033[0m\n "Fi}sub_host () {sub_ip_list =${result_base}/sub_ip_list.txt sub_alive=${result_base}/sub_alive.txt sub_die=${result_base}/sub_die.txt Sub_tel Net=${result_base}/sub_telnet.txt echo "ECHO-E" \033[44;36m detects if the virtual machine is alive ... \033[0m "egrep-v" #|^$ "$file | awk ' {print $3,$4} ' |        Uniq > $sub _ip_list sub_count=0 for SUB_IP in $ (cat ${sub_ip_list} | awk ' {print $} ');d o let sub_count++      echo "Count=${sub_count}"  Ping-c 2-i 0.02 ${sub_ip} >/dev/null 2>&1 if [$?-eq 0];then Echo ${sub_ip} >> ${ sub_alive} else sub_port= ' grep ${sub_ip} ${sub_ip_list} |  awk ' {print $} ' (sleep 1;) | Telnet ${sub_ip} ${sub_port} >> ${sub_telnet} fi done if [-E        ${sub_telnet}];then sub_ip=${result_base}/sub_ip.txt cat ${sub_ip_list} |awk ' {print $} ' > ${sub_ip} Cat ${sub_telnet} | Grep-b 1 \] | grep [0-9] | awk ' {print $} ' | Cut-d '. '-F 1,2,3,4 >> ${sub_alive} Cat ${sub_ip} ${sub_alive} | Sort |     Uniq-u > ${sub_die} fi echo "" Echo-e "\033[32;49;1m surviving virtual machine: \033[31;49;0m" Echo-e "' Cat ${sub_alive} ' \ n ' If [-S ${sub_die}];then echo-e "\033[31;49;1m virtual machine not detected: \033[31;49;0m" echo-e "\033[31;49;1m ' Cat ${sub         _die} ' \033[31;49;0m\n ' else echo-e ' \033[44;36m all virtual machines are alive. \033[0m\n "Fi}case" in Super) Super_host    ;; VM) Sub_host;;        Help) echo "" echo "grep" function "$file '" echo "super option represents the host for the inspection;"    Echo-e "VM option represents detecting virtual machines. \ n"; *) echo "" Echo-e $ "usage:sh $ {Super | VMS |  Help}\n "Esacexit 0

  

Server configuration file Format:

renxiaowei$ cat Server.txt #功能: Used for one-click detection of host, virtual machine survival File # format: VM host------Host management port----virtual machine----virtual Machine management port 192.168.2.2           80            192.168.2.50     192.168.2.3            192.168.2.51     22192.168.3.2            192.168.3.52     22

Please put the script, server configuration file in the same directory.

Script Execution Mode: chmod + x check.sh &&./check.sh [super|vm|help] or SH check.sh [super|vm|help].

The script parameter "super" means the host detection, the parameter "VM" means the virtual machine detection.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.