使用sshpass在登錄linux主機的時候?qū)崿F(xiàn)免密鑰登錄
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 username@server.example.com
在腳本中需要禁用host key檢查(一般這樣用)
$ sshpass -p 't@uyM59bQ' ssh -o StrictHostKeyChecking=no username@server.example.com
注意點:密碼直接在命令行不安全测砂,建議使用下邊的格式
語法為:
SSHPASS='t@uyM59bQ' sshpass -e ssh vivek@server42.cyberciti.biz
SSHPASS='t@uyM59bQ' sshpass -e ssh vivek@server42.cyberciti.biz date
SSHPASS='t@uyM59bQ' sshpass -e ssh vivek@server42.cyberciti.biz w
SSHPASS='t@uyM59bQ' sshpass -e ssh -o StrictHostKeyChecking=no vivek@server42.cyberciti.biz
把密碼保存在一個文件中使用-f選項讀取排抬,語法格式如下:
sshpass -f fileNameHere ssh user@server
步驟如下:
$ echo 'myPassword' > myfile
$ chmod 0400 myfile
$ sshpass -f myfile ssh vivek@server42.cyberciti.biz
在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 vivek@server1.cyberciti.biz
查看sshpass的幫助如下: