3

So I have a raspberry pi set up with my script on it, however I only want the script to run when I need it to, which isn't on a regular schedule...the best way for me to do this is to ping it(as my windows machine is my main computer).

In the command prompt I can ping the RPi IP address with ping xxx.xxx.xx.xx. Is there any way this can be adapted to send a command to my RPi rather than a generic response?

Thanks for any help!

EcSync
  • 151
  • 1
  • 6
  • 1
    You cannot change what ping sends (in terms of it sending a command). I'd suggest instead running a web server (simple with Python sockets) that when called, calls the script you need to run. – Vincent P Apr 02 '19 at 07:03
  • you could use a serial connection via the USB – DrBwts Apr 02 '19 at 09:27
  • ...or SSH (https://www.raspberrypi.org/documentation/remote-access/ssh/). – cfillion Apr 02 '19 at 09:45
  • 1
    Do you have PuTTY installed on Windows? PuTTY includes plink that can send commands to your Raspbian system from a Windows DOS cmd.exe https://the.earth.li/~sgtatham/putty/0.71/htmldoc/Chapter7.html#plink – Dougie Apr 02 '19 at 11:15
  • Interesting question I think... re-purposing ping to send commands to a host. But after reading the documentation, you will understand this is not what ping is designed (or able) to do. There are likely many ways you could accomplish this, but then again, it may be far more effort than you're prepared for. I'd suggest you think through what it is you're trying to do, and consider other options for accomplishing it. – Seamus Apr 02 '19 at 15:04
  • You might find some traction using iptables. Read this Q&A, then look into the iptables documentation to log that event, then write a script to monitor the log & take action when it occurs. Like I said... quite a bit of effort. – Seamus Apr 02 '19 at 15:44
  • 1
    You can use socat and ncat for a simple one line port trigger see my answer here: https://raspberrypi.stackexchange.com/a/79824/71180 – crasic Apr 02 '19 at 16:57

2 Answers2

4

Taking from another answer of mine, the socat utility provides a convenient way to create this kind of "one-off" servelets. Your use case is very appropriate for this utility. socat is part of a suite of network "swiss-army-knife" tools along with nc(netcat) and ncat

socat can be used to listen on a port, and run a command whenever a connection is made. This command can respond with text or data, but it does not have to.

socat tcp4-listen:5000,reuseaddr,fork exec:'/home/pi/myscript_trigger.sh'

If you need it to respond with use-able data (will echo everything script prints to shell)

socat tcp4-listen:5000,reuseaddr,fork exec:'/home/pi/myscript_trigger.sh',pty,raw,echo=0

On another computer you can connect to this socket using a related net utility nc

$ nc my-pi.ip 5000
09:08:21 up 6 days,  1:35,  9 users,  load average: 0.07, 0.28, 0.25

or you can use socat as well

$ socat tcp4-connect:my-pi.ip:5000

(In this case the program triggered for demo is the system uptime utility)

Or Any number of other utilities that can connect to port and ip, many common terminal programs like puTTY, minicom, picocom, screen, and realterm on both windows and linux can connect directly to a host:port and be the "source" of the trigger.

To specifically use ping (ICMP) to trigger, is a little more challenging, especially if you require that the ICMP response is issued as normal.

crasic
  • 2,993
  • 1
  • 10
  • 20
1

I recommend to use ssh for this.

But if you want to do it with pings, you could try it by using tcpdump.

while true; 
do
  sudo tcpdump -i wlan0 -c 1  icmp and host 192.168.4.67 and 192.168.4.1 &&
  yourCommand
  sleep 1
done

The tcpdump command will exit if icmp (ping) is send between 192.168.4.67 and 192.168.4.1, which should be the ip addresses of your computer and pi.

jake
  • 1,347
  • 10
  • 23