1. 程式人生 > >sshpass之非交互SSH密碼驗證

sshpass之非交互SSH密碼驗證

sshpass

SSH登陸不能在命令行中指定密碼。sshpass用於非交互SSH的密碼驗證,一般用在shell腳本中,無須手動輸入密碼。它允許你用 -p 參數指定明文密碼,然後直接登錄遠程服務器,它支持密碼從命令行、文件、環境變量中讀取。


一、安裝sshpass:

1. 通過源碼編譯安裝

# http://sourceforge.net/projects/sshpass/ 下載地址
$ tar -zxvf sshpass-xx.tar.gz
$ ./configure
$ make && make install

2. 通過源服務器安裝

$ sudo apt-get install sshpass


二、使用sshpass

1. 登錄遠程服務器

$ sshpass -p password ssh username@ip_addr

2. scp上傳或下載文件

$ sshpass -p password scp username@ip_addr:/remote_dir /local_dir
$ sshpass -p password scp /local_dir username@ip_addr:/remote_dir

3. sshpass命令詳細用法

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

   -P prompt     Which string should sshpass search for to detect a password prompt
   -v            Be verbose about what you're doing
   -h            Show help (this screen)
   -V            Print version information
At most one of -f, -d, -p or -e should be used


sshpass之非交互SSH密碼驗證