標籤:shell
Shell命令sshpass非互動SSH密碼驗證
#安裝sshpasssshpass
:http://sourceforge.net/projects/sshpass/
下載為一個 tar.gz的壓縮包。
$ tar -zxvf sshpass-1.05.tar.gz
$ cd sshpass-1.05
$ ./configure --prefix=/opt/sshpass
#指定安裝目錄
$ make
$ make install
$ cp /opt/sshpass/bin/sshpass /usr/bin/
安裝完成
#查看協助
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
#!/bin/bash
cd $(dirname $0)
source ./config.sh
date=`date +%Y-%m-%d`
#上傳DUMP指令碼到服務端
sshpass -pyour_pwd scp -P22 ./dump.sh your_user@192.168.1.10:~/
#-o StrictHostKeyChecking=no 避免第一次登入出現公開金鑰檢查。
sshpass -pyour_pwd scp -o StrictHostKeyChecking=no -P22 ./dump.sh your_user@192.168.1.10:~/
#登陸到服務端地址,執行DUMP指令碼,將執行結果寫入本地檔案
sshpass -pyour_pwd ssh -p22 your_user @192.168.1.10 "sh ~/dump.sh" >local_result.txt
#使用檔案中的密碼
sshpass -f pwd.file ssh -p22 your_user@192.168.1.10 "sh ~/dump.sh" >>local_result.txt
#從環境變數(SSHPASS)讀取密碼
sshpass -e ssh -p22 your_user@192.168.1.10 "sh ~/dump.sh" >>local_result.txt
Shell命令sshpass非互動SSH密碼驗證