# Bash script for checking connection / reconnect

**URL:** <https://community.allstarlink.org/t/bash-script-for-checking-connection-reconnect/22080>\
**Category:** AllStarLink\
**Tags:** asl3\
**Created:** [October 18, 2024, 9:04am UTC](https://community.allstarlink.org/t/bash-script-for-checking-connection-reconnect/22080 "2024-10-18T09:04:49Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![G4ZWH](https://community.allstarlink.org/letter_avatar_proxy/v4/letter/g/779978/32.png) [@G4ZWH](https://community.allstarlink.org/u/G4ZWH)\
**Post date:** [October 18, 2024, 9:04am UTC](https://community.allstarlink.org/t/bash-script-for-checking-connection-reconnect/22080/1 "2024-10-18T09:04:49Z")

</div>

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

```auto
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:

```auto
“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)

```

---

<div class="post-metadata">

**Author:** ![KG5DRT](https://community.allstarlink.org/letter_avatar_proxy/v4/letter/k/77aa72/32.png) [@KG5DRT](https://community.allstarlink.org/u/KG5DRT)\
**Post date:** [October 18, 2024, 12:30pm UTC](https://community.allstarlink.org/t/bash-script-for-checking-connection-reconnect/22080/2 "2024-10-18T12:30:35Z")

</div>

> [@G4ZWH](#):
>
> ```auto
> t
> asterisk -rx “rpt cmd 537101 ilink 3 51288”
> 
> ```

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.

---

<div class="post-metadata">

**Author:** ![G4ZWH](https://community.allstarlink.org/letter_avatar_proxy/v4/letter/g/779978/32.png) [@G4ZWH](https://community.allstarlink.org/u/G4ZWH)\
**Post date:** [October 18, 2024, 2:04pm UTC](https://community.allstarlink.org/t/bash-script-for-checking-connection-reconnect/22080/3 "2024-10-18T14:04:10Z")

</div>

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

```auto
#!/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 🙂

---

<div class="post-metadata">

**Author:** ![G4ZWH](https://community.allstarlink.org/letter_avatar_proxy/v4/letter/g/779978/32.png) [@G4ZWH](https://community.allstarlink.org/u/G4ZWH)\
**Post date:** [October 19, 2024, 8:54am UTC](https://community.allstarlink.org/t/bash-script-for-checking-connection-reconnect/22080/4 "2024-10-19T08:54:26Z")

</div>

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

The correct script is:

```auto
#!/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

```

---

<div class="post-metadata">

**Author:** ![N8PC](https://community.allstarlink.org/letter_avatar_proxy/v4/letter/n/f04885/32.png) [@N8PC](https://community.allstarlink.org/u/N8PC)\
**Post date:** [October 19, 2024, 9:27am UTC](https://community.allstarlink.org/t/bash-script-for-checking-connection-reconnect/22080/5 "2024-10-19T09:27:55Z")

</div>

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:

---

<div class="post-metadata">

**Author:** ![N5LSN](https://community.allstarlink.org/user_avatar/community.allstarlink.org/n5lsn/32/2454_2.png) [@N5LSN](https://community.allstarlink.org/u/N5LSN)\
**Post date:** [October 19, 2024, 2:10pm UTC](https://community.allstarlink.org/t/bash-script-for-checking-connection-reconnect/22080/6 "2024-10-19T14:10:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![N8PC](https://community.allstarlink.org/letter_avatar_proxy/v4/letter/n/f04885/32.png) [@N8PC](https://community.allstarlink.org/u/N8PC)\
**Post date:** [October 19, 2024, 10:18pm UTC](https://community.allstarlink.org/t/bash-script-for-checking-connection-reconnect/22080/7 "2024-10-19T22:18:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![N5LSN](https://community.allstarlink.org/user_avatar/community.allstarlink.org/n5lsn/32/2454_2.png) [@N5LSN](https://community.allstarlink.org/u/N5LSN)\
**Post date:** [October 20, 2024, 12:25am UTC](https://community.allstarlink.org/t/bash-script-for-checking-connection-reconnect/22080/8 "2024-10-20T00:25:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![G4ZWH](https://community.allstarlink.org/letter_avatar_proxy/v4/letter/g/779978/32.png) [@G4ZWH](https://community.allstarlink.org/u/G4ZWH)\
**Post date:** [October 20, 2024, 6:48am UTC](https://community.allstarlink.org/t/bash-script-for-checking-connection-reconnect/22080/9 "2024-10-20T06:48:19Z")

</div>

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 😁

---

<div class="post-metadata">

**Author:** ![Mike](https://community.allstarlink.org/user_avatar/community.allstarlink.org/mike/32/1569_2.png) [@Mike](https://community.allstarlink.org/u/Mike)\
**Post date:** [October 20, 2024, 1:24pm UTC](https://community.allstarlink.org/t/bash-script-for-checking-connection-reconnect/22080/10 "2024-10-20T13:24:59Z")

</div>

> [@N5LSN](#):
>
> A permanent connection will continually retry a connection if it is ever dropped.

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.

---

<div class="post-metadata">

**Author:** ![N5LSN](https://community.allstarlink.org/user_avatar/community.allstarlink.org/n5lsn/32/2454_2.png) [@N5LSN](https://community.allstarlink.org/u/N5LSN)\
**Post date:** [October 20, 2024, 2:18pm UTC](https://community.allstarlink.org/t/bash-script-for-checking-connection-reconnect/22080/11 "2024-10-20T14:18:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mike](https://community.allstarlink.org/user_avatar/community.allstarlink.org/mike/32/1569_2.png) [@Mike](https://community.allstarlink.org/u/Mike)\
**Post date:** [October 20, 2024, 10:29pm UTC](https://community.allstarlink.org/t/bash-script-for-checking-connection-reconnect/22080/12 "2024-10-20T22:29:46Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![system](https://community.allstarlink.org/uploads/default/original/2X/4/42700284e201b031bd667c3f63bbae05a74b3104.png) [@system](https://community.allstarlink.org/u/system)\
**Post date:** [October 23, 2024, 10:30pm UTC](https://community.allstarlink.org/t/bash-script-for-checking-connection-reconnect/22080/13 "2024-10-23T22:30:07Z")

</div>

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