本文整理汇总了Python中paramiko.client.AutoAddPolicy方法的典型用法代码示例。如果您正苦于以下问题:Python client.AutoAddPolicy方法的具体用法?Python client.AutoAddPolicy怎么用?Python client.AutoAddPolicy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类paramiko.client
的用法示例。
在下文中一共展示了client.AutoAddPolicy方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run_ssh_command
# 需要导入模块: from paramiko import client [as 别名]
# 或者: from paramiko.client import AutoAddPolicy [as 别名]
def run_ssh_command(self, command):
key = task_key(self.request.id)
redis.set(key, "")
client = SSHClient()
client.set_missing_host_key_policy(AutoAddPolicy)
known_hosts = os.path.expanduser('~/.ssh/known_hosts')
try:
client.load_host_keys(known_hosts) # So that we also save back the new host
except FileNotFoundError:
if not os.path.exists(os.path.dirname(known_hosts)):
os.mkdir(os.path.dirname(known_hosts))
open(known_hosts, "w").write("") # so connect doesn't barf when trying to save
if type(command) == list:
commands = command
else:
commands = [command]
for c in commands:
if os.path.exists(keyfile):
pkey = RSAKey.from_private_key_file(keyfile)
else:
pkey = None
client.connect(settings.DOKKU_HOST, port=settings.DOKKU_SSH_PORT, username="dokku", pkey=pkey, allow_agent=False, look_for_keys=False)
transport = client.get_transport()
channel = transport.open_session()
channel.exec_command(c)
while True:
anything = False
while channel.recv_ready():
data = channel.recv(1024)
handle_data(key, data)
anything = True
while channel.recv_stderr_ready():
data = channel.recv_stderr(1024)
handle_data(key, data)
anything = True
if not anything:
if channel.exit_status_ready():
break
time.sleep(0.1)
return redis.get(key).decode("utf-8")