yart yart - 1 year ago 100
Python Question

python libraries for ssh handling

I'm going to write first code for handling ssh commands on python and I did search over the stackoverflow and can see that there are several python libraries that can be used for handling commands passed through ssh, like paramiko, pexpect and perhaps some others.

Particularly, I will need to read content of the files from the remote server, copy files through ssh/scp, get output from remote server after starting the script on remote server.

Perhaps some experts could advice what library is better and specify advantages or disadvantages?

Answer Source

Libraries, Wrappers:

  1. http://media.commandline.org.uk/code/ssh.txt (example usage: http://commandline.org.uk/python/sftp-python-really-simple-ssh/)

    s = ssh.Connection('example.com', 'warrior', password = 'lennalenna')
    s.put('/home/warrior/hello.txt', '/home/zombie/textfiles/report.txt')
    s.get('/var/log/strange.log', '/home/warrior/serverlog.txt')
    s.execute('ls -l')
  2. http://www.lag.net/paramiko/

    #!/usr/bin/env python
    import paramiko
    from contextlib import contextmanager
    host = ''
    username = 'slacker'
    password = 'password'
    def create_ssh(host=host, username=username, password=password):
        ssh = paramiko.SSHClient()
           print "creating connection"
           ssh.connect(host, username=username, password=password)
           print "connected"
           yield ssh
           print "closing connection"
           print "closed"

1) utilizes the 2) and provides some higher level functions. If the latter suit your requirements, I'd suggest trying out 1)

Note: The code examples above are provided just for getting an impression; the code is not tested.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download