AndiTails
Posts: 9
Joined: Tue Jan 21, 2014 8:35 pm

[Script] Push your Pi's IP address to your phone

Mon Jun 09, 2014 4:05 pm

Hi all.

I run a headless Pi at work which is given a pretty random internal IP by the DHCP server, so I wanted a way to get the IP pushed to my phone whenever the Pi boots up so I can SSH in.
For this to work you will need an account with http://www.PushBullet.com (it's free) and it setup on your phone (Android or iOS, and again, free!).

Logged into your Pi, let's create the script in your home directory:

Code: Select all

nano piip.sh
Paste in the code as follows

Code: Select all

#!/bin/bash
ipVar=$(/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}')
curl https://api.pushbullet.com/v2/pushes \
-u <paste_your_API_key_here>: \
-d device_iden="<device_ID_of_your_phone>" \
-d type="note" \
-d title="Pi IP address" \
-d body=$ipVar \
-X POST
You'll notice there's a couple of <>'s to replace in there, you can get these from here (replace the <>'s too!):
Your API for PushBullet is here: https://www.pushbullet.com/account
And to find the device ID of your phone, click on your device on the list on the homepage to send it a note here https://www.pushbullet.com/ and copy the key after the "?device_iden=" part of the URL in your browser.
Add those bits to the script.
Ctrl-X to Save & Quit, then:

Code: Select all

sudo chmod 777 piip.sh
to make the script executable by any user.
Give it a test by typing this at the command prompt:

Code: Select all

./piip.sh
You should get a message on your phone with the internal LAN IP address of your Pi.
Note - this is not for external IP - there's plenty of scripts for this, and I'm sure you could adapt this easily if you wanted to.
Lastly, add it to your crontab to make it start at boot:

Code: Select all

sudo crontab -e
Scroll to the bottom and add this line:

Code: Select all

@reboot /home/pi/piip.sh
Hope that helps someone.

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

Re: [Script] Push your Pi's IP address to your phone

Mon Jun 09, 2014 4:21 pm

777???
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)

AndiTails
Posts: 9
Joined: Tue Jan 21, 2014 8:35 pm

Re: [Script] Push your Pi's IP address to your phone

Mon Jun 09, 2014 4:22 pm

To be honest, I've done mine 700, but I didn't add mine to the crontab that way and wasn't sure if it would have access issues... ;)
Would 700 work via that crontab method?

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: [Script] Push your Pi's IP address to your phone

Mon Jun 09, 2014 8:58 pm

Excellent work :)

Ideal for my headless Pi :)
Thanks very much :)

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

diederik
Posts: 389
Joined: Wed Mar 26, 2014 11:17 pm

Re: [Script] Push your Pi's IP address to your phone

Mon Jun 09, 2014 10:24 pm

You can't just use the hostname to ssh into your Pi?
Like 'ssh [email protected]'?

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: [Script] Push your Pi's IP address to your phone

Mon Jun 09, 2014 10:35 pm

Not when your got a few and they all use the same SD card image :)
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

AndiTails
Posts: 9
Joined: Tue Jan 21, 2014 8:35 pm

Re: [Script] Push your Pi's IP address to your phone

Tue Jun 10, 2014 7:55 am

diederik wrote:You can't just use the hostname to ssh into your Pi?
Like 'ssh [email protected]'?
My work network will only give out a hostname if you connect to the domain controller.
Because the Pi doesn't (and I don't want it to), the hostname won't resolve.
So I needed to find the IP ;)

AndiTails
Posts: 9
Joined: Tue Jan 21, 2014 8:35 pm

Re: [Script] Push your Pi's IP address to your phone

Tue Jun 10, 2014 8:32 am

This morning, the DHCP server is being a little slow, so I had to tweak my crontab to be

Code: Select all

@reboot sleep 20;/home/pi/piip.sh
So it waits 20 seconds before running the command, and hopefully the network is all up by then!

The pains on running on an aging network with 2,000+ PCs on it and only 3 admins looking after it (not me)...

diederik
Posts: 389
Joined: Wed Mar 26, 2014 11:17 pm

Re: [Script] Push your Pi's IP address to your phone

Tue Jun 10, 2014 12:18 pm

AndiTails wrote:My work network will only give out a hostname if you connect to the domain controller.
Because the Pi doesn't (and I don't want it to), the hostname won't resolve.
Makes total sense :)
On my LAN, the DHCP server and DNS server are integrated in my router, so it works in my case.
But they're still separate services.

Neat solutions you found to your issue 8-)

heeed
Posts: 9
Joined: Tue Nov 27, 2012 8:17 pm
Location: Yorkshire
Contact: Website

Re: [Script] Push your Pi's IP address to your phone

Tue Jun 10, 2014 4:25 pm


simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: [Script] Push your Pi's IP address to your phone

Sat Jun 14, 2014 8:30 pm

Just used it for real for the 1st time - I had to reboot my home router so all devices got new Ip addresses and when I rebooted my Pi - it just told me its new address :)

Excellent Utility :)

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: [Script] Push your Pi's IP address to your phone

Mon Jun 16, 2014 1:15 pm

Aah - I've just realised a slight downside with it - the Pi needs internet access in order to push the message out to the pushbullet servers and then they send it onto my phone.
(Rather than direct across the LAN)

I found this out when trying a robot out in a school - I switched on and got no message on my phone (even though I'd added my home SSID to the WiFi network - perks of being the network manager :)

I realised in this case that no msg was going across the LAN and that I prob needed to add pushbullet.com to our list of allowed URLs.

Once I'd done that and rebooted the Pi - all OK :)

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: [Script] Push your Pi's IP address to your phone

Mon Jun 16, 2014 1:16 pm

A LOT easier than going into DHCP server and seeing what IP has been allocated nevertheless :)
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

mpatronska
Posts: 14
Joined: Sun Jun 07, 2015 7:05 am

Re: [Script] Push your Pi's IP address to your phone

Mon Nov 14, 2016 9:19 pm

Hi,

I was following the proposed example, but I used systemd service to run the script on boot. However the service failed to start:
[email protected]:/lib/systemd/system $ systemctl status send_ip.service
● send_ip.service - Send IP Service
Loaded: loaded (/lib/systemd/system/send_ip.service; enabled)
Active: failed (Result: exit-code) since Mon 2016-11-14 20:58:14 UTC; 14min ago
Process: 505 ExecStart=/home/pi/piip.sh (code=exited, status=6)
Main PID: 505 (code=exited, status=6)
Unit:
[Unit]
Description=Send IP Service
After=network.target

[Service]
Type=idle
ExecStart=/home/pi/piip.sh

[Install]
WantedBy=multi-user.target
Alias=send_ip.service
Running the script manually or by systemctl start send_ip.service is ok.
Do you have any suggestions why it fails on boot?
Thanks!

Return to “Raspbian”