I am working with Paramiko on Linux, I would like to know if I can send a variable to shell. I want to enter to "enable mode" of a Cisco router. But I don't want to hard-code the password in the script. I am using getpass
, but when I run the script it fails in the enable
command. I get "bad secrets". I know that happens for wrong password. I know shell had to add \n
in the end of the command. But how I can add to a variable?
#!/bin/python3
import paramiko
import time
import getpass
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
password = getpass.getpass('Enter password: ')
router = {'hostname': '192.168.142.132', 'port': '22', 'username':'user', 'password': password}
print(f"Connecting to {router['hostname']} ")
ssh_client.connect(**router, look_for_keys=False, allow_agent=False)
shell = ssh_client.invoke_shell()
shell.send('enable\n')
passwordE = getpass.getpass('Enter the enable password:')
shell.send('passwordE')
Use +
operator.
shell.send(passwordE + '\n')
Or you can simply call send
twice:
shell.send(passwordE)
shell.send('\n')
Obligatory warning: Do not use AutoAddPolicy
this way – You are losing a protection against MITM attacks by doing so. For a correct solution, see Paramiko "Unknown Server".