SyncingFeeling
Posts: 45
Joined: Thu Jan 03, 2013 9:15 pm

If IP is x, then y

Mon May 27, 2013 6:47 pm

Hi,

Trying to set up a script which says 'if external IP is x, then do y. If IP not x, do z.'

Here it is:

Code: Select all

#/!/bin/bash
IP=$(wget -q -O - http://www.whatismyip.com/automation/n09230945.asp)
if [[ $IP = x.x.x.x ]]; then
  echo "yep"
else
  exit 0
fi
The error message is 'syntax error near unexpected token fi'.

I take this as the pi's way of saying 'not even close, you moron' :D. I'm sure I've done lots of things wrong here, but what are they?

User avatar
joan
Posts: 15370
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: If IP is x, then y

Mon May 27, 2013 7:07 pm

You want #!/bin/bash as the first line.

wget -q -O - http://www.whatismyip.com/automation/n09230945.asp returns a web page, not its IP address.

RobHenry
Posts: 452
Joined: Fri Sep 21, 2012 9:04 pm
Location: UK

Re: If IP is x, then y

Mon May 27, 2013 7:40 pm

Can't remember where I picked it up but I use this to return my external ip address:

curl ifconfig.me

User avatar
joan
Posts: 15370
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: If IP is x, then y

Mon May 27, 2013 7:50 pm

Or

Code: Select all

#!/bin/bash

IP=$(curl ipecho.net/plain 2>/dev/null)

if [[ $IP = x.x.x.x ]]; then
  echo "yep"
else
  echo "no"
fi

remsnet
Posts: 151
Joined: Wed Dec 19, 2012 7:32 pm
Location: Planet Gaia
Contact: Website Yahoo Messenger

Re: If IP is x, then y

Mon May 27, 2013 7:55 pm

i.e use dnyndns client that solve your truble.

SyncingFeeling
Posts: 45
Joined: Thu Jan 03, 2013 9:15 pm

Re: If IP is x, then y

Tue May 28, 2013 12:13 am

joan wrote:Or

Code: Select all

#!/bin/bash

IP=$(curl ipecho.net/plain 2>/dev/null)

if [[ $IP = x.x.x.x ]]; then
  echo "yep"
else
  echo "no"
fi
Thanks, but returns the same error as before: syntax error near unexpected token fi.

User avatar
rpdom
Posts: 18136
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: If IP is x, then y

Tue May 28, 2013 5:01 am

How are you running this script?

Is it from the command line, or from IDLE (or some other editor)?

SyncingFeeling
Posts: 45
Joined: Thu Jan 03, 2013 9:15 pm

Re: If IP is x, then y

Tue May 28, 2013 11:53 am

I am running from the command line. I intend to run it via cron, when I have it working.

User avatar
joan
Posts: 15370
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: If IP is x, then y

Tue May 28, 2013 12:15 pm

Just to clarify.

You should enter the script's text into a file and then set the file's executable bit.

Assuming your script is in a file called getip.
joan@mercury:~$ cat getip
#!/bin/bash

IP=$(curl ipecho.net/plain 2>/dev/null)

if [[ $IP = x.x.x.x ]]; then
echo "yep"
else
echo "no"
fi
joan@mercury:~$ chmod +x getip
joan@mercury:~$ ./getip
no
joan@mercury:~$

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: If IP is x, then y

Tue May 28, 2013 12:25 pm

As so often happens, there are two separate issues here - and the responders all seem to be missing the OP's point (which is #1 in the list below) and instead obsessing on #2 below.

The issues are:

1) I need help with my shell scripting. In particular, I need to learn how to quote my variables.

2) What is the best way to use external servers to determine my "WAN IP address"?

Now, it turns out that these issues are related, in that if the OP had a reliable method of getting the IP address - and only the IP address into a shell variable, then the first issue wouldn't arise, but that doesn't negate the fact that the real issue here is that he is pretty new to shell scripting.

Anyway, addressing #2 first, it seems to me that the best way to get the iP (and only the IP) is:

Code: Select all

curl -q ipecho.net/plain
Note, FWIW, that another way to do it is to connect to an FTP site (pretty much any old FTP site will do) and do "quote stat".

Now, addressing #1:

If you want to do it with an "if" statement, you should do:

if [ "$IP" = "w.x.y.z" ]

That usually makes the weird error messages go away.

Or, you can use "case", like:

case $IP in
*w.x.y.z*) echo Yaman!;;
*) echo Nope;;
esac

The nice thing about 'case', is that, almost as if by magic, quoting is not needed.

P.S. Finally, note that like the other responders on this thread, I wasn't able to get the "whatismyip.com" thing to work at all. In fact, I had spent some time messing with that company some time ago (on a totally unrelated matter) and I pretty much concluded that they had made themselves automation-proof. YMMV (of course).
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

SyncingFeeling
Posts: 45
Joined: Thu Jan 03, 2013 9:15 pm

Re: If IP is x, then y

Wed May 29, 2013 10:36 am

Thanks that worked fine! :)

flanman
Posts: 7
Joined: Sat Jun 08, 2013 2:11 pm

Re: If IP is x, then y

Sun Jun 16, 2013 3:17 pm

Hi All,

I'm trying to do something similar using Python. I'd like to shoot myself a text message each time the external IP address of the Pi changes, that way I can always connect remotely. I've got the texting part working, but I'm having trouble figuring out what to do with pycurl.

Code: Select all

import pycurl
#use cURL to get current IP address
current_ip = ""
c = pycurl.Curl()
c.setopt(c.URL, 'http://ipecho.net/plain')
current_ip = str(c.perform())
In the last line, c.perform() returns a "None" as opposed to the ip address. It does, however, echo the IP to the shell.

I'm pretty confident I could get the storage of current IP, lookup of previous, and related code going, but it's my first day using Python and curl and I don't know how to pipe the output of c.perform() from stdout to a file. Any suggestions?

Return to “Beginners”