openssh 実装のあまり知られていない、ほとんど使用されていない機能を使用して、目的を正確に達成できます。
- 現状を活かしている
- 現在の作業ディレクトリを使用できます
- セッション開始前のトンネリング設定は不要
- 別の端末や接続を開く必要はありません
- インタラクティブ セッションで 1 回限りの取引として使用することも、自動化されたセッションの一部として使用することもできます
local>
以下の例の、remote>
、および
ssh>
プロンプトのそれぞれにあるものだけを入力してください。
local> ssh username@remote
remote> ~C
ssh> -L6666:localhost:6666
remote> nc -l 6666 < /etc/passwd
remote> ~^Z
[suspend ssh]
[1]+ Stopped ssh username@remote
local> (sleep 1; nc localhost 6666 > /tmp/file) & fg
[2] 17357
ssh username@remote
remote> exit
[2]- Done ( sleep 1; nc localhost 6666 > /tmp/file )
local> cat /tmp/file
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
...
~/.ssh/id_rsa.pub
または、ローカル マシンから~/.ssh/authorized_keys
リモート マシンのファイルにファイルを転送する場合など、他の方向に進みたい場合がよくあります。
local> ssh username@remote
remote> ~C
ssh> -R5555:localhost:5555
remote> ~^Z
[suspend ssh]
[1]+ Stopped ssh username@remote
local> nc -l 5555 < ~/.ssh/id_rsa.pub &
[2] 26607
local> fg
ssh username@remote
remote> nc localhost 5555 >> ~/.ssh/authorized_keys
remote> cat ~/.ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2ZQQQQBIwAAAQEAsgaVp8mnWVvpGKhfgwHTuOObyfYSe8iFvksH6BGWfMgy8poM2+5sTL6FHI7k0MXmfd7p4rzOL2R4q9yjG+Hl2PShjkjAVb32Ss5ZZ3BxHpk30+0HackAHVqPEJERvZvqC3W2s4aKU7ae4WaG1OqZHI1dGiJPJ1IgFF5bWbQl8CP9kZNAHg0NJZUCnJ73udZRYEWm5MEdTIz0+Q5tClzxvXtV4lZBo36Jo4vijKVEJ06MZu+e2WnCOqsfdayY7laiT0t/UsulLNJ1wT+Euejl+3Vft7N1/nWptJn3c4y83c4oHIrsLDTIiVvPjAj5JTkyH1EA2pIOxsKOjmg2Maz7Pw== username@local
少し説明が整いました。
最初のステップはLocalForward
;を開くことです。まだ確立していない場合は、~C
エスケープ文字を使用して ssh コマンド ラインを開き、次のコマンドを実行できます。
remote> ~C
ssh> help
Commands:
-L[bind_address:]port:host:hostport Request local forward
-R[bind_address:]port:host:hostport Request remote forward
-D[bind_address:]port Request dynamic forward
-KR[bind_address:]port Cancel remote forward
この例LocalForward
では、クライアントとサーバーの両方に対して localhost のポート 6666 を確立します。ポート番号は、開いている任意のポートにすることができます。
nc
コマンドはnetcat
パッケージからのものです。「TCP/IP スイス アーミー ナイフ」と呼ばれています。シンプルですが、非常に柔軟で便利なプログラムです。Unix ツールベルトの標準的な部分にしてください。
この時点nc
で、ポート 6666 でリッスンし、別のプログラムがそのポートに接続して の内容を送信できるようになるのを待っています
/etc/passwd
。
~^Z
次に、 がtilde
続く別のエスケープ文字を使用しcontrol-Z
ます。これにより、ssh プロセスが一時的に中断され、シェルに戻ります。
nc
転送されたポート 6666 に接続するために使用できるローカル システムに 1 つ戻ります-l
。この場合、a がないことに注意してくださいnc
。代わりに、リモート側でnc
既にリッスンしている に接続するためのクライアントとして使用したいだけです。nc
コマンドに関する残りの魔法nc
が必要です。上記でssh
プロセスが一時的に中断されたことを思い出してください。そのため、式&
全体(sleep + nc)
がバックグラウンドに置かれ、sleep
ssh が でフォアグラウンドに戻るのに十分な時間が与えられますfg
。
-R
2 番目の例では、代わりに を使用して反対方向に向かうトンネルを設定することを除いて、考え方は基本的に同じ-L
ですRemoteForward
。そして、ローカル側は-l
引数を使用したい場所ですnc
。
デフォルトのエスケープ文字は ~ ですが、次のように変更できます。
-e escape_char
Sets the escape character for sessions with a pty (default: ‘~’). The escape character is only recognized at the beginning of a line. The escape character followed by a dot
(‘.’) closes the connection; followed by control-Z suspends the connection; and followed by itself sends the escape character once. Setting the character to “none” disables any
escapes and makes the session fully transparent.
エスケープ文字で使用できるコマンドの完全な説明は、ssh のマンページにあります。
ESCAPE CHARACTERS
When a pseudo-terminal has been requested, ssh supports a number of functions through the use of an escape character.
A single tilde character can be sent as ~~ or by following the tilde by a character other than those described below. The escape character must always follow a newline to be interpreted
as special. The escape character can be changed in configuration files using the EscapeChar configuration directive or on the command line by the -e option.
The supported escapes (assuming the default ‘~’) are:
~. Disconnect.
~^Z Background ssh.
~# List forwarded connections.
~& Background ssh at logout when waiting for forwarded connection / X11 sessions to terminate.
~? Display a list of escape characters.
~B Send a BREAK to the remote system (only useful for SSH protocol version 2 and if the peer supports it).
~C Open command line. Currently this allows the addition of port forwardings using the -L, -R and -D options (see above). It also allows the cancellation of existing remote port-
forwardings using -KR[bind_address:]port. !command allows the user to execute a local command if the PermitLocalCommand option is enabled in ssh_config(5). Basic help is avail‐
able, using the -h option.
~R Request rekeying of the connection (only useful for SSH protocol version 2 and if the peer supports it).