赞
踩
这是一个非交互示例…它发送cd tmp,ls然后退出。
import sys
sys.stderr = open(‘/dev/null’) # Silence silly warnings from paramiko
import paramiko as pm
sys.stderr = sys.stderr
import os
class AllowAllKeys(pm.MissingHostKeyPolicy):
def missing_host_key(self, client, hostname, key):
return
HOST = ‘127.0.0.1’
USER = ”
PASSWORD = ”
client = pm.SSHClient()
client.load_system_host_keys()
client.load_host_keys(os.path.expanduser(‘~/.ssh/known_hosts’))
client.set_missing_host_key_policy(AllowAllKeys())
client.connect(HOST, username=USER, password=PASSWORD)
channel = client.invoke_shell()
stdin = channel.makefile(‘wb’)
stdout = channel.makefile(‘rb’)
stdin.write(”’
cd tmp
ls
exit
”’)
print stdout.read()
stdout.close()
stdin.close()
client.close()
如果你有一个交互式用例,这个答案不会帮助…我个人会使用pexpect或exscript的交互式会话。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。