zoukankan      html  css  js  c++  java
  • 使用paramiko连接第一台服务器,并在第一台服务器上执行ssh连接第二台服务器,提示密码输入时的处理办法

    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(
    hostname='192.168.40.111',
    port=22,
    username='root',
    password='yourpass'
    )
    stdin, stdout, stderr = client.exec_command('ssh 192.168.40.158 "df -h"', get_pty=True)

    while not stdout.channel.exit_status_ready():
        temp = stdout.channel.recv(1024)
    result
    = temp.decode('utf-8').strip(' ')# 获取数据 if "continue connecting" in result: # 可能遇到询问是否添加指纹 stdout.channel.send("yes ") # 自动推送yes if "password:" in result: # 可能遇到询问密码 password_cmd = "%s " % yourpass # 自动推送密码(第二台服务器的密码) stdout.channel.send(password_cmd) else: print(result)

    if stdout.channel.exit_status_ready(): # 这里需要单独再判断一次,否则读取的数据可能少一部分 temp = stdout.channel.recv(1024) result = temp.decode('utf-8').strip(' ') print(result)

    break

  • 相关阅读:
    监听器
    过滤器
    连接池与分页
    jdbc优化
    jdbc入门
    web开发mysql基础
    自定义标签
    jsp基础
    会话管理入门
    19. Remove Nth Node From End of List C++删除链表的倒数第N个节点
  • 原文地址:https://www.cnblogs.com/t-road/p/14470373.html
Copyright © 2011-2022 走看看