在 bash 腳本中使用 ssh 金鑰

在 bash 腳本中使用 ssh 金鑰

我嘗試使用 openssh 和私鑰/公鑰從 Windows 2003 伺服器連接到 unix 伺服器,但一直要求提供私鑰密碼和 Windows 密碼。

我已經在 linux 上的授權金鑰上新增了公鑰(如果我使用 putty 連線並且私鑰工作正常)。但我需要使用 bash 腳本進行連接,而不需要請求密碼和密碼短語。

這是命令:

ssh -v -i privatefile -p 2022 user@server

這是日誌:

OpenSSH_3.7.1p1, SSH protocols 1.5/2.0, OpenSSL 0.9.7b 10 Apr 2003
debug1: Connecting to SERVER port 2022.
debug1: Connection established.
debug1: identity file privatefile type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.4
debug1: match: OpenSSH_7.4 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_3.7.1p1
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-cbc hmac-sha1 none
debug1: kex: client->server aes128-cbc hmac-sha1 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Host 'SERVER' is known and matches the RSA host key.
debug1: Found key in /cygdrive/c/Users/PAMA1/.ssh/known_hosts:1
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
D:\Entrada\z_Bkp>NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received

Anyone gaining unauthorized access will be subject to PROSECUTION.

debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mi
c,password,keyboard-interactive
debug1: Next authentication method: publickey
debug1: Trying private key: bilaetlopenssh
Enter passphrase for key 'bilaetlopenssh':
debug1: Next authentication method: keyboard-interactive
Enter your Windows password :

答案1

如果使用putty,你可以使用puttygen動態儲存金鑰私鑰,輸入密碼金鑰一次(即直到重新啟動)。

建議的操作是保存與遠端主機、遠端使用者和使用的金鑰的會話,並使用該會話執行命令會議

\path\name\to\putty.exe -load "mysession"

所以要使用膩子複製pscp

\path\name\to\pscp.exe -load "mysession" local-file.txt remote:/path/file.txt
\path\name\to\pscp.exe -load "mysession" remote:/path/file.txt local-file.txt

在unix世界中puttygen被稱為ssh-add,我沒有WSL來測試它。

相關內容