Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

executable file 55 lines (47 sloc) 1.232 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#!/usr/bin/env python

import pexpect
import sys
import shutil
import os

logfile = sys.stdout
def ssh_in():
    cmd = "ssh -i %s localhost hostname" % (sys.argv[1])
    print cmd
    child = pexpect.spawn (cmd, timeout=30, maxread=20000, logfile=logfile)
    child.expect ('(yes/no)?')
    child.sendline ('yes')
    try:
        child.expect ('(yes/no)?')
        child.sendline ('yes')
        rc = child.expect(pexpect.EOF)
    except:
        rc = 0
    return rc


print "try ssh"
try:
    cmd="ssh-keygen -f %s" % (sys.argv[1])
    print cmd
    child = pexpect.spawn (cmd, timeout=60, maxread=20000, logfile=logfile)
    child.expect (':')
    child.sendline ('')
    child.expect (':')
    child.sendline ('')
    rc = child.expect(pexpect.EOF)
    if rc != 0:
        print "keygen failed"
        sys.exit(rc)

    cmd = "cp %s.pub %s/.ssh/authorized_keys" % (sys.argv[1], os.environ['HOME'])
    print cmd
    child = pexpect.spawn (cmd, timeout=30, maxread=20000, logfile=logfile)
    rc = child.expect(pexpect.EOF)

    print "setting up ssh knowhosts"
    try:
        rc = ssh_in()
    except:
        rc = ssh_in()
    sys.exit(rc)

except SystemExit:
    raise

except Exception, ex:
    print ex
    sys.exit(1)
Something went wrong with that request. Please try again.