1

I have a RaspberryPi, running the standard Pi distro, and a computer running Linux 16.04 in the same local network with fixed IP adresses. The Pi is used to wake the computer by sending a Wake-on-Lan package and then tunnelling onto the computer via SSH.

Is there an easy and simple way for the computer to send a message to the Pi, which the Pi then interprets as the computer having started up succesfully and being reachable?

I already have set up a system where the Pi is continuously pinging the computer until it gets a response, but I specificly want to have the computer message the Pi and not the Pi checking up on the computer.

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
  • 3
    Just put something like echo foo | nc <pi ip address> <port> into /etc/rc.local on computer, and have pi listen on the given port for messages in separate script or even background. You could then use wait from script to know when background task finished. – Sergiy Kolodyazhnyy Nov 16 '18 at 18:42
  • And how would I set up the Pi to listen for that ping? – UmBottesWillen Nov 16 '18 at 23:01
  • I've put that as an answer in more details. General idea is that you already have nc tool for sending packets and testing connection with most *nix systems, and it's easy to make a simpke server-client type of thing with it – Sergiy Kolodyazhnyy Nov 16 '18 at 23:41

1 Answers1

2

Use nc - simplest method.

On computer( assuming it is a *nix system) : put ( echo "computer is up" | nc 192.168.0.123 6677 ) & into /etc/rc.local. The (...) & spawns background shell to prevent the rc.local script from blocking other things from execution while computer boots. Assume 192.68.0.123 is your Pi's IP address on same network as computer .

On Pi, have a process listening on port 6677 with nc -l 6677 after whatever command you're using to send wake on LAN signal. By default nc terminates once the sending side closed the connection ( which should happen after all bytes of "computer is up" string have been sent ). So you could do something like

netreply=$( nc -l 6677)
case $netreply in
     "computer is up") echo "All good" ;;
     *)  echo some boo-boo happened ;;
esac
  • Thanks, I'll try that out and see if that works. – UmBottesWillen Nov 17 '18 at 02:07
  • I'm not sure rc.local works very well with systemd ... a more universal solution for most current Linux distros is to use a systemd service file that waits until the network interface is up and running... like this – RubberStamp Nov 17 '18 at 02:23
  • @RubberStamp rc.local works with systemd and there is rc-local.service which executes /etc/rc.local script. See https://askubuntu.com/a/919598/295286 Can you explain what exactly you mean by "not sure ...works very well" part ? Even though I agree it'd be nicer to have an actual service made, /etc/rc.local is perfectly fine alternative. – Sergiy Kolodyazhnyy Nov 17 '18 at 02:33
  • Some distros may not include (or enable) the rc.local service in the default configuration. My understanding is that the rc.local systemd service runs after the network is up and running anyway. So, why not just put the messaging script its own service file for easy visibility and uniformity? ... I guess the point was, it's possible that a given Linux distro will not automatically run rc.local .... – RubberStamp Nov 17 '18 at 03:00
  • @RubberStamp Fair enough. But if it's not enabled by default nothing stops us from enabling that service. But sure, it may be a viable alternative for visibility/uniformity to have it's own service. – Sergiy Kolodyazhnyy Nov 17 '18 at 03:05
  • Your suggestion did indeed work! Now, since my computer has the bad habit of starting itself without me actually wanting it to, can I implement a service on the Pi that is permamently listening for the computer to wake up and respond appropriately (I already have shutdown script, I just need a way to detect the computer starting), that can be stopped when I want to start the computer? – UmBottesWillen Nov 17 '18 at 21:42
  • @UmBottesWillen Glad I could help. The nc command has -k switch which would make it to stay listening on the port for new connections So nc -k -l 6677 is probably what you want. Output from that probably could be piped to something else, like nc -k -l 6677 | while IFS= read -r line; do printf "Received message: %s" "$line" ; done And in that command you could replace printf with whatever you want the Pi to do. – Sergiy Kolodyazhnyy Nov 17 '18 at 21:47
  • Sorry for the late addendum, for now everything has worked out quite well. While I do struggle to get the service running, I'm confident that I'll figure that one out. Now, my last, tiny question for you would be if I can specify a source Ip adress for nc -l to listen for and to disregard connections made from any other IP adresses. – UmBottesWillen Nov 27 '18 at 11:20
  • @UmBottesWillen I think what you need is -v flag to give verbose output from nc. It should report what address connected to the port, but you'll have to parse it later with other tools, such as awk maybe, but bash's basic case statement could work too. If you need specific parsing, best idea would be to ask a new question as we're kinda going out of scope of this question by now. – Sergiy Kolodyazhnyy Nov 28 '18 at 06:18