I recently got myself a Raspberry Pi 2 to learn a couple of things in my spare time. It is now running on Raspbian and I control it remotely via ssh from a laptop with Linux Mint 17.2 installed.
Now I would like the Pi to automatically tell the laptop that it is online after a reboot so that I know that I can connect to it via ssh. I know I can just wait a few seconds or ping the Pi, but somehow I got it into my head that it would be nice if a small message popped up in my terminal on the laptop.
What I got so far after some tinkering is the following (I'm VERY new to this, so I'm not even aware of the levels this might be horribly wrong on):
/usr/bin/ssh 'laptop_user'@'laptop_ip' "echo '### RaspberryPi 2 online ###' | /usr/bin/write 'laptop_user' pts/0"
This works when run in a terminal on the Pi if my laptop has the IP 'laptop_ip' and if 'laptop_user' is logged in on
pts/0 (lots of if's, but I figured I would get to those after I got the initial idea up and running). On the laptop terminal something like
Message from 'laptop_user'@'laptop_host' on pts/0 at 09:58: ### RaspberryPi 2 online ### EOF
I then put the command into a small script:
#! /bin/sh /usr/bin/ssh 'laptop_user'@'laptop_ip' "echo '### RaspberryPi 2 online ###' | /usr/bin/write 'laptop_user' pts/0" exit 0
saved it as
/etc/network/if-up.d/sayhi on the Pi, and made it executable (following the best answer on this question). I checked that this script does indeed get executed after each reboot of the Pi. The thing is, if I run the script manually everything works fine and I get the message on my laptop terminal. But if the script is automatically executed on reboot I don't get the message. Putting the command into
crontab didn't work either.
I unfortunately lack the knowledge of how a startup of the Pi (or any computer) actually works. So I don't know if the services required for this command are already good to go.
So my question is:
Why don't I get the "online" message when the script is run automatically and when should I run my little script to achieve the desired behavior.
Also, there might be way better alternatives to my way of doing this. So if anyone could point me in the right direction I'd appreciate it.
Thanks in advance!
I forgot to mention that I'm using key authentication and as the script will be run as the Pi's
rootI added its public RSA key to
authorized_keyson the laptop and I'm using the private key as the identify file for the
I'm now logging the output of
/sbin/ip addrwhile running the script on startup and if gives me:
eth0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000 link/ether b8:27:eb:34:66:ce brd ff:ff:ff:ff:ff:ff
When running the script later (manually over ssh)
/sbin/ip addrgives me:
eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether b8:27:eb:34:66:ce brd ff:ff:ff:ff:ff:ff inet 192.168.0.105/24 brd 192.168.0.255 scope global eth0 valid_lft forever preferred_lft forever
So the problem seems to be that the Pi does not have a local IP while running the scripts in
/etc/network/if-up.d. I now have to run my script after the IP is assigned. Unfortunately, I don't know enough about networking to be able to do so.