![ジャンプサーバーが提供する可変ホスト名を使用してSSH接続を設定する方法](https://rvso.com/image/789293/%E3%82%B8%E3%83%A3%E3%83%B3%E3%83%97%E3%82%B5%E3%83%BC%E3%83%90%E3%83%BC%E3%81%8C%E6%8F%90%E4%BE%9B%E3%81%99%E3%82%8B%E5%8F%AF%E5%A4%89%E3%83%9B%E3%82%B9%E3%83%88%E5%90%8D%E3%82%92%E4%BD%BF%E7%94%A8%E3%81%97%E3%81%A6SSH%E6%8E%A5%E7%B6%9A%E3%82%92%E8%A8%AD%E5%AE%9A%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95.png)
ログイン ノード (ジャンプ サーバー) に接続し、コマンドを実行してそのノードからホスト名を取得し、ログイン ノードに使用したのと同じユーザー名と資格情報を使用してそのホスト名に ssh する必要がある場合に備えて、.sshconfig ファイルを作成したいと思います。最終的に作成した構成は次のとおりです。
Host jump-server
HostName loginnode
User user
Host remote
User user
IdentityFile C:\Users\user\.ssh\id_rsa
ForwardX11 yes
ForwardX11Trusted yes
XAuthLocation /usr/bin/xauth
ProxyCommand ssh -q jump-server "ssh user@`command_to_get_the_hostname` bash -l"
次のコマンドを使用して ssh を実行しようとしています:
ssh.exe -v remote
そして、次のような出力が得られました:
OpenSSH_for_Windows_8.1p1, LibreSSL 3.0.2
debug1: Reading configuration data C:\\Users\\user/.ssh/config
debug1: C:\\Users\\user/.ssh/config line 14: Applying options for remote
debug1: Executing proxy command: exec ssh -q jump-server "ssh lemoni15@`command_to_get_the_hostname` bash -l"
debug1: identity file C:\\Users\\user\\.ssh\\id_rsa type 0
debug1: identity file C:\\Users\\user\\.ssh\\id_rsa-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_for_Windows_8.1
: command not foundSH_for_Windows_8.1
答え1
RemoteCommand
ジャンプ ホストだけがリモート ホストのホスト名を知っているとおっしゃいましたね。 はProxyCommand
ローカル マシン上で実行されるため、command_to_get_the_hostname
ジャンプ ホスト上で が実行されることはありません。最初にジャンプ ホストに接続してからそこでコマンドを実行するには、RemoteCommand
代わりに を使用できます。
RemoteCommand
サーバーへの接続に成功した後にリモート マシンで実行するコマンドを指定します。コマンド文字列は行末まで拡張され、ユーザーのシェルで実行されます。 - -
例えば
Host remote
Hostname jump-host.example.com
User user
RemoteCommand ssh user@$(/full/path/to/command_to_get_the_hostname)
これで、を使用してリモートホストに接続できますssh -t remote
。-t
擬似端末割り当てを強制する$(command)
リモート側、つまりジャンプ ホストで実行します。
副作用として、リモート ホストにローカルを使用できなくなりますが、2 番目がジャンプ ホスト上で実行されているIdentityFile C:\Users\user\.ssh\id_rsa
ため、ジャンプ ホストにはキーが必要です。ssh
ProxyJump
スクリプト
ジャンプ ホストをProxyJump
( ) ホストとして使用し、ローカルの( )、( )、( ) など-J
を使用できるようにするには、SSH 構成ファイルを使用するだけでなく、スクリプトを作成する必要があります。まず、ジャンプ ホストに接続し、そこからリモート ホストのアドレスを取得して、それを別の SSH 接続で使用する必要があります。IdentityFile
-i
ForwardX11
-X
ForwardX11Trusted
-Y
とバッシュこれは次のようになります:
#!/bin/bash
remotehost=$(\
ssh [email protected] \
/full/path/to/command_to_get_the_hostname) || exit 1
ssh -X -Y -J [email protected] \
-i ~/.ssh/id_rsa user@"$remotehost"
私考えるこれはパワーシェルWindows 上のスクリプト (テストしていません):
$remoteHost = ssh.exe [email protected] `
/full/path/to/command_to_get_the_hostname
if (-not $?) {throw "Failed to get the remote hostname"}
ssh.exe -X -Y -J [email protected] `
-i "C:\Users\user\.ssh\id_rsa" "user@$remoteHost"