检查是否设置了无密码访问
我将通过 python 脚本测试是否已设置无密码 ssh 登录。 如果我运行普通的 ssh 命令,那么它将等待一段时间接受密码。 有没有办法让 ssh 命令在 ssh 要求输入密码时立即返回错误。
有可能实现这一目标吗?
I would test via a python script whether a passwordless ssh login has been setup or not.
If i run the normal ssh command then it will wait to accept the password for some amount of time.
Is there a way where the ssh command should return an error as soon as ssh asks for a password.
Is it possible to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
ssh 命令接受相当大的选项集,因此假设您的 Python 代码正在调用 shell 命令,请执行以下操作:
如果尚未设置密钥,这将立即失败,如果已设置,则立即完成,这就是原因命令被传递到主机。这为您提供了一个简单的测试。
The ssh command accepts quite a large set of options, so assuming that your Python code is calling shell commands do the following :
This will instantly fail if the keys have not been setup, and instantly complete if they have, which is the reason for the command being passed to the host. This gives you a simple test.
如果您没有将密码传递给
SSHClient
的.connect()
方法并且无法获取工作密钥,paramiko 将引发AuthenticationException
成立。paramiko will raise an
AuthenticationException
if you don't pass a password to theSSHClient
's.connect()
method and a working key cannot be found.您可以为 ssh 命令设置一个参数,以便在身份验证过程中仅尝试公钥,并设置批处理模式,该命令在远程主机中执行该命令时始终返回 true。通过这种方法,您可以捕获 var $? 中的退出代码。并做一些决定。
您可以通过在 ssh 命令的详细输出中查找“权限被拒绝”来捕获自定义输出到变量,如以下代码所示。
我希望这对您有用。
You can set a parameter to ssh command to only try the public key in the authentication process and set a batch mode with a command that always return true when the command is executed in the remote host. With that approach you can capture the exit code in the var $? and do some decision.
You can capture custom output to a variable by finding the "Permission denied" in the verbose output of the ssh command as is in the following code.
I hope this find you useful .
您可以使用
PasswordAuthentication
选项Python 代码
You may use the
PasswordAuthentication
optionPython code