[英]python libraries for ssh handling
由于你没有在协议级别做任何特殊的事情,你可能不需要在python中完全实现协议,你可以简单地使用subprocess
模块运行ssh / scp命令。
import subprocess
subprocess.check_call(['ssh', 'server', 'command'])
subprocess.check_call(['scp', 'server:file', 'file'])
图书馆,包装:
#!/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)利用2)并提供一些更高级别的功能。 如果后者符合您的要求,我建议尝试1)
更新:1)现在不见了(2017-09-12)
http://media.commandline.org.uk/code/ssh.txt (示例用法: https : //zeth.net/archive/2008/05/29/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') s.close()
注意 :上面的代码示例仅用于获取展示; 代码未经过测试。
试试看Twisted Conch吧 。 它处理你想要的一切。 如果您只是在寻找SCP风格的文件传输,您可以随时使用python模块secsh-filexfer 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.