當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。