Page 1 of 1

Emailing myself my Pi's external IP address

Posted: Tue Jan 15, 2013 10:31 pm
by ChefBorjan
Hi there.

I have come across the following command

Code: Select all

curl -s http://ifconfig.me
That quickly returns a nice simple output just displaying my external IP address. I would like to append this information to the python script as given by this guide: http://elinux.org/RPi_Email_IP_On_Boot_Debian

I'm quite new to linux, so could someone just suggest to me where I need to slot it in? I presume I need to make some sort of variable that calls this command and stores the output in a string, then spits this back out just after

Code: Select all

my_ip = 'Your ip is %s' %  ipaddr
This should make it a lot easier to gain access to the machine when I plug it into a new location.

Re: Emailing myself my Pi's external IP address

Posted: Thu Jan 17, 2013 10:58 pm
by jamiesk
Using your URL supplied ... try changing the line

_IP=$(hostname -I) || true

to

_IP=`wget -q -O - http://icanhazip.com/ | tail`

and it should work :)

Re: Emailing myself my Pi's external IP address

Posted: Sat Feb 02, 2013 9:54 am
by ChefBorjan
I would just like to say I'm still having problems with this, the above code doesn't seem to work for me.. anyone else?

Re: Emailing myself my Pi's external IP address

Posted: Sat Feb 02, 2013 12:40 pm
by bgreat
Make certain you enter the command correctly. The answer given is an example of command output substitution within the bash (and similar) shell.

From original answer:

Code: Select all

_IP=`wget -q -O - http://icanhazip.com/ | tail`
Note: that those are back-tick characters surrounding the command and not single quote characters. On my US keyboard, the back-tick is located to the left of the number one key.

For reference:

Code: Select all

  ' --> single quote
  ` --> back qoute/back-tick
  " --> double quote
If you want to use your original example site, you can use it in the same way:

Code: Select all

_IP=`curl -s http://ifconfig.me`
In scripting for bash (and similar), enclosing a command in back-ticks is an indication to run the command and use the command output inline. So, using your command as an example, typing at command prompt gives (ip replaced with xx.xx.xx.xx):

Code: Select all

$ curl -s http://ifconfig.me
xx.xx.xx.xx
Executing the script line previously given, after the shell executes and performs substitution, would be equivalent to:

Code: Select all

_IP=xx.xx.xx.xx
The _IP variable is assigned the command output.

The example you linked to has a Python script also. To perform an equivalent in the Python script requires you to update the IP request command with:

Code: Select all

arg='curl -s http://ifconfig.me'
p=subprocess.Popen(arg,shell=True,stdout=subprocess.PIPE)
Single quotes this time... ;)

Enjoy!
Bill