0

I have a program running on my workstation that takes an input as follows

echo mycommand >/dev/tcp/127.0.0.1/1234

I would like to be able to send the "mycommand" to the specified port on the localhost machine via a click on a webpage so non-savvy people can execute the command easily.

Is there any way to get this to work?

  • 1
    This could be done rather trivially with a basic PHP script; otherwise you're going to have to read up on CGI scripting, I think. – DopeGhoti Aug 23 '17 at 18:15
1

It is quite complicated to do this properly, generating an html page with a form and using a proper POST request, perhaps even with authorisation by password. Provided you are on a local network and do not mind anyone connecting, you can get someway towards a demonstration of what could be done with a simple shell script using socat to manage the connection.

Create a shell script, say /tmp/myscript with contents

#!/bin/bash
echo "HTTP/1.0 200 OK
Content-Type: text/plain

running command" |
sed 's/$/\r/'
if echo mycommand >/dev/tcp/127.0.0.1/1234
then echo OK
else echo fail
fi
date

Don't forget to make it executable with chmod +x /tmp/myscript. Then run

socat tcp-listen:8001,reuseaddr,fork exec:/tmp/myscript

and you should be able to connect your browser to http://yourhostname:8001/ to trigger the command. Or for your testing use curl -v http://localhost:8001/ and you should see something like

* Connected to localhost (127.0.0.1) port 8001 (#0)
> GET / HTTP/1.1
> Host: localhost:8001
> User-Agent: curl/7.43.0
> Accept: */*
> 
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Content-Type: text/plain
< 
running command
OK
Wed Aug 23 21:04:47 CEST 2017

This is less than perfect, as some browsers may cache the result and not actually connect, hence the date output as a check. By using port 8001 you do not need to run socat as root. If you want to use the usual server port 80 (so your url is just http://yourhostname) you need to do sudo socat ....

If you are familiar with python, ruby, perl or many other languages, they all provide simple web servers that can handle POST requests correctly.

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.