工做中会遇到判断服务器是否存活的场景,好比:批量重启服务器后,各宿主机或虚拟机是否启动成功。python
检测服务器是否存活的原理很简单:先检查服务器是否通(ping);若是不通则检查服务器上的服务是否通,若是服务也不通,则说明这台服务器没有开启。可使用的工具备:ping、telnet、nmap或者tcping等。linux
nmap功能强大,用处普遍;tcping能够在服务器禁ping的状况下检查机器是否存活(好比windows机器默认就是禁ping的)。在这里,我用的是ping和telnet组合。windows
关于ping工具,合理的使用相关参数比较有利于排查问题,好比:"-c"参数能够用来限制发送ping多少个包,"-i"参数能够用来限制ping的频率等。举例:bash
ping -c2 -i0.02 www.baidu.com > /dev/null 2>&1
上一段代码意思为:以0.02秒的间隔时间ping www.baidu.com 2次,并将结果(正确或错误)引入到“黑洞”(丢弃),合理的使用参数能够帮助咱们快速判断目标主机是否可达。固然,会有人问:你将测试结果引入到黑洞,我怎么知道目标主机是通仍是不通呢?这个时候能够经过判断操做状态得知,命令为:服务器
echo $?
若是上面的结果返回值为0,说明www.baidu.com可达,不然www.baidu.com可能不通,须要进行下一步再判断。有人说,经过手动执行命令麻烦,能否再精巧些?那就写成脚本吧,以下:tcp
#!/bin/bash # test. ping -c2 -i0.02 www.baidu.com > /dev/null 2>&1 if [ $? -eq 0 ];then echo "Host can access ...." else ... fi
解释上面那段脚本,若是主机不通,则执行后续动做,这些后续动做放在"else"代码块中,后续动做可使用nmap、telnet等工具,这里介绍telnet。工具
有人会说,telnet容易,若是telnet程序一直卡在那里则说明服务器端口不通,而后经过“CTRL+c”终止就好了,可是怎样在脚本中使用telnet呢?简单,经过Linux的管道功能(“|”),可让脚本沉睡一段时间,这段时间内telnet执行任务,待沉睡时间到期则终止telnet程序。话虽如此,怎样实现呢?以下:测试
(sleep 1;) | telnet www.baidu.com 80
判断某台服务器是否存活,能够先ping IP,再telnet port,若是是测试一个IP,则可将脚本写成以下:code
#!/bin/bash # test. host= www.baidu.com port=80 telnet_file=/tmp/telnet_file.txt ping -c2 -i0.02 $host > /dev/null 2>&1 if [ $? -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 !!!"
fi fi
上面的脚本是针对一个IP进行判断,说实话若是是判断一台服务器是否可达的话直接终端敲命令就知道了,不须要“大费周折”写脚本了,可是若是同时测试多台机器就须要用到脚本了。对于编写批量判断多台主机是否可达的脚本,只须要将上述思路稍做整理,灵活使用linux基础命令便可实现。固然,若是目标机器很是多的状况下,推荐使用ansible或者saltstack,或者干脆写个python多进程的脚本去判断。server
下面贴出我以前基于ping和telnet工具写的脚本,大体原理和上面讲述的类似,在执行脚本前,请先编辑服务器配置文件。
执行脚本:
renxiaowei$ cat check.sh #!/bin/bash # 功能:一键检测宿主机、虚拟机是否存活 # 做者:任小为 # 版本:v1.0 file=server.txt result_base=/tmp/check_host_net rm -rf $result_base mkdir -p $result_base super_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检测宿主机是否存活... \033[0m" egrep -v "#|^$" $file | awk '{print $1,$2}' | uniq > $super_ip_list count=0 for ip in $(cat ${super_ip_list} | awk '{print $1}');do let count++ echo "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 $2}'` (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 $1}' > ${super_ip} cat ${super_telnet} | grep -B 1 \] | grep [0-9] | awk '{print $3}' | 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存活的宿主机: \033[31;49;0m" echo -e "`cat ${super_alive}`\n" if [ -s ${super_die} ];then echo -e "\033[31;49;1m检测不通的宿主机:\033[31;49;0m" echo -e "\033[31;49;1m`cat ${super_die}`\033[31;49;0m\n" else echo -e "\033[44;36m全部宿主机都存活.\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_telnet=${result_base}/sub_telnet.txt echo "" echo -e "\033[44;36m检测虚拟机是否存活... \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 $1}');do 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 $2}'` (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 $1}' > ${sub_ip} cat ${sub_telnet} | grep -B 1 \] | grep [0-9] | awk '{print $3}' | 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存活的虚拟机: \033[31;49;0m" echo -e "`cat ${sub_alive}`\n" if [ -s ${sub_die} ];then echo -e "\033[31;49;1m检测不通的虚拟机:\033[31;49;0m" echo -e "\033[31;49;1m`cat ${sub_die}`\033[31;49;0m\n" else echo -e "\033[44;36m全部虚拟机都存活.\033[0m\n" fi } case $1 in super) super_host ;; vm) sub_host ;; help) echo "" echo "`grep "功能" $file`" echo "super选项表明检测宿主机;" echo -e "vm选项表明检测虚拟机.\n" ;; *) echo "" echo -e $"Usage: sh $0 { super | vm | help }\n" esac exit 0
服务器配置文件格式:
renxiaowei$ cat server.txt #功能:用于一键检测宿主机、虚拟机是否存活的文件 #格式:vm宿主机 ------ 宿主机管理端口 ---- 虚拟机 ---- 虚拟机管理端口 192.168.2.2 80 192.168.2.50 22 192.168.2.3 80 192.168.2.51 22 192.168.3.2 80 192.168.3.52 22
请将脚本、服务器配置文件放在同一个目录下。
脚本执行方式:chmod + x check.sh && ./check.sh [super|vm|help] 或者 sh check.sh [super|vm|help] 。
脚本参数“super”意为宿主机检测;参数“vm”意为虚拟机检测。