/sshex-py

Basic and lightweight ssh expect

Primary LanguagePython

Basic and lightweight ssh expect.

Run commands on ssh hosts and handle prompts (passwords, confirmations, etc).


Example:

    # We run rsync with sudo from a remote host to another remote host

    from sshex import Ssh

    src_host = 'src_host'
    src_username = 'src_username'
    src_password = 'src_password'
    src_path = '/path/src/'

    ssh = Ssh(src_host, src_username, src_password)

    dst_host = 'dst_host'
    dst_username = 'dst_username'
    dst_password = 'dst_password'
    dst_path = '/path/dst/'

    cmd = 'rsync -av %s %s@%s:%s' % (src_path, dst_username, dst_host, dst_path)

    # expect patterns (sudo password is automatically handled)
    expects = [
        (r'(?i)\(yes/no\)', 'yes'),
        (r'(?i)\bpassword\b', dst_password),
        ]

    output, return_code = ssh.run(cmd, expects=expects, use_sudo=True)

    print 'output:', output
    print 'return code:', return_code