Automating CLI with paramiko in python : Sending keyboard keys into CLI command

71 Views Asked by At

I am currently automating CLI command testing with paramiko in python. After successfull connection with ssh and running one command, an interactive session opens up in CLI itself, which is asking to enter choice from 1-10 and 'e' as exit. So how can I pass these keys into cli and press enter to navigate to that menu?

output = ''
strings = []
ssh = paramiko.SSHClient()

# Automatically add the host key to the local SSH key store
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

# Connect to the remote host with a password
ssh.connect(ip, username=username, password=password)
channel = ssh.invoke_shell()
# If su is True, login as root user
if su:
    channel.send('su - root\n')
    channel.send(supasswd+'\n')
command = '''ls -ltr
su -xyzconfig
1'''  
channel.send(command)
while channel.recv_ready():  # monitoring process 
    strings.append(channel.recv(65535).decode())
    time.sleep(2)
output =''.join(strings)
strings = []

Now I could extract the output till su -xyzconfig. But after that in command shell(if run manually), It asks to Enter choice as a keyboard key to navigate inside the menu. But here I am unable to send "1" as a key. So is there something else which I can do to navigate in the menu?

Menu which is appearing when I am manually entering commands

0

There are 0 best solutions below