depauw
Posts: 44
Joined: Mon Nov 09, 2015 2:12 pm

network status script: synthax error

Wed Jan 11, 2017 4:34 pm

Hello there,
I've written a script to check if my pi has an internetconnection by pinging Google.
If not, it checks what type of connection I have (wifi and/or ethernet).
It restarts them 5 times.
If there is still no connection, the a reboot is executed.

When I run the code, I get a synthax error: synthax error, unexpected symbol 'done'

Code: Select all

#!/bin/bash

for i in {1..5}
do
   echo "Test nr $i"
   ping -c4 8.8.8.8 > /dev/null

   if [ $? != 0 ] 
   then
     echo "test connection type: wlan0 or eth0"
     ifconfig wlan0 | grep -i "inet addr:" | wc -l
	 
     if [ $? != 0 ]
     then 
        echo "wlan0 connection detected"
        if [ $i -lt 5 ]
        then 
          echo "No internet connection, restarting wlan0"
          ifconfig wlan0 down
          sleep 5
          ifconfig wlan0 up

     else 
        ifconfig eth0 | grep -i "inet addr:" | wc -l
        if [ $? != 0 ]
        then
           echo "eth0 connection detected"
           if [ $i -lt 5 ]
           then
              echo "No internet connection, restarting eth0"
              ifconfig eth0 down
              sleep 5
              ifconfig eth0 up

     else 
       echo "No network connection, reboot"
       sleep 5
       sudo /sbin/shutdown -r now
     fi
   else
      echo "Connection ok!"
   fi
done
I hope someone can point me to what I'm doing wrong!

greetings,
Christophe

User avatar
PeterO
Posts: 4939
Joined: Sun Jul 22, 2012 4:14 pm

Re: network status script: synthax error

Wed Jan 11, 2017 4:41 pm

At a first look you are missing some "fi"s

Five x "if" but only Two x "fi"

Looks at the examples http://www.tldp.org/LDP/abs/html/testconstructs.html

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
B.Goode
Posts: 8217
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: network status script: synthax error

Wed Jan 11, 2017 5:44 pm

In addition to what @PeterO has said -

This query has erroneously been posted in the Python sub-forum. (That itself is trivial and can be rectified.)

But if you are familiar with Python note that programming constructs like loops and conditionals in Shell script use specific keywords like done to terminate the loop, they do not rely on left hand indentation (which you have used a lot in a python-intro style.)

Just curious whether you might have made that assumption that has led to the if statements not being terminated?

The reference provided should make this clear.

Edit: a Moderator has now relocated this discussion to an appropriate sub-forum

depauw
Posts: 44
Joined: Mon Nov 09, 2015 2:12 pm

Re: network status script: synthax error

Wed Jan 25, 2017 2:47 pm

Hey, sorry for the delay. Dayjob got a bit in the way

Thanks for the pointers and the links.
I started this script out as a python script, but then made a bash script of it. This explains a bit why there's a lot of python in it.
I've done some reading an googling and took another approach to my script.
The goal is still the same:
- check if there is an internetconnection (ping google)
- see what type of connection there is (wlan and/or ethernet)
- restart the active adaptor.
- if no result after 3 attempts: reboot

So, I've come up with this.
Something must be wrong, because I'm not getting the results I'm expecting.
You'll see there's a lot of 'echo' in the script (for debugging purpose) and there are no active commands in it. That is for later ;)

Really appreciate your help!!!

Code: Select all

#!/bin/bash 

wlan=$(ifconfig wlan0 | grep -i "inet addr:" | wc -l)
echo $wlan
eth=$(ifconfig eth0 | grep -i "inet addr:" | wc -l)
echo $eth

for i in {1..3}
do
        echo "test $i"
        ping -c4 7.8.8.8 > /dev/null

        echo $?

        if [ $? != 1 ]
        then
                echo "connection ok"

        elif [ $? != 0 ] && [ $wlan != 0 ]
        then
                echo "no connection, wlan detected. Restarting wlan adaptor"

        elif [ $? != 0 ] && [ $eth != 0 ]
        then
                echo "no connection, eth detected. Restarting eth adaptor"
        fi
done
echo "last check before reboot"
ping -c4 7.8.8.8 > /dev/null
echo $?
if [ $? != 0 ] 
then
        echo "reboot"   
fi
Last edited by depauw on Thu Jan 26, 2017 10:28 am, edited 1 time in total.

User avatar
B.Goode
Posts: 8217
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: network status script: synthax error

Wed Jan 25, 2017 3:17 pm

Something must be wrong, because I'm not getting the results I'm expecting.
Give us a clue...

What results do you get? How does that differ from what you were expecting?

