无法通过SSH Paramiko运行命令

问题描述

我已登录设备,并尝试通过ssh连接运行命令。每当我运行命令时,都会提示我输入密码,我会通过stdin传递密码,程序会卡在stdout.readlines()处。我认为这是因为paramiko无法通过该命令传递密码。 你能帮我吗? 下面是我正在使用的代码

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()

ssh -vv用户名@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

这种情况就像我运行exec_command之后,提示输入密码。就像当您运行sudo命令时,操作系统要求您输入密码才能像这样运行该命令时,系统会提示我输入密码。我的命令是我登录到路由器之类的网络设备并运行命令并存储输出,然后检查输出是否成功运行(如果未成功运行),然后是什么错误。该代码适用于其他命令。当我在cli上运行此命令时,它会要求输入密码,然后输入密码即可运行。我尝试在stdin.write(command_pass +'\ r \ n')中使用\ r \ n,但仍然卡住

下面是我使用的shell通道代码

def SSH_Connection_Shell:
    ssh = paramiko.SSHClient()
    ssh.load_system_host_keys()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(host,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()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)