在Linux后台中,經常會用到ssh、scp等命令。需要進行認證,手動輸入密碼,是交互式的過程。
當將ssh、scp等命令做成自動化腳本時,可能需要非交互式的登錄過程,此時可以用sshpass。
安裝方式
1.在基於 RedHat/CentOS 的系統中,首先需要啟用 EPEL 倉庫並使用 yum 命令安裝它。 # yum install sshpass 2.在 Debian/Ubuntu 和它的衍生版中,你可以使用 apt-get 命令來安裝。 $ sudo apt-get install sshpass 3.mac環境 brew install sshpass 4.另外,你也可以從最新的源碼安裝 sshpass,首先下載源碼並從 tar 文件中解壓出內容: $ wget http://sourceforge.net/projects/sshpass/files/latest/download -O sshpass.tar.gz $ tar -xvf sshpass.tar.gz $ cd sshpass-1.06 $ ./configure # sudo make install
使用舉例
使用-p
參數指定登錄密碼
# 免密碼登錄 $ sshpass -p password ssh username@host # 遠程執行命令 $ sshpass -p password ssh username@host <cmd> # 通過scp上傳文件 $ sshpass -p password scp local_file root@host:remote_file # 通過scp下載文件 $ sshpass -p password scp root@host:remote_file local_file