
我想透過 SSH 連接到遠端計算機,但我不想輸入密碼,而是想從另一個文件重定向它。
password.txt
所以我有一個儲存密碼的檔案。File.sh
是我的 bash 文件。
在File.sh
#!/bin/bash
ssh -T [email protected]
在執行該文件時,我做了:
./File.sh < password.txt
但無論如何我都被要求輸入密碼。
如何從文件中取得要輸入的密碼?
答案1
帶有「檔案中的密碼」的 SSH 通常用作公鑰身份驗證。使用 建立金鑰對ssh-keygen
,將公鑰上傳到另一台主機:
scp ./.ssh/id_rsa.pub [email protected]:~/
並將其放置為~/.ssh/authorized_keys
:
ssh [email protected]
mkdir ~/.ssh
mv ~/id_rsa.pub ~/.ssh/authorized_keys
或者,如果authorized_keys
文件已存在:
cat ~/id_rsa.pub >> ~/.ssh/authorized_keys
設定適當的權限(檔案為600,目錄為700):
chmod 600 ~/.ssh/authorized_keys
chmod 700 ~/.ssh
並啟動一個新的 ssh 會話。
答案2
您可以使用sshpass
以下方法來做到這一點:
sshpass -f password.txt ssh -T [email protected]
答案3
#!/usr/bin/expect -f
spawn ssh shw@hostname
expect -exact "shw@hostname's password: "
send -- "PASSWORD\r"
expect "$ "
interact
Expect 是一個自動化流程的工具。