I am logged into a device and trying to run commands over the ssh connection. Whenever I run the command I get the prompt to enter a password I pass the password through stdin and the program gets stuck at stdout.readlines(). I think the reason for that is because paramiko is not able to pass the password for that command. Can you help? Below is the code that I am using
def SSH_Connection():
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(device_ip, username=username, password=password,port=22)
print(command_to_run)
stdin, stdout, stderr = ssh.exec_command(command_to_run)
stdin.write(command_pass+'\n')
stdin.flush()
data = stdout.readlines()
ssh.close()
for line in data:
print(line)
SSH_Connection()
Output of ssh -vv username@host your_command:
OpenSSH_for_Windows_7.6p1, LibreSSL 2.6.4
debug2: resolving "host" port 22
debug2: ssh_connect_direct: needpriv 0
debug1: Connecting to host [host] port 22.
debug1: Connection established.
debug1: key_load_public: No such file or directory
debug1: identity file C:\\Users\\temp/.ssh/id_rsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file C:\\Users\\temp/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file C:\\Users\\temp/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file C:\\Users\\temp/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file C:\\Users\\temp/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file C:\\Users\\temp/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file C:\\Users\\temp/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file C:\\Users\\temp/.ssh/id_ed25519-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_for_Windows_7.6
debug1: Remote protocol version 2.0, remote software version OpenSSH_12.1
debug1: match: OpenSSH_12.1 pat OpenSSH* compat 0x04000000
debug2: fd 3 setting O_NONBLOCK
debug1: Authenticating to host:22 as 'admin'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug2: local client KEXINIT proposal
Cipher exchange happens
debug1: sending SSH2_MSG_KEX_ECDH_INIT
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key:
debug1: Host 'host' is known and matches the RSA host key.
debug1: Found key in C:\\Users\\temp/.ssh/known_hosts:16
debug2: set_newkeys: mode 1
debug1: rekey after 4294967296 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug2: set_newkeys: mode 0
debug1: rekey after 4294967296 blocks
debug1: pubkey_prepare: ssh_get_authentication_socket: No such file or directory
debug2: key: C:\\Users\\temp/.ssh/id_rsa (0000000000000000)
debug2: key: C:\\Users\\temp/.ssh/id_dsa (0000000000000000)
debug2: key: C:\\Users\\temp/.ssh/id_ecdsa (0000000000000000)
debug2: key: C:\\Users\\temp/.ssh/id_ed25519 (0000000000000000)
debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
WARNING
Activity on this device, and attempted access, is logged.
debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug1: Next authentication method: publickey
debug1: Trying private key: C:\\Users\\temp/.ssh/id_rsa
debug1: Trying private key: C:\\Users\\temp/.ssh/id_dsa
debug1: Trying private key: C:\\Users\\temp/.ssh/id_ecdsa
debug1: Trying private key: C:\\Users\\temp/.ssh/id_ed25519
debug2: we did not send a packet, disable method
debug1: Next authentication method: keyboard-interactive
debug2: userauth_kbdint
debug2: we sent a keyboard-interactive packet, wait for reply
debug2: input_userauth_info_req
debug2: input_userauth_info_req: num_prompts 1
debug1: read_passphrase: can't open /dev/tty: No such file or directory
Password:
debug2: input_userauth_info_req
debug2: input_userauth_info_req: num_prompts 0
debug1: Authentication succeeded (keyboard-interactive).
Authenticated to host ([host]:22).
debug1: channel 0: new [client-session]
debug2: channel 0: send open
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: pledge: network
debug2: channel_input_open_confirmation: channel 0: callback start
debug2: fd 3 setting TCP_NODELAY
debug2: client_session2_setup: id 0
debug1: Sending command: my_command
debug2: channel 0: request exec confirm 1
debug2: channel_input_open_confirmation: channel 0: callback done
debug2: channel 0: open confirm rwindow 0 rmax 32768
debug2: channel 0: rcvd adjust 2097152
debug2: channel_input_status_confirm: type 99 id 0
debug2: exec request accepted on channel 0
The scenario is like after I run exec_command, it prompts for password. It's like when you run a sudo command OS asks you for password to run it like that as soon as I run the command I get prompted for password. My command is that I log in to a network device like a router and run a command and store the output and then check the output whether it ran successfully if not then what was the error. The code works for other commands. Wwhen I am running this command over cli it asks for password and then I enter the password and it runs. I tried using \r\n in stdin.write(command_pass+'\r\n') but it still gets stuck
Below is the shell channel code I used
def SSH_Connection_Shell:
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, username=username, password=password,port=22)
channel = ssh.invoke_shell()
channel.send('\n')
channel.send(command_to_run+'\n')
channel.send(pass)
channel.send('\r\n')
channel.send('\n')
time.sleep(5)
out=channel.recv(9999)
print(out)
file = open('Output_Of_Commands.txt', 'ab')
file.write(out)
file.close()