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?
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') s.close()
#!/usr/bin/env python import paramiko from contextlib import contextmanager host = '192.168.10.142' username = 'slacker' password = 'password' def create_ssh(host=host, username=username, password=password): ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) try: print "creating connection" ssh.connect(host, username=username, password=password) print "connected" yield ssh finally: print "closing connection" ssh.close() 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.