安裝
tar zxvf sshpass-1.05.tar.gz
cd sshpass-1.05
./configure --prefix=/opt/sshpass
make
make install
結合使用:
sshpass 和ssh,scp,sftp一起使用
實例:
[root@fs bin]# ./sshpass -p 123456 ssh -o StrictHostKeyChecking=no [email protected] "ls -t | head -n 1"
[root@fs bin]# ./sshpass -p 123456 scp -o StrictHostKeyChecking=no /root/abc.sh 192.168.1.15:/root
-p:指定ssh的密碼
-o StrictHostKeyChecking=no 避免第一次登錄出現公鑰檢查。也就是避免出現
sshpass: 用於非交互的ssh 密碼驗證
ssh登陸不能在命令行中指定密碼,也不能以shell中隨處可見的,sshpass 的出現,解決了這一問題。它允許你用 -p 參數指定明文密碼,然後直接登錄遠程服務器。 它支持密碼從命令行,文件,環境變量中讀取
$> sshpass -h
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
-h Show help (this screen)
-V Print version information
At most one of -f, -d, -p or -e should be used
sshpass [-f|-d|-p|-e] [-hV] command parameters 中的 command parameters 和使用交互式密碼驗證的使用方法相同
#從命令行方式傳遞密碼
$> sshpass -p user_password scp -P22 192.168.1.2:/home/test/t .
#從文件讀取密碼
$> echo "user_password" > user.passwd
#從環境變量獲取密碼
$> export SSHPASS="user_password"
----------------------------------------------------------------------------------------------------
用sshpass實現ssh的自動登陸
要實現ssh自動登錄,網上搜了一下,主要有兩種方法:1、生成公鑰。2、編寫expect腳本。這兩種方法,用起來都有點複雜。在新立得上安裝ssh的時候,偶然發現一個sshpass,百度谷歌之,英文資料甚多,而中文資料寥寥。其實sshpass的用法很簡單。
用法:
sshpass 參數 SSH命令(ssh,sftp,scp等)。
參數:
-p password //將參數password作爲密碼。
-f passwordfile //提取文件passwordfile的第一行作爲密碼。
-e //將環境變量SSHPASS作爲密碼。
比如說:
scp [email protected]:/home/xxx/test /root 這個命令的作用是將服務器端文件test傳到本地文件夾/root下。
利用sshpass,假設密碼爲efghi,則可寫作:
ssh -p efghi scp [email protected]:/home/xxx/test /root
另外,對於ssh的第一次登陸,會提示:“Are you sure you want to continue connecting (yes/no)”,這時用sshpass會不好使,可以在ssh命令後面加上 -o StrictHostKeyChecking=no來解決。比如說上面的命令,就可以寫作ssh -p efghi scp [email protected]:/home/xxx/test /root -o StrictHostKeyChecking=no。
原文地址:http://wkm.iteye.com/blog/1972155