Ssh

如何從 cron 作業自動進行 SSH 身份驗證和 CVS 更新?

  • January 29, 2015

我從 cron 作業中呼叫腳本。在腳本中,它需要執行 CVS 命令以從遠端儲存庫進行更新。為此,它需要進行 SSH 身份驗證。

我將以下程式碼放入腳本中

...
ROOTPATH="/Users/qazwsx/project/"

cd $ROOTPATH

SSHAGENT="${ROOTPATH}ssh-agent.cf"

if [ -f "$SSHAGENT" ]; then
   rm "$SSHAGENT"
fi

ssh-agent -s | head -n 2 > "$SSHAGENT"

if [ -f "$SSHAGENT" ]; then
       echo "$SSHAGENT"
       source "$SSHAGENT"
       ssh-add
       #function killsshagent {
   #    /bin/kill $SSH_AGENT_PID
   #}
   #trap killsshagent EXIT
   rm "$SSHAGENT"
fi
...

但是當腳本執行時,會出現如下視窗:

在此處輸入圖像描述

而且我無法在輸入欄位中輸入任何內容。所以 SSH 認證後的步驟沒有成功執行,因為 SSH 訪問沒有建立。我想知道我應該怎麼做才能在 cron 工作中進行 CVS 更新。

您可以創建另一個密鑰對,無需密碼:

$ ssh-keygen -t rsa -b 4096 -f ~/.ssh/cvs_rsa.id

然後將~/.ssh/cvs_rsa.id.pub的內容添加到遠端主機的authorized_keys中。然後,您應該能夠從您的腳本中“ssh -i ~/.ssh/cvs_rsa.id user@remotehost”。

請注意,在創建沒有密碼的密鑰時,任何擁有私鑰文件的人都可以以您的身份登錄。謹慎使用!

更新:來自 ssh-keygen (1) 手冊頁:

ssh-keygen [-q] [-b bits] -t type [-N new_passphrase] [-C comment] [-f output_keyfile]

-b bits
        Specifies the number of bits in the key to create.  For RSA keys, the minimum size is 768 bits and the default is 2048 bits.  Generally, 2048 bits is considered sufficient.  DSA keys must be exactly 1024 bits as speci‐
        fied by FIPS 186-2.  For ECDSA keys, the -b flag determines the key length by selecting from one of three elliptic curve sizes: 256, 384 or 521 bits.  Attempting to use bit lengths other than these three values for
        ECDSA keys will fail.

-t type
        Specifies the type of key to create.  The possible values are “rsa1” for protocol version 1 and “dsa”, “ecdsa” or “rsa” for protocol version 2.

因此,基本上該命令說,製作一個 4096 位 RSA 密鑰並將其作為 cvs_rsa.id 儲存在目前使用者主目錄的 .ssh 目錄中。~/.ssh/ 擴展為 $ HOME/.ssh/ for whatever value of $ HOME 已設置。

引用自:https://serverfault.com/questions/663260