I am looking for a way to login via SSH to a Cisco UCS server and execute few commands.
I am able to login and execute several commands and get the output. But the one command that requires y and Enter key doesn't seem to work.
If I try the same via terminal manually, it works. Looks like Enter key is not being executed on the server regardless of using \n
, \r
or \r\n
.
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