使用python 实现SSH登录设备时出现问题?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用python 实现SSH登录设备时出现问题?相关的知识,希望对你有一定的参考价值。

想用python实现一个功能,即SSH远程登录设备。在实现的过程中遇到了一个问题:在使用Secure CRT登录设备时,除了用户名和密码验证之外,还会给手机发送一个实时的动态验证码,用户名、密码以及验证码都正确时,才可以登录该设备。
现在我想实现使用python远程SSH登录这个设备,一般设备只要求用户名和密码做验证时的写法如下面的代码所示。然后我想登录的这个设备,还需要手机收到的验证码来验证用户信息,请问在python中,有关验证码的这部分的代码应该怎么写?

import paramiko
s = paramiko.SSHClient()

s.set_missing_host_key_policy(paramiko.AutoAddPolicy())

s.connect(hostname='xxxx', port=22, username='xxxx', password='xxxx')

解决了啊,不过不是用的
stdin,stdout,stderr=client.exec_command('show arp;show clock')
用这种方法只能弄一条命令
用下面这个
chan= client.invoke_shell()
chan.send('en\n')
chan.send("password\n")
chan.send('show log\n')
result = chan.recv(100000).decode()
用了这个可能还有别的问题,试试吧,不行再讨论追问

可以详细解释一下您的代码是什么意思吗

参考技术A 使用佩斯登陆时间出现问题就是用贝克斯简答就行。

尝试使用特定用户通过 ssh 登录时出现“写入失败:管道损坏”

【中文标题】尝试使用特定用户通过 ssh 登录时出现“写入失败:管道损坏”【英文标题】:"Write Failed: Broken Pipe" when trying to login through ssh with a specific user 【发布时间】:2013-08-15 17:15:38 【问题描述】:

我正在尝试设置 SSHFTP 服务器,通过 apache@localhost 连接后,我立即断开连接并显示“写入失败:管道损坏”错误消息。我可以很好地连接到jack@localhost,但不能连接到用户apache

这些是我添加到 sshd_config 的唯一设置(我只想在 apache 工作时允许它):

Match User apache
    ChrootDirectory /apache
    AllowTCPForwarding no
    X11Forwarding no
    ForceCommand /usr/lib/openssh/sftp-server
Match
#AllowUsers apache

这是我添加到 ssh_config 的内容:

ServerAliveInterval 120
TCPKeepAlive no

我确保用户 apache 对 /apache 文件夹具有完全权限,并且我可以以该用户身份登录并在终端中修改项目。该文件夹只有 2 个文件:index.html 和 test.php

我也去了网络上的另一台电脑,用FileZilla以jack用户登录。效果很好。

这是我尝试连接时终端的日志。

jack@JacksServer:~$ ssh -v apache@localhost
OpenSSH_5.9p1 Debian-5ubuntu1.1, OpenSSL 1.0.1 14 Mar 2012
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to localhost [127.0.0.1] port 22.
debug1: Connection established.
debug1: identity file /home/jack/.ssh/id_rsa type -1
debug1: identity file /home/jack/.ssh/id_rsa-cert type -1
debug1: identity file /home/jack/.ssh/id_dsa type -1
debug1: identity file /home/jack/.ssh/id_dsa-cert type -1
debug1: identity file /home/jack/.ssh/id_ecdsa type -1
debug1: identity file /home/jack/.ssh/id_ecdsa-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.9p1 Debian-5ubuntu1.1
debug1: match: OpenSSH_5.9p1 Debian-5ubuntu1.1 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.9p1 Debian-5ubuntu1.1
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: sending SSH2_MSG_KEX_ECDH_INIT
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ECDSA bb:f3:74:9d:97:80:89:dc:d9:68:53:5c:f7:25:19:4e
debug1: Host 'localhost' is known and matches the ECDSA host key.
debug1: Found key in /home/jack/.ssh/known_hosts:1
debug1: ssh_ecdsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Trying private key: /home/jack/.ssh/id_rsa
debug1: Trying private key: /home/jack/.ssh/id_dsa
debug1: Trying private key: /home/jack/.ssh/id_ecdsa
debug1: Next authentication method: password
apache@localhost's password: 
debug1: Authentication succeeded (password).
Authenticated to localhost ([127.0.0.1]:22).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
Write failed: Broken pipe
jack@JacksServer:~$

Ubuntu 版本信息:

Distributor ID: Ubuntu
Description:    Ubuntu 12.04.2 LTS
Release:    12.04
Codename:   precise

编辑:

