Testing connection with a script

I have a box that has to start dhcpd when a certain connection is down and stop the dhcpd when the connection is up again. I would appriciate any help making this script:

From server A:

ping server B
if connection is ok then do nothing
if connection is not ok then start dhcpd
until connection to B is up again

Regards kim jensen

Welcome to the forum !

This is just a hint and am not very thorough with this.

Ping works using ICMP and a host might be still up and not accepting/responding to ICMP packets, in such cases ping might not help with. Instead, check if the destination ip is reachable. If that is so, start the process from the local host where request is initiated.

Let us know, how it goes :slight_smile:

Hello there,
thank you for the reply! I guess I didn't make myself clear.
I only need to test if the connection is up the 'aliveness' of the B
server is uninteresting at this point.

I have tryed something like this:

#!/bin/bash
ping hostB -c3 -W3 > /dev/null
echo $? > A
if grep 0 /home/sipp/A > /dev/null
then exit;
elif
grep 1 /home/sipp/A
then
until grep 0 /home/sipp/A > /dev/null
do
[a command]
done
fi

my problem is that the loop keeps looping even if the connection is up again?

Regards KimJ

1 Like

No need to redirect output of $? to a file "A" and then grep from that.
Instead use it directly in 'if' block

if [ $? -eq 0 ];
then
# stuff to do
fi
1 Like

-thanks again
I do learn from this :slight_smile:

My plan is to execute the script every minute by a cronjob that works fine. But the thing is that I made an infinite loop and I don't know how to get out of it again when the connection is up again ?

you miss a lot about how does work a daemon...
I do too: AFAIK
a daemon is still listening, ever, at what ever could come to it by its port...

daPeach thank you for the reply
my aim is this:
B is primary dhcp server
if A server have connectivity to the B server then nothing
If A server have no connectivity to the B server, it should start dhcpd -until connection to B server is up again..

Modify your script something like this :

until grep 0 /home/sipp/A > /dev/null
do

[a command]

## Added here one again the ping statement to test the connection 
ping hostB -c3 -W3 > /dev/null
echo $? > A
if grep 0 /home/sipp/A > /dev/null
then exit;
fi 

done

I came up with this:
(instead of starting dhcpd I am testing with mahjongg)

It goes like this:
when there is no connection the script will start mahjongg
when the connection come up again mahjongg gets killed
that part works fine.
But when the connection is ok, the script tryes to kill a process that is not there.. it is supposed to exit. Can anyone with a sharp eye see where my error is?

Regards KimJensen

A=`ps auxww |grep /usr/games/mahjongg|grep -v grep |awk '{print $2}'` > /dev/null #process ID of mahjongg

ping 10.0.0.2 -c3 -W3 > /dev/null

if [[ $? -eq 1 && -z $A ]] #if connection error and mahjongg pid is empty
then /usr/games/mahjongg & #then start mahjongg
elif
[[ $? -eq 0 && -z $A ]] # if connection ok and mahjongg pid is empty
then exit #do nothing
else kill $A # else kill mahjongg pid
fi

---------- Post updated at 12:49 PM ---------- Previous update was at 08:47 AM ----------

I solved it like this:
#!/bin/bash
A=`ps auxww |grep /usr/games/mahjongg|grep -v grep|awk '{print $2}'`
ping 10.0.0.2 -c3 -W3 > /dev/null
B=$?
if [[ $B -eq 1 && -z $A ]]
then /usr/games/mahjongg &
elif
[[ $B -eq 1 && -n $A ]]
then exit
elif
[[ $B -eq 0 && -z $A ]]
then exit
else kill $A
fi

-not very pretty but it works :slight_smile: