sshpass: 用于非交互的 ssh 密码验证

[撰文/Zhang Huangbin (michaelbibby AT gmail.com)]

OpenSSH 自带的 ssh 客户端程序(也就是 'ssh' 命令)默认不允许你以非交互的方式传递密码,如:

ssh www.iredmail.org <<EOF ssh_password ls /var/ EOF

Shell 里这样的输入重定向使用得非常普遍,而且通常都工作得很好。但是 ssh 不允许这样的方式来传递密码,所以需要远程连上服务器后进行的批处理就无法进行。

sshpass 的出现,解决了这一问题。它允许你用 -p 参数指定明文密码,然后直接登录远程服务器。例如:

# sshpass -p 'ssh_password' ssh www.iredmail.org

用 '-p' 指定了密码后,还需要在后面跟上标准的 ssh 连接命令。

用法就是这么简单。

注意:

我之前使用的 sshpass 是 1.0,在第一次连接服务器的时候,无法自动接受服务器的 Key 验证,也不会出现提示信息,所以第一次连接服务器请使用标准的 ssh 命令行客户端工具,接受了服务器的 key 之后再用 sshpass。

sshpass

Read More:

  • No related posts

11 Comments

  1. 1 dybnu Commented @ 2009-01-13 10:25Reply to this comment

    用ssh就不要用密码验证,openbsd的老大推荐

  2. 2 number5 Commented @ 2009-01-13 10:38Reply to this comment

    为什么不直接用publickey验证?

  3. 3 hmy Commented @ 2009-01-13 10:47Reply to this comment

    现实总不是理想的,哈哈,我刚接手管理的一些机器就是用ldap用户认证,不太方便放证书。 所以我也是刚用上这个软件,挺不错的。

    另外有个软件也不错,叫 sshproxy 有兴趣的同学自己去看

  4. 4 hmy Commented @ 2009-01-13 10:48Reply to this comment

    现在sshpass 支持 -f 选项,从文本文件读取密码,不用出现在命令行。

  5. 5 hmy Commented @ 2009-01-13 10:50Reply to this comment

    晕,再补充一下,把 StrictHostKeyChecking no 加到/etc/ssh/sshconfig 可以让ssh客户端自动接受新主机的hostkey,不用每次都自己输入yes

  6. 6 bones7456 Commented @ 2009-01-13 12:06Reply to this comment

    我曾经也给ssh写过一个patch,加上password参数,提交上去,人家说不安全,想想也是,于是就用自己的sssh了.

  7. 7 walkerxk Commented @ 2009-01-13 14:06Reply to this comment

    为什么不直接用expect?

  8. 8 Mike Commented @ 2009-01-13 14:27Reply to this comment

    为啥不用key登陆?

  9. 9 gougo Commented @ 2009-01-13 14:29Reply to this comment

    感觉这东西的功能用expect就能够很简单的实现了 .

  10. 10 feiy Commented @ 2009-01-13 17:18Reply to this comment

    就是,为什么不用expect,我多台服务器需要批量进行命令操作都是用expect +ssh做的

  11. 11 davix Commented @ 2009-01-14 12:52Reply to this comment

    我從來都是用公鑰,例如ssh-copy-id命令