Reputation: 161
I need some help with a Linux shell script which should automatically check if a host is reachable. It is best to ping port 22 every 3-5 seconds. If the port is reachable the program should execute the command service HelloWorld stop. If the host is not reachable, the script should automatically execute a command on the computer e.g. service HelloWorld start.
Does anyone know how to implement this?
I have something like this, but that not functionning,
#!/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
Upvotes: 0
Views: 407
Reputation: 325
Try the following code
#!/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
In this way, it's allowed to check IP with specific port is reachable or not
Upvotes: 1