Enter key works manually but not when sent via paramiko invoke_shell.sendall

Question:

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 ‘rn’

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 chassisrn')
    time.sleep(2)
    ucs.sendall('power onrn')
    time.sleep(2)
    ucs.sendall("yrn")
    time.sleep(10)
    ucs.sendall('showrn')
    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
Asked By: n3had

||

Answers:

I would be looking at this command:

    ucs.sendall('power onrn')

It is possible that because you are using both r and n that the console is interpreting the r to accept the power on command and the n to immediately cancel it. So before you get the chance to input "Y", the command has already been cancelled by the preceding "n"

Answered By: tomgalpin
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.