Bash script for checking connection / reconnect

Has anyone got a working bash script they could share for checking if a node is connected, and then reconnecting if it is not? I have found an example on the forum (below) but this one does not seem to work.

If anyone can share a working script, I would be very grateful!

73
Robert

date >> /usr/sbin/cron_check.txt
CHK=
echo “Checking if connected to the hub”

CHK= asterisk -rx "rpt nodes 537101" | grep -c "51288"
echo $CHK >> /usr/sbin/cron_check.txt

if [ $CHK -eq 1 ]
then
echo “OK, Connected.”
echo “OK, connected.” >> /usr/sbin/cron_check.txt
else
echo “Reconnecting.”
echo “Reconnecting.” >> /usr/sbin/cron_check.txt
asterisk -rx “rpt cmd 537101 ilink 3 51288”
fi

#End

It throws out the error:

“Checking if connected to the hub”
1
./autoconnect.sh: line 8: [: =: unary operator expected
“Reconnecting.”
No such command '“rpt' (type 'core show help “rpt' for other possible commands)

Try changing it to “rpt fun 537101 *351288” maybe? That’s what I would do, but you could probably remove the spaces so it’s “rpt cmd 527101 ilink351288” and I bet that might work too.

1 Like

This got me thinking and I run it through shellcheck. It was some formatting errors. Correct script is:

#!/bin/bash
date >> /usr/sbin/cron_check.txt
CHK=
echo "Checking if connected to the hub"

CHK= asterisk -rx "rpt nodes 537101" | grep -c "51288"
echo $CHK >> /usr/sbin/cron_check.txt

if [ "$CHK" = 1 ]
then
echo "OK, Connected."
echo "OK, connected." >> /usr/sbin/cron_check.txt
else
echo "Reconnecting."
echo "Reconnecting." >> /usr/sbin/cron_check.txt
asterisk -rx "rpt cmd 537101 ilink 3 51288"
fi

#End

Which is now working :slight_smile:

Just a follow up, there were still some errors. Chat GPT helpfully fixed them for me.

The correct script is:

#!/bin/bash

date >> /usr/sbin/cron_check.txt
CHK=$(asterisk -rx "rpt nodes 537101" | grep -c "51288")
echo "$CHK" >> /usr/sbin/cron_check.txt

echo "Checking if connected to hubnet"

if [ "$CHK" -eq 1 ]; then
    echo "OK, Connected."
    echo "OK, connected." >> /usr/sbin/cron_check.txt
else
    echo "Reconnecting."
    echo "Reconnecting." >> /usr/sbin/cron_check.txt
    asterisk -rx "rpt cmd 537101 ilink 13 51288"
fi

Here is a correct one that works no matter the system. I call it node_check.sh run it from any directory normally in the etc/asterisk/local directory

node_check yournodenumber nodetolookfor
ex:
./node_check.sh 457430 55553

#!/bin/bash
date >> /etc/asterisk/local/cron_check.txt
CHK=
echo “Checking if connected to the hub”

CHK=asterisk -rx "rpt nodes $1" | grep -c $2
echo $CHK >> /etc/asterisk/local/cron_check.txt

if [ $CHK -eq 1 ]
then
echo “OK, Connected.”
echo “OK, connected.” >> /etc/asterisk/local/cron_check.txt
else
echo “Reconnecting.”
echo “Reconnecting.” >> /etc/asterisk/local/cron_check.txt
asterisk -rx “rpt cmd $1 ilink 3 $2”
fi

ex run:

1 Like

This functionality is already built into app_rpt with permanent connections and startupmacro. Just curious, why complicate things with an external script?

it is to check to make sure you are connected. if the other end drops and you are not aware of it then startup_macro will not help you. especially if there is a node down for some unknown reason.

A permanent connection will continually retry a connection if it is ever dropped. If you do a startup macro for a permanent connection, then the script would not be needed. Asterisk would make sure the connection to the node is established whenever possible, forever.

This is true but permanent connections are only permanent dialing out. If they get disconnected from the other end, or some other hiccup, it won’t redial.

I have the start up permanent connection *813nnnnn as well as the script :grin:

That is generally true, but there is a limit.

This has been discussed in the forum before, but I can’t find it.

And keep in mind, it may not stand up to a reboot on the other end.

Ok, that is why I asked. I was wondering what I was missing :grin:

Without authority and thinking this through,

if the target dropped off the nodelist, (5-20min?)
It would not know how to connect anymore or that it was a valid node.
I think that would end it.

It would be the same as a active connect to a node that did not exist and the command would terminate.

It should persist in the case of network issues ‘between’ the two.
It still has reg data to do it…

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.