0

The idea I have is to make a netcat server, and when you connect to it, the server runs a python script with which the user can interact. So for example the script asks for a number, and it outputs that number to the power of 3. How can you do that? How do you run a program and redirect the output to the connected user and make them able to interact with it in real time?

Pseudo output:

$ nc 192.168.1.13  
Input a number: 2  
8  
(end)  

The script:

print(str(int(input("Input a number: "))**3))
muru
  • 72,889
  • 1
    If you involve Python might as well do the networking in Python too...? – frostschutz Oct 19 '19 at 23:24
  • @frostschutz can you give an example? not sure where to start, im a bit of a noob – InxaneNinja Oct 19 '19 at 23:41
  • See https://docs.python.org/3/library/socket.html for an example of IPv4 echo server. Hint, you don't need client - you can just use nc. If you still cannot get the code working, post whatever code you've written so far ( even if it doesn't work ) and then we can discuss how to improve that. – Sergiy Kolodyazhnyy Oct 20 '19 at 09:42

1 Answers1

1
socat TCP-LISTEN:50011,fork EXEC:'/path/to/script',stderr,pty,echo=0

Notes:

  • 50011 is the listening port, you can choose your own. Connect to it later, e.g. nc 127.0.0.1 50011.
  • fork allows socat to serve to many clients (also simultaneously).
  • Without pty it's normal to get Input a number: only after you actually provided input blindly.
  • My tests indicate that if one terminates socat with Ctrl+C then the port may stay in use for a while. If it's a problem, consider reuseaddr (i.e. TCP-LISTEN:50011,fork,reuseaddr).