V
V
vhsporno2019-07-17 19:42:04
Django
vhsporno, 2019-07-17 19:42:04

What's the best way to use Django + multiple SSH connections?

Now I have it implemented in such a way that when I enter the page, I connect via SSH to the device, after that I can perform any actions, but without a connection. I use paramiko. In code it looks like this

client = paramiko.SSHClient()
def connect(ip, port, user, pass):
   client =  paramiko.connect()#и так далее

def ls():
    client.exec_command('ls')# и так далее

Accordingly, we go to another page (with another client), we get a new connection, and any commands work with it.
The disadvantages are that every time a page is reloaded, a connection occurs, the connection does not close, and therefore a large number of connections accumulate on the device.
I would like to do it in some way. For example, when the server rises, there is a connection to all clients with different variable names = different connections (the question is how to implement this? how then to call the execution of a command without a connection in the function?). I would also like to use Celery so that all this works in the background, and when connecting (for example, the client is offline, even if you set a timeout of 3 seconds, the server hangs for 3 seconds).
The connect -> command -> connect.close() option seems to me too long.
Approximately 7 SSH connections are used The
question is how best to implement this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Cheremisin, 2019-07-17
@leahch

Run on a separate thread for each ssh connection. Synchronize streams through an external message bus, for example, you can implement it through redis, simply and cheaply.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question