Question

I'm writing fairly simple application which connects to server through SSH (using paramiko), does something and writes output to web page. I wrote a script which works well when I run it from command line. However, if I run it in Django application, it can't get through connect part.

SSH connect part:

transport = paramiko.Transport((host, port))

# application cannot get through this line
transport.connect(username = '***', password = '***')

output = ...

View:

def ssh_output(request):
    return HttpResponse(output)

Any idea why does it behave like this? Is there any way to fix it?

Was it helpful?

Solution

I'm guessing your Django app may be running under a different user than the user you're running your command line script under. Also, I'm guessing it might be the first time the Django app user is trying to ssh to the host, so it may be hanging on some sort of 'is it OK to update ~/.ssh/known_hosts' question.

It looks like if you use SSHClient instead of Transport, then you can set the missing host key policy to automatically add the missing host keys ala

import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(your_host, port=your_port, username=your_username, password=your_password)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top