首页 > 解决方案 > 如何使用 Python 的 Paramiko 模块 ssh 进入需要两次密码验证的服务器?

问题描述

如何使用 Paramiko SSH 到需要双重密码身份验证的服务器?

使用特定用户时,它首先提示输入用户密码,然后输入另一个密码,因此我的会话需要是交互式的。我已经使用 pexpect 模块ssh在 Linux 上生成了一个进程,但由于我无法在 Windows 中执行此操作,因此我需要一种使用 Paramiko 的方法来执行此操作。

服务器是我们的产品,它是 CentOS 的略微修改版本。我正在编写自动化代码来测试一些需要我 ssh 进入服务器并验证一些命令的功能。我能够以 root 用户身份登录,但对于我感兴趣的用户,它要求输入第二个密码。

这是ssh -vvv命令的输出:

ssh -vvv -p2222 nobrk1n@10.213.23.112  
OpenSSH_6.4,OpenSSL 1.0.1e-fips 2013 年 2 月 11 日  
debug1:读取配置数据 /etc/ssh/ssh_config  
debug1:/etc/ssh/ssh_config 第 51 行:为 * 应用选项  
调试2:ssh_connect:需要priv 0  
debug1:连接到 10.213.23.112 [10.213.23.112] 端口 2222。  
debug1:建立连接。  
调试1:永久设置uid:0/0  
debug3:RSA1 标识符不正确  
debug3:无法加载“/root/.ssh/id_rsa”作为 RSA1 公钥  
debug1:身份文件 /root/.ssh/id_rsa 类型 1  
debug1:身份文件/root/.ssh/id_rsa-cert type -1  
debug1:身份文件 /root/.ssh/id_dsa 类型 -1  
debug1:身份文件 /root/.ssh/id_dsa-cert type -1  
debug1:身份文件 /root/.ssh/id_ecdsa 类型 -1  
debug1:身份文件/root/.ssh/id_ecdsa-cert type -1  
debug1:为协议 2.0 启用兼容模式  
debug1:本地版本字符串 SSH-2.0-OpenSSH_6.4  
debug1:远程协议版本2.0,远程软件版本OpenSSH_7.4  
debug1:匹配:OpenSSH_7.4 拍 OpenSSH*  
debug2:fd 3 设置 O_NONBLOCK  
调试3:put_host_port:[10.213.23.112]:2222  
debug3:load_hostkeys:从文件“/root/.ssh/known_hosts”加载主机“[10.213.23.112]:2222”的条目  
debug3:load_hostkeys:在文件 /root/.ssh/known_hosts:1 中找到密钥类型 ECDSA  
debug3: load_hostkeys: 加载了 1 个键  
debug3: order_hostkeyalgs: 首选 hostkeyalgs: ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa -sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521  
debug1: SSH2_MSG_KEXINIT 已发送  
debug1:收到 SSH2_MSG_KEXINIT  
debug2:kex_parse_kexinit:ecdh-sha2-nistp256、ecdh-sha2-nistp384、ecdh-sha2-nistp521、diffie-hellman-group-exchange-sha256、diffie-hellman-group-exchange-sha1、diffie-hellman-group14-sha1、 diffie-hellman-group1-sha1  
debug2: kex_parse_kexinit: ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2- nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-rsa-cert-v01@openssh.com,ssh-dss-cert-v01@openssh.com,ssh-rsa-cert-v00@openssh.com, ssh-dss-cert-v00@openssh.com,ssh-rsa,ssh-dss  
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-gcm@openssh.com,aes256-gcm@openssh.com,aes128-cbc,3des-cbc,blowfish-cbc,cast128- cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se  
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-gcm@openssh.com,aes256-gcm@openssh.com,aes128-cbc,3des-cbc,blowfish-cbc,cast128- cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se  
debug2: kex_parse_kexinit: hmac-md5-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256- etm@openssh.com、hmac-sha2-512-etm@openssh.com、hmac-ripemd160-etm@openssh.com、hmac-sha1-96-etm@openssh.com、hmac-md5-96-etm@openssh。 com,hmac-md5,hmac-sha1,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-ripemd160,hmac-ripemd160@openssh.com, hmac-sha1-96,hmac-md5-96  
debug2: kex_parse_kexinit: hmac-md5-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256- etm@openssh.com、hmac-sha2-512-etm@openssh.com、hmac-ripemd160-etm@openssh.com、hmac-sha1-96-etm@openssh.com、hmac-md5-96-etm@openssh。 com,hmac-md5,hmac-sha1,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-ripemd160,hmac-ripemd160@openssh.com, hmac-sha1-96,hmac-md5-96  
调试2:kex_parse_kexinit:无,zlib@openssh.com,zlib  
调试2:kex_parse_kexinit:无,zlib@openssh.com,zlib  
调试2:kex_parse_kexinit:  
调试2:kex_parse_kexinit:  
调试2:kex_parse_kexinit:first_kex_follows 0  
debug2:kex_parse_kexinit:保留 0  
debug2:kex_parse_kexinit:curve25519-sha256、curve25519-sha256@libssh.org、ecdh-sha2-nistp256、ecdh-sha2-nistp384、ecdh-sha2-nistp521、diffie-hellman-group-exchange-sha256、diffie-hellman-group16- sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha256,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1  
debug2: kex_parse_kexinit: ssh-rsa,rsa-sha2-512,rsa-sha2-256,ecdsa-sha2-nistp256,ssh-ed25519  
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com  
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com  
调试2:kex_parse_kexinit:hmac-sha2-256,hmac-sha2-512  
调试2:kex_parse_kexinit:hmac-sha2-256,hmac-sha2-512  
调试2:kex_parse_kexinit:无,zlib@openssh.com  
调试2:kex_parse_kexinit:无,zlib@openssh.com  
调试2:kex_parse_kexinit:  
调试2:kex_parse_kexinit:  
调试2:kex_parse_kexinit:first_kex_follows 0  
debug2:kex_parse_kexinit:保留 0  
debug2:mac_setup:找到 hmac-sha2-256  
调试1:kex:服务器->客户端aes128-ctr hmac-sha2-256无  
debug2:mac_setup:找到 hmac-sha2-256  
调试1:kex:客户端->服务器aes128-ctr hmac-sha2-256无  
debug1:发送 SSH2_MSG_KEX_ECDH_INIT  
debug1:期待 SSH2_MSG_KEX_ECDH_REPLY  
debug1:服务器主机密钥:ECDSA 30:5c:e6:be:81:31:79:b8:71:80:bf:49:95:a9:79:12  
调试3:put_host_port:[10.213.23.112]:2222  
调试3:put_host_port:[10.213.23.112]:2222  
debug3:load_hostkeys:从文件“/root/.ssh/known_hosts”加载主机“[10.213.23.112]:2222”的条目  
debug3:load_hostkeys:在文件 /root/.ssh/known_hosts:1 中找到密钥类型 ECDSA  
debug3: load_hostkeys: 加载了 1 个键  
debug3:load_hostkeys:从文件“/root/.ssh/known_hosts”加载主机“[10.213.23.112]:2222”的条目  
debug3:load_hostkeys:在文件 /root/.ssh/known_hosts:1 中找到密钥类型 ECDSA  
debug3: load_hostkeys: 加载了 1 个键  
debug1:主机 '[10.213.23.112]:2222' 已知且与 ECDSA 主机密钥匹配。  
debug1:在 /root/.ssh/known_hosts:1 中找到密钥  
debug1:ssh_ecdsa_verify:签名正确  
调试2:kex_derive_keys  
debug2:set_newkeys:模式 1  
debug1: SSH2_MSG_NEWKEYS 已发送  
debug1:期待 SSH2_MSG_NEWKEYS  
调试2:set_newkeys:模式0  
debug1:收到 SSH2_MSG_NEWKEYS  
debug1:服务器不允许漫游  
debug1:SSH2_MSG_SERVICE_REQUEST 已发送  
debug2: service_accept: ssh-userauth  
debug1:收到 SSH2_MSG_SERVICE_ACCEPT  
debug2: key: /root/.ssh/id_rsa (0x55f959096720),  
debug2: key: /root/.ssh/id_dsa ((nil)),  
debug2: key: /root/.ssh/id_ecdsa ((nil)),  
debug1:可以继续的身份验证:publickey、gssapi-keyex、gssapi-with-mic、password  
debug3:重新开始,传递不同的列表 publickey、gssapi-keyex、gssapi-with-mic、password  
debug3:首选 gssapi-keyex、gssapi-with-mic、publickey、keyboard-interactive、密码  
debug3:authmethod_lookup gssapi-keyex  
debug3:仍然首选:gssapi-with-mic、publickey、keyboard-interactive、password  
debug3:authmethod_is_enabled gssapi-keyex  
debug1:下一个认证方法:gssapi-keyex  
debug1:没有有效的密钥交换上下文  
debug2:我们没有发送数据包,禁用方法  
debug3:authmethod_lookup gssapi-with-mic  
debug3:剩余首选:publickey、keyboard-interactive、password  
debug3:authmethod_is_enabled gssapi-with-mic  
debug1:下一个身份验证方法:gssapi-with-mic  
debug1:未指定的 GSS 故障。次要代码可以提供更多信息  
没有可用的 Kerberos 凭据(默认缓存:KEYRING:persistent:0)  
  
