我需要一些帮助与Linux外壳脚本,这应该会自动检查主机是否可以访问。最好每隔3-5秒ping一次端口22。如果端口可达,则程序应执行命令service HelloWorld stop。如果无法访问主机,脚本应自动在计算机上执行命令,例如service HelloWorld start。
有人知道如何实现这一点吗?
我有这样的东西,但那不起作用,
#!/bin/bash
IP='192.168.1.1'
fping -c1 -t300 $IP 2>/dev/null 1>/dev/null
if [ "$?" = 0 ]
then
service helloworld stop
else
service helloworld start
fi发布于 2019-03-11 15:38:44
尝试以下代码
#!/bin/bash
IP='192.168.1.1'
PORT=22
(echo >/dev/tcp/$IP/$PORT) &>/dev/null
if [ "$?" = 0 ]
then
service helloworld stop
else
service helloworld start
fi这样就可以检查特定端口的IP是否可达
https://stackoverflow.com/questions/55097072
复制相似问题