【发布时间】: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
【问题讨论】: