I have created a small shell script with the help of internet searches. I am still very new to scripting. So my script looks like this
#!/bin/sh
# pppoe-wan connect check
logger "Starting internet check script..."
IP=""
internet_func ()
{
IP="$(ip addr show pppoe-wan | grep -oE '\b([0-9]{1,3}\.){3}[0-9]{1,3}\b')"
logger "function says:" $IP
}
logger "trying to run the IP function..."
internet_func
if [$IP != ""]
then
{
logger "Network is connected - no need to reset."
}
elif [$IP = ""]
then
{
logger "Network is down. Resetting the network, hold on!"
/etc/init.d/network restart
}
fi
logger "Finished internet check script."
Well my script does not work well at the moment and I dont know what else do i need to do in there to make it work. I have already done chmod +x script.sh
to make it runnable. I can run the script just fine but the internet_func
doesn't really work though. The variable IP
doesn't get the value from the command that it is supposed to.
For example if i stop the pppoe-wan
interface then the command returns "pppoe-wan does not exist." but it should return an empty string because it did not find the IP. To explain the script, I want to only get the IP from the command and if the command returns the IP the network is connected but if it returns an empty string then the network is down.
Can someone help me figure out how can i make it work?
Update:
I kinda updated the script a little bit and now the If-else part doesn't work as expected. The IP gets two values xx.xx.xx.xx xx.xx.xx.xx
but the if else statement never gets executed. Any pointers?
Output from log:
Wed Feb 28 13:49:56 2018 user.notice root: Starting internet check script...
Wed Feb 28 13:49:56 2018 user.notice root: trying to run the IP function...
Wed Feb 28 13:49:56 2018 user.notice root: function says: 39.40.204.13 182.176.1.48
Wed Feb 28 13:49:56 2018 user.notice root: Finished internet check script.
The output in CLI:
root@AhmarRouter:/data# ./adslpppoe.sh
./adslpppoe.sh: line 28: [39.40.204.13: not found
./adslpppoe.sh: line 28: [39.40.204.13: not found