sshpass-Linux命令之非互動SSH密碼驗證
阿新 • • 發佈:2020-09-09
sshpass-Linux命令之非互動SSH密碼驗證
ssh登陸不能在命令列中指定密碼。sshpass的出現,解決了這一問題。sshpass用於非互動SSH的密碼驗證,一般用在sh指令碼中,無須再次輸入密碼。
它允許你用-p引數指定明文密碼,然後直接登入遠端伺服器,它支援密碼從命令列、檔案、環境變數中讀取。
其預設沒有安裝,需要手動安裝,方法如下:
sshpass下載地址:http://sourceforge.net/projects/sshpass/下載為一個tar.gz的壓縮包。
$tar -zxvfsshpass-1.05.tar.gz
$cdsshpass-1.05
$./configure--prefix=/opt/sshpass#指定安裝目錄
$make
$makeinstall
$cp/opt/sshpass/bin/sshpass/usr/bin/
經過以上步驟,sshpass安裝完成,輸入命令sshpass如出現如下提示即安裝成功:
2.用法介紹
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
-p password #後跟密碼
[root@zhu ~] # sshpass -p 123456 ssh [email protected]
Last login: Wed Apr 16 15:35:22 2014 from 192.168.56.1
[root@jiang ~] # exit
logout
Connection to 192.168.56.102 closed.
-f filename #後跟儲存密碼的檔名,密碼是檔案內容的第一行。
[root@zhu ~] # cat 1.txt
123456
[root@zhu ~] # sshpass -f 1.txt ssh [email protected]
Last login: Fri Apr 18 13:48:20 2014 from 192.168.56.101
[root@jiang ~] # exit
logout
Connection to 192.168.56.102 closed.
-e #將環境變數SSHPASS作為密碼 [root@zhu ~] # export SSHPASS=123456
[root@zhu ~] # sshpass -e ssh [email protected]
Last login: Fri Apr 18 13:51:45 2014 from 192.168.56.101
[root@jiang ~] # exit
logout
Connection to 192.168.56.102 closed.
|
#從命令列方式傳遞密碼 -p指定密碼
$sshpass-p '123456' sshuser_name@host_ip
$sshpass-p '123456' scproot@host_ip:/home/test/t./tmp/
如在多臺主機執行命令:
1 2 3 4 5 6 7 |
[root@zhu ~] # cat a.sh
#!/bin/bash
for i in $( cat /root/1 .txt)
do
echo $i
sshpass -p123456 ssh root@$i 'ls -l'
done
|