debug1:未指定的 GSS 故障。次要代码可以提供更多信息  
没有可用的 Kerberos 凭据(默认缓存:KEYRING:persistent:0)  
  
debug2:我们没有发送数据包,禁用方法  
debug3: authmethod_lookup 公钥  
debug3:剩余首选:键盘交互,密码  
debug3:authmethod_is_enabled 公钥  
debug1:下一个认证方式:publickey  
debug1:提供 RSA 公钥:/root/.ssh/id_rsa  
调试3:send_pubkey_test  
debug2:我们发送了一个公钥包,等待回复  
debug1:可以继续的身份验证:publickey、gssapi-keyex、gssapi-with-mic、password  
debug1:尝试私钥:/root/.ssh/id_dsa  
debug3:没有这样的身份:/root/.ssh/id_dsa:没有这样的文件或目录  
debug1:尝试私钥:/root/.ssh/id_ecdsa  
debug3:没有这样的身份:/root/.ssh/id_ecdsa:没有这样的文件或目录  
debug2:我们没有发送数据包,禁用方法  
debug3:authmethod_lookup 密码  
debug3:剩余首选:,密码  
debug3:authmethod_is_enabled 密码  
debug1:下一个认证方式:密码  
nobrk1n@10.213.23.112 的密码:  
debug3: packet_send2: 添加 64 (len 60 padlen 4 extra_pad 64)  
debug2:我们发送了一个密码包,等待回复  
debug1:认证成功(密码)。  
已通过 10.213.23.112 ([10.213.23.112]:2222) 的身份验证。  
debug1:通道 0:新的 [client-session]  
调试3:ssh_session2_open:channel_new:0  
debug2:通道 0:发送打开  
debug1:请求 no-more-sessions@openssh.com  
debug1:进入交互式会话。  
调试1:client_input_global_request:rtype hostkeys-00@openssh.com want_reply 0  
debug2:回调开始  
debug2: fd 3 设置 TCP_NODELAY  
debug3: packet_set_tos: 设置 IP_TOS 0x10  
调试2:client_session2_setup:id 0  
debug2:通道 0:请求 pty-req 确认 1  
debug1:发送环境。  
debug3:忽略环境 XDG_SESSION_ID  
debug3:忽略 env HOSTNAME  
debug3:忽略 env TERM  
debug3:忽略 env SHELL  
debug3:忽略 env HISTSIZE  
debug3:忽略环境 SSH_CLIENT  
debug3:忽略环境 SSH_TTY  
debug3:忽略环境用户  
debug3:忽略环境 LS_COLORS  
debug3:忽略 env MAIL  
debug3:忽略环境路径  
debug3:忽略 env PWD  
debug1:发送 env LANG = en_US.UTF-8  
debug2:通道 0:请求 env 确认 0  
debug3:忽略 env HISTCONTROL  
debug3:忽略环境 SHLVL  
debug3:忽略 env HOME  
debug3:忽略 env LOGNAME  
debug3:忽略环境 XDG_DATA_DIRS  
debug3:忽略 env SSH_CONNECTION  
debug3:忽略 env LESSOPEN  
debug3:忽略环境 XDG_RUNTIME_DIR  
debug3:忽略 env _  
debug2:通道 0:请求 shell 确认 1  
debug2:回调完成  
debug2:通道 0:打开确认 rwindow 0 rmax 32768  
debug2:channel_input_status_confirm:类型 99 id 0  
debug2: 在通道 0 上接受 PTY 分配请求  
debug2:通道 0:rcvd 调整 2097152  
debug2:channel_input_status_confirm:类型 99 id 0  
debug2:在通道 0 上接受了 shell 请求  
最后登录时间:2018 年 12 月 11 日星期二 21:17:10 从 10.213.23.201  
请输入shell密码:debug3: Received SSH2_MSG_IGNORE  
debug3:收到 SSH2_MSG_IGNORE  
debug3:收到 SSH2_MSG_IGNORE  
debug3:收到 SSH2_MSG_IGNORE  
debug3:收到 SSH2_MSG_IGNORE  
debug3:收到 SSH2_MSG_IGNORE  
debug3:收到 SSH2_MSG_IGNORE  
debug3:收到 SSH2_MSG_IGNORE  
debug3:收到 SSH2_MSG_IGNORE  
debug3:收到 SSH2_MSG_IGNORE  
debug3:收到 SSH2_MSG_IGNORE  
debug3:收到 SSH2_MSG_IGNORE  
debug3:收到 SSH2_MSG_IGNORE  
debug3:收到 SSH2_MSG_IGNORE  
debug3:收到 SSH2_MSG_IGNORE  
debug3:收到 SSH2_MSG_IGNORE  
debug3:收到 SSH2_MSG_IGNORE  
  
进入外壳...  
[root@atd-reg 根]#

我无法执行这样的命令:

ssh -vvv -p2222 nobrk1n@10.213.23.112 ls

在执行上述命令时,系统会提示我输入 nobrk1n 用户密码。但是在输入密码时它会卡住。我已将上述命令的输出粘贴到https://pastebin.com/hSfiCmdi。通常我首先使用 ssh 进入服务器ssh -p2222 user@host,当建立连接并成功登录时,我开始执行命令。

标签: pythonsshparamikomulti-factor-authentication

解决方案


您的服务器对第一个密码使用标准密码身份验证。

只有在 shell 启动时才会询问第二个密码。简单 I/O 用于此目的。

此外,您的服务器似乎不支持“exec”接口/通道来执行命令(因为ssh user@host command不起作用)。什么可能与“shell密码”功能有关。所以你可能不得不使用“shell”通道来执行你的命令,否则不推荐

ssh = paramiko.SSHClient()
ssh.connect(hostname, username = username, password = password1)
channel = ssh.invoke_shell()
channel.send(password2 + "\n")
channel.send(command + "\n")
while not channel.recv_ready():
    time.sleep(1)
out = channel.recv(9999)

推荐阅读