当前位置: 首页>>代码示例>>Python>>正文


Python client.AutoAddPolicy方法代码示例

本文整理汇总了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") 
开发者ID:palfrey,项目名称:wharf,代码行数:42,代码来源:tasks.py


注:本文中的paramiko.client.AutoAddPolicy方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。