ssh は、(可変数の)中間ホップを介していくつかのコマンドを実行します。

ssh は、(可変数の)中間ホップを介していくつかのコマンドを実行します。

複数のホップを介して(すべての)コマンドをリモートで実行する方法を作成するには、アイデアが必要です。最大の問題は、中間ホップの数が可変であるということです!したがって、1)サーバーの「トポロジー」を説明し、2)機能が必要ですか? / Scriptは、最終的な宛先に到達するのに必要なだけ中間ホップを使用して、すべてのサーバーでいくつかのコマンドを実行できます。

トポロジーの例:(しかしこれも違います)

server A communicates with server B directly.
servers C and D are "behind" B.
server E is behing C
etc.... (many servers, many indirect ways to reach some of them, some have multiple ways)

Then from A, you can execute "some command" on E with:
    ssh foo@B ssh bar@C ssh baz@E "some command"
 or you can build intermediary tunnels connecting A to **C** and then:
    ssh -P 1234 baz@E "some command"

多くのサーバーに直接移動する「ホームホップ」があります。

私は(スクリプトで)次のタスクを実行できるあまり複雑でないコマンドを使用して、5つのサーバーすべてで(潜在的に複雑な)コマンドを実行できるようにしたいと思います。

do_on_all_servers "for dir in /something* ; do cd \"\$dir\" && something using $thisLOCALvariable ; done"
# with an easy way to say "from X, ssh to specificuser@Y using this ssh command and this username" to describe each intermediary hops
# dir is a remote variable, thisLOCALvariable is at the initiating script level, and the amount of "\" needs to be adjusted if going through multiple hops...

追加の制約:GNUツールもなく、「nc」もなく、単に(非常に)古いsshやawkなどの古いツールを移植可能な方法で使用するだけです。移植性が高くなければなりません(きれいな「1990?」オプションとツールは避けてください)。最終的なターゲットからスクリプトを複製し、そのスクリプトの複数のホップを介してSSHを介してこれを実行したくありませんが、これがより簡単でより良いと思われる場合はそれも機能します。

今、私は変数を使ってsshをリンクしていますが、... "どのくらい必要か"質問には役に立ちません。

私のアイデアには、「shh user@host」を含む変数を一緒に囲むことが含まれます。あるいは、これを使用すると非常に簡潔になる可能性があります。https://stackoverflow.com/a/15198031/1841533トンネリング方式を正しく実行できる場合は、正しいローカルポートを使用してscpなどの最終宛先に「直接」(ただし、これがすべてのホップで機能しているかどうかわからない)に到達できます。

ベストアンサー1

シェル関数ラッパーを作成/使用して、ネストを使用してエンドホストに到達するsshカスタム構成ファイルを生成できます。ProxyCommandsこれにより、関数が呼び出され、生成された一時ssh構成-Fファイルを指します。

このような:

sssh() {
  TMPF=$(mktemp);
  echo -n "$1" |
  awk '
    BEGIN {RS=" "}
    { split($0,a,":"); u=a[1]; p=a[3]; h=a[2]
      print "\nHost "h"\n\tUser "u"\n\tPort "p;
      if (hop) print "\tProxyCommand ssh -q -W %h:%p "hop;
      hop=h }
    END {print "\nHost *\n\tControlMaster auto\n\tControlPath /tmp/%r@%h:%p"}
  ' > $TMPF;   TMPA=($1); TMPAA=(${TMPA[-1]//:/ }); TMPH=${TMPAA[1]}; shift;
  ssh -F $TMPF $TMPH $@;
  rm $TMPF; }

次のように実行すると:

sssh "user1:so.me.ho.st:1234 user2:router.internal:2345 user3:device.internal:3456" do_complex_command

次のtmpファイルが生成されます。

Host so.me.ho.st
    User user1
    Port 1234

Host router.internal
    User user2
    Port 2345
    ProxyCommand ssh -q -W %h:%p so.me.ho.st

Host device.internal
    User user3
    Port 3456
    ProxyCommand ssh -q -W %h:%p router.internal

Host *
    ControlMaster auto
    ControlPath /tmp/%r@%h:%p

最後に実行:

ssh -F /tmp/tmp.IUVSRrer45 device.internal do_complex_command

これはdo_complex_command最後の「最も内側の」ホストで実行されます。すべてのブローカーでコマンドを実行する必要がある場合は、機能を少し調整する必要があります。

おすすめ記事