【问题标题】:Enter key works manually but not when sent via paramiko invoke_shell.sendall输入键手动工作,但通过 paramiko invoke_shell.sendall 发送时不起作用
【发布时间】:2023-02-09 21:46:35
【问题描述】:

我正在寻找一种通过 SSH 登录到 Cisco UCS 服务器并执行少量命令的方法。 我能够登录并执行多个命令并获得输出。但是需要 y 和 ENTER KEY 的一个命令似乎不起作用。

如果我通过终端手动尝试相同的操作,它就可以工作。看起来无论使用 '\n'、'\r' 还是 '\r\n',服务器上都没有执行 ENTER 键

def power_up(host, username, password):
    ssh = paramiko.client.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(host, username=username, password=password)
    ucs = ssh.invoke_shell()
    ucs.sendall('scope chassis\r\n')
    time.sleep(2)
    ucs.sendall('power on\r\n')
    time.sleep(2)
    ucs.sendall("y\r\n")
    time.sleep(10)
    ucs.sendall('show\r\n')
    time.sleep(10)
    s = ucs.recv(4096)
    with open("Output.txt", "ab") as text_file:
        text_file.write(s)
    with open("temp2", "wb") as text_file:
        text_file.write(s)
    ssh.close()




hostname# scope chassis
hostname /chassis #
hostname /chassis # power on
This operation will change the server's power state.
Do you want to continue?[y|N]y

hostname /chassis #
hostname /chassis # show
Power Serial Number Product Name  PID           UUID
----- ------------- ------------- ------------- ------------------------------------
off   xxxxxxxxxxx   UCS C240 M3S  UCSC-C240-M3S xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

【问题讨论】:

    标签: python paramiko cisco ucs


    【解决方案1】:

    我会看这个命令:

        ucs.sendall('power on
    ')
    

    有可能是因为你同时使用 和 控制台正在解释 接受上电命令和 立即取消它。所以在你有机会输入“Y”之前,命令已经被前面的“ “

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 2017-08-05
      • 1970-01-01
      • 1970-01-01
      • 2017-06-12
      • 1970-01-01
      相关资源
      最近更新 更多