解决方案
库、包装:
-
#!/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()
注意到:代码的例子上提供的只是为了得到一种印象;代码是没有进行测试。
其他提示
由于你没有在协议层面做什么特别的东西,你可能并不需要的协议,在Python中完全实现,你可以简单地使用subprocess
模块运行ssh / scp的命令。
import subprocess
subprocess.check_call(['ssh', 'server', 'command'])
subprocess.check_call(['scp', 'server:file', 'file'])
试着看看 扭曲的海螺.它处理你想要的一切。如果你们只是寻找SCP式文件传输,你总是可以使用的模块蟒蛇 secsh-filexfer.
不隶属于 StackOverflow