Tob*_*obu 25
由于你没有在协议级别做任何特殊的事情,你可能不需要在python中完全实现协议,你可以简单地使用subprocess模块运行ssh/scp命令.
import subprocess
subprocess.check_call(['ssh', 'server', 'command'])
subprocess.check_call(['scp', 'server:file', 'file'])
Run Code Online (Sandbox Code Playgroud)
mik*_*iku 24
图书馆,包装:
#!/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"
Run Code Online (Sandbox Code Playgroud)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()
Run Code Online (Sandbox Code Playgroud)注意:上面的代码示例仅用于获取展示; 代码未经过测试.