2

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!

3

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.