Search code examples
bashscriptingcommandunify

Execute system commands in bash script - command not found


I have the following script: getip.sh

#!/bin/bash
int_ip_addr=`/sbin/ifconfig pppoe0 | grep 'inet addr:' | cut -d: -f2| cut -d' ' -f1`
pb_ip_addr=`curl ipinfo.io/ip`
echo "Internal IP Address is $int_ip_addr"
echo "External IP Address is $pb_ip_addr"
if [ "$int_ip_addr" == "$pb_ip_addr" ]; then
    echo "PPPoE IP is Public - $int_ip_addr"; 
else 
    echo "PPPoE IP is not Public - $int_ip_addr";
    disconnect interface pppoe0; connect interface pppoe0
fi

It works well except for the execution of the commands "disconnect interface pppoe0; connect interface pppoe0"

Disconnect and connect have no path if searched with whereis. The commands work fine when using them in the terminal but once i pute them in the script i get this:

./tmp/getip.sh: line 10: disconnect: command not found ./tmp/getip.sh: line 10: connect: command not found

I tried breaking the commands down or just trying to execute them stand alone but for some reason those commands are simply unavailable for bash.

This is on a linux file system on a USG-3P router from unify.

The commands are meant to disconnect and reconnect the PPPoE Interface.

The whole scripts purpose is to check if the internal PPPoE interface IP matches the externally seen IP or if it does not. This is a test to see if i got a CGNAT IP or a PUBLIC IP.

I'd like to use the script to reconnect until i get a PUBLIC IP.


Solution

  • I managed to find the culprit, it appears that the device has some obfuscation protection for these commands, some wrapper of sorts, i eventually found it via the forums.

    Here is the final version of the script with a loop that reconnects until it gets a matching IP address.

    #!/bin/bash
    int_ip_addr=`/sbin/ifconfig pppoe0 | grep 'inet addr:' | cut -d: -f2| cut -d' ' -f1`
    pb_ip_addr=`curl ipinfo.io/ip`
    echo "Internal IP Address is $int_ip_addr"
    echo "External IP Address is $pb_ip_addr"
    while [ "$int_ip_addr" != "$pb_ip_addr" ]; do
        echo "PPPoE IP is not Public - $int_ip_addr";
        /opt/vyatta/bin/vyatta-op-cmd-wrapper disconnect interface pppoe0; 
        /opt/vyatta/bin/vyatta-op-cmd-wrapper connect interface pppoe0;
        echo "Sleeping for 30 seconds"
        sleep 30s
        int_ip_addr=`/sbin/ifconfig pppoe0 | grep 'inet addr:' | cut -d: -f2| cut -d' ' -f1`
        pb_ip_addr=`curl ipinfo.io/ip`
        echo "Internal IP Address is $int_ip_addr"
    done 
    echo "PPPoE IP is Public - $int_ip_addr";