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!


2 Answers 2


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.


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; 
  sudo tcpdump -i wlan0 -c 1  icmp and host and &&
  sleep 1

The tcpdump command will exit if icmp (ping) is send between and, 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 and acknowledge you have read our privacy policy.

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