depauw
Posts: 44
Joined: Mon Nov 09, 2015 2:12 pm

Re: network status script: synthax error

Thu Jan 26, 2017 10:32 am

Did some editing to the posted code, it was a wrong version.
These are the results I get:
1
1
test 1
1
connection ok
test 2
1
connection ok
test 3
1
connection ok
last check before reboot
1

I was not expecting to get a connection ok. It should be a no connection, first on wlan with restart and then on eth with restart.
maybe something is wrong with the way i'm storing the result of the ping test?

shuckle
Posts: 565
Joined: Sun Aug 26, 2012 11:49 am
Location: Finland

Re: network status script: synthax error

Thu Jan 26, 2017 12:07 pm

$? is the status of the last command.
After
echo $?
it shows the status of echo, which is pretty often 0.

depauw
Posts: 44
Joined: Mon Nov 09, 2015 2:12 pm

Re: network status script: synthax error

Sun Jan 29, 2017 6:52 pm

ok!
Now, I want to write the result of the ping test (0 or 1) into a variable.
I've tried this: png=$(ping -c4 7.8.8.8 > /dev/null)
but when I do 'echo $png' the output is nothing, of blank.

any idea's on this?

kind regards,
Christophe

shuckle
Posts: 565
Joined: Sun Aug 26, 2012 11:49 am
Location: Finland

Re: network status script: synthax error

Sun Jan 29, 2017 7:21 pm

ping -c4 7.8.8.8 > /dev/null
png=$?

depauw
Posts: 44
Joined: Mon Nov 09, 2015 2:12 pm

Re: network status script: synthax error

Sun Feb 05, 2017 7:44 pm

@ shuckle: thanks for that! :oops:
really embarressed I didn't found this on my own.....

Back on track now!
I'll publish the finished code within the next few days, if the dayjob doesn't have to many surprises :o

Christophe

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: network status script: synthax error

Sun Feb 05, 2017 9:24 pm

depauw wrote: I've tried this: png=$(ping -c4 7.8.8.8 > /dev/null)
but when I do 'echo $png' the output is nothing, of blank.
any idea's on this?
I know you've got an answer but there are two issues, one is that the google server is at 8.8.8.8 so that will fail after some time and won't test if your connection is actually up.

But the reason you are getting a blank back in your variable is because of the /dev/null . Even if you really just wanted the success/failure value and didn't care to parse out, say, the time the ping took, it makes sense I think to not put in the > /dev/null until you have things at least working up to that point. Without that null dump, you'd have got the output in the variable or right out on the screen and then immediately realised the trouble.

depauw
Posts: 44
Joined: Mon Nov 09, 2015 2:12 pm

Re: network status script: synthax error

Mon Feb 06, 2017 10:06 am

I used 7.8.8.8 for testing purposes so I could simulate the situation of no connection.
In the final script (below) I'm using 8.8.8.8

So here is my result:
- first check: is there connection with internet
- second check: what kind of connection: wifi or ethernet
- depending on the type of connection: the appropriate adapter is reset, 3 attempts are made
- if connection is not restored: a reboot is done

Code: Select all

#!/bin/bash 

ip='8.8.8.8' #ip you want to ping for connection. 8.8.8.8 is google
wlan=$(ifconfig wlan0 | grep -i "inet addr:" | wc -l)
echo $wlan #0 means no wifi
eth=$(ifconfig eth0 | grep -i "inet addr:" | wc -l)
echo $eth #0 means no ethernet

for i in {1..4}
do
        echo "test $i"
        ping -c4 $ip > /dev/null
	png=$?
        echo $png

        if [ $png = 0 ] && [ $i -le 3 ] 
        then
                echo "connection ok"
		break #there is no need to do more tests when the connection is ok 
		
        elif [ $png != 0 ] && [ $wlan != 0 ] && [ $i -le 3 ]
        then
                echo "no connection, wlan detected. Restarting wlan adaptor"
		ifconfig wlan0 down
          	sleep 5
          	ifconfig wlan0 up

	elif [ $png != 0 ] && [ $eth != 0 ] && [ $i -le 3 ]
        then
                echo "no connection, eth detected. Restarting eth adaptor"
		ifconfig eth0 down
              	sleep 5
              	ifconfig eth0 up
 
	elif [ $png != 0 ] && [ $i -eq 4 ] 
        then
               	echo "no connection at all: reboot"
		sleep 5
       		sudo /sbin/shutdown -r now
        fi
done
I'd like to thank everyone who helped me out and pointed me to some very helpfull resources!
This has been a great learning experience, where I started out with a Python script with a lot of nested if/then and landed with a bash script with variables and nested condition tests.

Return to “Other programming languages”