各リモートシステムでコマンドを使用して複数のSSH接続を作成する

各リモートシステムでコマンドを使用して複数のSSH接続を作成する

私は3つの異なるリモートコンピュータに接続し、それらの間にファイルをコピーしてから、最後のリモートコンピュータに滞在しながらいくつかのタスクを実行しようとしています。このプロセスを自動化するのに役立つbashスクリプトを作成しようとしています。これが私がしたいことです。

fname='file to be copied across these remote machines' 
rmt1='remote machine1'
mt2='remote machine2'
rmt3='remote machine3'

#copy file from local to rmt1
scp -r  $fname  $rmt1
#log into rmt1
ssh -Y $rmt1
#from rmt1, copy file from rmt1 to rmt2
scp -r  $fname  $rmt2
#from rmt1 log into rmt2
ssh -Y $rmt2
#from rmt2 copy file from rmt2 to rmt3
scp -r  $fname  $rmt3
#from rmt2 log into rmt3
ssh -Y $rmt3
Then finally stay in rmt3 where I can do other things.

毎回あまりにも多くの内容を入力する必要がないように、このプロセスを自動化するのに役立つbashスクリプトが必要です。どんな助けでも大変感謝します。ノート 以下を使用して公開/秘密鍵を有効にしました。SSHキージェネレータしたがって、どちらもパスワードを入力する必要はありません。

助けてくれてありがとう

ベストアンサー1

ローカルホスト上の1つの秘密鍵がOpenSSHの最新バージョンだけでなく、3つのリモートホストすべてへのログインを許可すると仮定すると、次のようになります。

#!/bin/sh
fname='file to be copied across these remote machines' 
rmt1='remote machine1'
rmt2='remote machine2'
rmt3='remote machine3'

scp -J "$rmt1,$rmt2" "$fname" "$rmt3":/target/path/
ssh -Y -J "$rmt1,$rmt2" "$rmt3"

おすすめ記事