Pexpect

Instalación

apt-get install python-pexpect

Ejemplo de conexión SSH

#!/usr/bin/env python
import sys

machines = {
        'drazel' : ['192.168.0.2','lez','mdplez'],
        'ma1': ['machine1.mondomaine.com','monutilisateur','monmotdepasse']
}

l = len(sys.argv)
if l == 1:
        print "Liste des machines :"
        for machine in machines:
                print machine,
        print ""
        sys.exit(0)
elif l > 2:
        print "Trop de parametres"
        sys.exit(1)

try:
        machine = machines[sys.argv[1]]
except KeyError:
        print sys.argv[1] + " n'existe pas"
        sys.exit(2)

cmd = "ssh "+machine[1]+"@"+machine[0]
password = machine[2]

import pexpect,time
ssh = pexpect.spawn(cmd)
ssh.expect (['Password:','password:'])
time.sleep (0.1)
ssh.sendline(password)
ssh.interact()
ssh.expect(pexpect.EOF)

Ejemplo de SSH anidado

#!/usr/bin/env python
import pexpect
ssh = pexpect.spawn('ssh lunix@entrerios')
ssh.expect (['Password:','password:'])
ssh.sendline('lunix')
print ssh.before
print('Ingresando a entrerios')
#ssh.interact()
#ssh.expect(pexpect.EOF)
ssh.sendline('ssh lunix@cordoba')
ssh.expect (['Password:','password:'])
ssh.sendline('lunix')
print ssh.before
print('ingresando a cordoba')
ssh.interact()
ssh.expect(pexpect.EOF)

Referencias

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License