views:

26

answers:

1

Hey all,

I'm trying to create a debug listener that listens on a port for a data string and forwards it to a php script as an argument. How can I specify that the input to the port should be used as an argument?

For example: The remote device send a string with comma separated values value1,value2,value3 and I'd like to run the command php file.php value1,value2,value3 and have the values used in $_SERVER["argv"][1].

Any help would be appreciated!

+1  A: 

Something like this, maybe? Every line sent will run a separate invocation of the PHP script. Note that netcat will quit once the connection is closed; some versions have a flag that tells it to go back to listening. Or you could wrap the whole thing in a while loop, or you could look into inetd or xinetd instead of netcat, though those might be a bit heavyweight.

nc -l myport | while read line ; do php file.php "$line" ; done
Jander
This is just what I needed! I added the -k switch to accept multiple connections and it's good enough for now. Maybe later I'll tinker with inetd and replace the php code with something more suitable to a linux environment. Thanks for the help!
onik
You're welcome! You've probably thought of this already, but if this is on a public IP address, remember to firewall your port so strangers don't trigger your script.
Jander
iptables takes care of that, but thanks for reminding :)
onik