Shell脚本检查Ping到远程主机和端口是否打开

时间:2020-03-05 15:29:09  来源:igfitidea点击:

使用此Shell脚本,您可以对远程主机执行ping操作,并检查是否在该服务器上打开了提到的端口。

这有助于系统管理员执行ping测试,并确保特定端口是否存在任何问题。

ping shell脚本

#!/bin/bash
if [ "$#" = "0" ];
then
echo "Usage: 
bobbin@theitroad:/$./script 127.0.0.1 22
Ping to host - OK, port 22 not opened.
desktop:~/$
" exit 1 fi host= port= email="[email protected]" subject="Script result" if ping -q -c 4 $host >/dev/null then ping_result="OK" else ping_result="NOT OK" fi nc_result=`nc -z $host $port; echo $?` if [ $nc_result != 0 ]; then port_result="not opened" else port_result="opened" fi message="Ping to host - ${ping_result}, port $port ${port_result}." if [ "$ping_result" != "OK" -o "$nc_result" != "0" ]; then echo "$message" echo "$message" | mail -s "$subject" $email fi

脚本输出

ping to localhost并检查为22端口(SSH服务器)

##代码##