sshpass是一款凡是为凡是使用ssl方式访问的操作提供一个免输入密码的非交互式操作,以便于在脚本中执行ssl操作,如ssh,scp等。sshpass是一家以色列公司Lingnu开发的,由于软件还处于初期,bug还是很有可能出现的。所以使用这个软件时要慎重。
sshpass安装
源码安装
curl -O -L http://downloads.sourceforge.net/project/sshpass/sshpass/1.06/sshpass-1.06.tar.gz && tar xvzf sshpass-1.06.tar.gz && cd sshpass-1.06 && ./configure && make && sudo make install
自己安装软件包
rpm -ivh $BaseDir/tools/sshpass/sshpass-1.06-1.el6.x86_64.rpm
自动安装软件包
# yum -y install sshpass
sshpass用法
sshpass [-ffilename|-dnum|-ppassword|-e] [options] command arguments
-p 直接在命令行给出密码
sshpass -p '123' ssh root@192.168.1.1 'ls -l'
-f 文件首行给出密码。
sshpass -f file.txt ssh root@192.168.1.1 'ls -l'
-e 由环境变量SSHPASS给出密码。
export SSHPASS='123'
sshpass -e ssh root@192.168.1.1 'ls -l'
-d 由文件描述符给出密码。
sshpass -d 51671 ssh root@192.168.1.1 'ls -l'
对于ssh的第一次登陆,会提示:“Are you sure you want to continue connecting (yes/no)”,这时用sshpass会不好使,可以在ssh命令后面加上 -o StrictHostKeyChecking=no来解决。比如说上面的命令,就可以写作ssh -p efghi scp abc@192.168.0.5:/home/xxx/test /root -o StrictHostKeyChecking=no。
sshpass帮助
Usage: sshpass [-f|-d|-p|-e] [-hV] command parameters -f filename Take password to use from file -d number Use number as file descriptor for getting password -p password Provide password as argument (security unwise) -e Password is passed as env-var "SSHPASS" With no parameters - password will be taken from stdin -P prompt Which string should sshpass search for to detect a password prompt -v Be verbose about what you're doing -h Show help (this screen) -V Print version information At most one of -f, -d, -p or -e should be used