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.