我运行命令sudo grep -ir ssh /var/log/* 得到了这个:

/var/log/auth.log~:Aug 13 16:08:15 JacksServer sshd[32292]: fatal: bad ownership or modes for chroot directory "/apache"
/var/log/auth.log~:Aug 13 16:08:15 JacksServer sshd[32156]: pam_unix(sshd:session): session closed for user apache

我尝试按照某些网站的建议执行 chmod 755 /apache 并更改所有权,但我仍然收到此错误。该文件夹当前归用户apache所有。

更多关于 apache 用户的信息:

root@JacksServer:/apache# ls -la /apache
total 24
drwxr-xr-x  4 apache root    4096 Aug 13 15:49 .
drwxr-xr-x 29 root   root    4096 Aug 13 03:06 ..
drwxr-xr-x  2 apache nogroup 4096 Aug 13 13:57 .cache
-rw-r--r--  1 apache root       5 Aug 13 03:56 index.html
-rw-r--r--  1 apache root       0 Aug 13 03:56 index.html~
drwxr-xr-x  8 apache root    4096 Aug 13 15:51 .ssh
-rw-r--r--  1 apache root      20 Aug 13 03:59 test.php
-rw-r--r--  1 apache root       0 Aug 13 03:59 test.php~
root@JacksServer:/apache# ls -la /apache/.ssh
total 92
drwxr-xr-x  8 apache root  4096 Aug 13 15:51 .
drwxr-xr-x  4 apache root  4096 Aug 13 15:49 ..
-rw-r--r--  1 apache jack   220 Jun  6 20:08 .bash_logout
drwxr-xr-x 19 apache jack  4096 Aug 13 15:04 .cache
drwxr-xr-x  3 apache jack  4096 Jun  6 20:49 .compiz-1
drwxr-xr-x  3 apache jack  4096 Jun  6 20:22 .dbus
drwxr-xr-x  5 apache jack  4096 Aug 13 05:37 .gconf
-rw-r-----  1 apache jack     0 Aug 13 15:47 .gksu.lock
drwxr-xr-x  3 apache jack  4096 Jun  6 20:22 .local
-rw-r--r--  1 apache jack   675 Jun  6 20:08 .profile
-rw-------  1 apache jack   256 Jun  6 20:22 .pulse-cookie
drwxr-xr-x  2 apache jack  4096 Aug 13 14:20 .ssh
-rw-------  1 apache jack    56 Aug 13 04:45 .Xauthority
-rw-------  1 apache jack 44215 Aug 13 13:54 .xsession-errors

【问题讨论】:

向我们展示 ls -la /apache ls -la /apache/.ssh (都假设 /apache 是该用户的主目录,而 /etc/passwd 是 apache 的条目。 我将这些命令的结果添加到原始帖子中。 /apache 是用户 apache 的主目录。 OK 玩弄权限后,我将文件夹设置为root控制,并将其更改为755。现在我收到错误/bin/false: No such file or directory 我将ForceCommand /usr/lib/openssh/sftp-server 更改为ForceCommand internal-sftp,现在它在我执行SFTP 时有效!感谢您为我指明正确的方向。 【参考方案1】:

我已经解决了我在 Mac OS X 中的问题,我已经更改了

#ClientAliveInterval 0

ClientAliveInterval 300

在 this 之后的 /etc/sshd_config 中,然后重新启动机器,完成。

【讨论】:

【参考方案2】:

由于太长,作为另一个答案发布:

在我的发现中,ChrootDirectory 的这一要求使其毫无用处。为什么某人的主目录对该用户不可写?瘸。在 Chroot Dirs 中也无法访问符号链接。

但是 - 我找到了一个很好的解决方法。我创建了一个/home/chroot/outsource 目录,并在其中使用mount --bind 将系统中的另一个目录/var/www/clients/theclient 挂载到/home/chroot/outsource/theclient

由于mount --bind 在重启之前只是暂时的,所以我在/etc/fstab 中添加了这一行:

/var/www/clients/theclient /home/chroot/outsource/theclient none bind,auto,user,noexec 0 0

【讨论】:

【参考方案3】:

这两个问题的答案都在这个帖子里:https://askubuntu.com/questions/134425/how-can-i-chroot-sftp-only-ssh-users-into-their-homes

“基本上chroot目录必须归root所有,不能有任何组写权限。”

这意味着您需要确保您的 ChrootDirectory (/apache) 归 root 所有,并且不能被任何组/其他人写入。这是由于一些模糊的 setuid 相关安全风险outlined here。

另外,要回答关于/bin/false: No such file or directory 的问题:尝试改用/sbin/nologin

【讨论】:

是的,我应该回答我自己的问题。这就是我使用 apache 和 sftp 设置它的方式:/apache 是 755,/apache/www 是 775,/apache/www/public_html 是网站的顶层。我将 /apache 设置为由 root:website-apache 递归拥有(website-apache 仅包含用户 apache)。 你是神!!谢谢你救了我的命

以上是关于使用python 实现SSH登录设备时出现问题?的主要内容,如果未能解决你的问题,请参考以下文章

尝试使用特定用户通过 ssh 登录时出现“写入失败:管道损坏”

ssh登录时出现闪退问题

阿里云服务器教程–SSH 登录时出现如下错误:Host key verification failed

尝试使用 JWT 创建登录时出现“不是函数”错误

SSH 登录时出现如下错误:Disconnected:No supported authentication methods available

Python 自动化运维实现ssh 远程登录管理设备