ChefBorjan
Posts: 6
Joined: Tue Jan 15, 2013 10:22 pm

Emailing myself my Pi's external IP address

Tue Jan 15, 2013 10:31 pm

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.

jamiesk
Posts: 95
Joined: Mon Nov 26, 2012 8:48 pm
Location: Ipswich, Suffolk, England, UK.

Re: Emailing myself my Pi's external IP address

Thu Jan 17, 2013 10:58 pm

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 :)
Pi1 (Nov 2012 loft)= 1KW immersion controller for Solar panel
Pi2 (Jan 2013 living room)=Play thing
Pi3 (Feb 2013 mobile)= Play thing with Tandy Ladder board,breakout board,Nokia display
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=26&t=28193

ChefBorjan
Posts: 6
Joined: Tue Jan 15, 2013 10:22 pm

Re: Emailing myself my Pi's external IP address

Sat Feb 02, 2013 9:54 am

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?

User avatar
bgreat
Posts: 235
Joined: Mon Jan 23, 2012 2:09 pm

Re: Emailing myself my Pi's external IP address

Sat Feb 02, 2013 12:40 pm

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

Return to “Networking and servers”