2

I am using this code to add server to the known_hosts:

subprocess.Popen(['sshpass', '-p', password, 'ssh', '-o', 'StrictHostKeyChecking=no', add_key], stdout=subprocess.PIPE).communicate()[0]

This adds hostname to the known_hosts but the server hangs as it tries to enter into the host. I just want add hostname to the known_hosts and continue with my other codes. How can I do that? Thanks

2 Answers 2

1

This should do the job. This solution use the pexpect library which is a great way to automate commands. You basically call add_known_hosts with the host, user, password that you want added. it will try to ssh to that host and either enters the password or responds to the add to known hosts connection with a yes and then enters the password. Finally it cleanly exits the connection by sending the exit command. You can modify this and not require a username and password and just answer yes to the continue connecting question and then end the ssh process instead of continuing with the password prompt.

import pexpect, time

def add_known_hosts(host, user, password):
    child = pexpect.spawn('ssh %s@%s' % (user, host))
    i = child.expect(['.* password:', '.* continue connecting (yes/no)?'])
    if i == 1:
        child.sendline('yes')
        child.expect('.* password:')
    child.sendline(password)
    child.expect('%s@%s' % (user, host))
    time.sleep(1)
    child.sendline('exit')

add_known_hosts('myhost', 'myusername', 'mypassword')

debugging

from the comments below it seems you are facing issues using pexpect on your system. A good way to just to do a simple test to confirm pexpect is working correctly with ssh is to run the code below. Fill in host, user with your appropriate settings and then run the code, you should be able to interact with the ssh session. At this point you can build up from here and see exactly what text you are expecting to get from ssh and what text you want to send in response.

import pexpect
host, user = 'myhost', 'myusername'
child = pexpect.spawn('ssh %s@%s' % (user, host))
child.interact()
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for your answer. This seems abit complete. Is there any way to tweak my code and get it work. I tried your code but didn't work too.
check the output of your ssh command that it matches the text exactly. I've tested this and it works on my ubuntu 11.04 machine. What os are you running.
I am running Debian 6. what's roo@homebox. Your code seems fairly complicated.
Getting this error: EOF at /upload_file/ End Of File (EOF) in read_nonblocking(). Exception style platform. <pexpect.spawn object at 0x3311790> version: 2.3 ($Revision: 399 $)
child = pexpect.spawn('ssh %s@%s % (user, host)) File "<stdin>", line 1 child = pexpect.spawn('ssh %s@%s % (user, host)) ^ SyntaxError: EOL while scanning string literal
|
0

Never mind, Solved it myself. Here's what I did:

test = subprocess.Popen(['sshpass', '-p', password, 'ssh', '-o', 'StrictHostKeyChecking=no', add_key])
            time.sleep(5.0)
            test.kill()

Thanks!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.