Is it possible to use Python's cmd module remotely with multiple TCP clients?

194 views Asked by At

I created a python script that I'm running on my server which provides a simple command line interface for running custom configuration scripts. I'm using the cmd module to do this and it's been working great so far:

from cmd import Cmd

class MyPrompt(Cmd):

    def do_run1(self, args):
        print("running config1")

    def do_run2(self, args):
        print("running config2")

if __name__ == '__main__':
    prompt = MyPrompt()
    prompt.prompt = '> '
    prompt.cmdloop('Starting prompt...')

I also created another script which will open a TCP server and listen for remote clients in a new thread.Clients can send configuration commands to the server and the server will execute them and send back any output. Right now the client is very basic. It can send anything and does not have access to the nice interface that the cmd module provides.It's also up to the server to parse the received message and figure out the command that the client wants to run (via a long if else parser).

I'm trying to combine these 2 scripts, but I'm having a lot of trouble figuring out the best way to do so. I want to allow someone on the server to use the cmd lscript locally, but I also want the cmd script to accept remote clients and give them access to the cmd prompt at the same time. Also, I need a way for commands entered locally and the commands sent by remote clients to be added to a queue in order for the configuration commands to be run one at a time (each command takes a few minutes to run and cannot be executed in parallel).

Can anyone provide some examples or guidance on how I can extend my cmd script to support remote connections? I have no idea where to start and would be very appreciative of any help!

1

There are 1 answers

0
Arafangion On

You're probably better off investigating and learning Ansible.

Whilst I don't have experience with it, it's been highly recommended to me and it is implemented in and uses Python.

The documentation seems to be quite good.

(I don't use it because I haven't had the need to do this sort of thing - I generally do applications development)