sshpass
一個簡單、輕量級命令行工具,提供非交互式密碼驗證
原理
- ssh 直接使用 TTY 訪問,以確保密碼是用戶鍵盤輸入的。 sshpass 在專門的 tty 中運行 ssh,以誤導 ssh 相信它是從用戶接收到的密碼
- 使用 sshpass 是不安全的,因為所有系統上的用戶可以看到密碼。因此,在生產環境,建議使用 密鑰登錄。
安裝
sudo apt install sshpass
命令
sshpass [options] ssh user@host
options
-p password
直接提供密碼
sshpass -p 'my_pass_here' ssh aaronkilik@10.42.0.1 'df -h'
-e
讀取環境變量 SSHPASS
在bashrc
中添加 SSHPASS
環境變量,配置登錄密碼
export SSHPASS='PmN8uq48tBGwrMCY'
sshpass -e ssh user@host
# 配置別名使用更香
alias sshtup='sshpass -e ssh root@192.20.0.164 '"'"'sh /root/update.sh'"'"''
-f filename
從文件中讀取密碼
sshpass -f password_filename ssh aaronkilik@10.42.0.1 'df -h'