使用sshpass在登录linux主机的时候实现免密钥登录
centos安装sshpass,首先需要安装epel源
First, enable EPEL repo and type the following yum command:
$ sudo yum install sshpass
测试
ssh登录主机名为server.example.com 密码为: t@uyM59bQ:
$ sshpass -p 't@uyM59bQ' ssh [email protected]
在脚本中需要禁用host key检查(一般这样用)
$ sshpass -p 't@uyM59bQ' ssh -o StrictHostKeyChecking=no [email protected]
注意点:密码直接在命令行不安全,建议使用下边的格式
语法为:
SSHPASS='t@uyM59bQ' sshpass -e ssh [email protected]
SSHPASS='t@uyM59bQ' sshpass -e ssh [email protected] date
SSHPASS='t@uyM59bQ' sshpass -e ssh [email protected] w
SSHPASS='t@uyM59bQ' sshpass -e ssh -o StrictHostKeyChecking=no [email protected]
把密码保存在一个文件中使用-f选项读取,语法格式如下:
sshpass -f fileNameHere ssh user@server
步骤如下:
$ echo 'myPassword' > myfile
$ chmod 0400 myfile
$ sshpass -f myfile ssh [email protected]
在rsync中使用sshpass两种形式;
Run rsync over SSH using password authentication, passing the password on the command line:
$ rsync --rsh="sshpass -p myPassword ssh -l username" server.example.com:/var/www/html/ /backup/
或者
$ SSHPASS='yourPasswordHere' rsync --rsh="sshpass -e ssh -l username" server.example.com:/var/www/html/ /backup/
使用gpg加密密码:
How do I use sshpass with gpg encrypted file?
First, create a file as follows:(步骤如下)
$ echo 'mySshPasswordHere' > .sshpassword
Now, encrypt a file using gpg command:
$ gpg -c .sshpassword
$ rm .sshpassword
Finally, use it as follows:
$ gpg -d -q .sshpassword.gpg > fifo; sshpass -f fifo ssh [email protected]
查看sshpass的帮助如下: