リモートで実行されるスクリプトから複数の変数を入力としてスクリプトに渡す方法

リモートで実行されるスクリプトから複数の変数を入力としてスクリプトに渡す方法

私はあるサーバーから別のサーバーセットにsshを実行し、IPセットに対してnetcatコマンドを実行するGnu / LinuxのBashシェルスクリプトを作成しました。

以下は、SSHを介して他のサーバーに接続するスクリプトです。

*read -p "Enter the instant stack host[s]: " hosts
read -p "Enter the list of destinations [IP]: " dest_list
read -p "Enter the port # associated with the destination: " port
for h in $hosts
do
                scp ./connectivityTest.sh @$h:/var/tmp/
               ssh user@$h . /var/tmp/connectivityTest.sh "$dest_list" "$port"
done*

netcatコマンドを実行するスクリプトは次のとおりです。

*echo "Working on `hostname` to check connectivity for following end points"
echo $1
echo $2
dest_list=`echo "$1"`
port=`echo "$2"`
echo $dest_list
        for d in $dest_list
        do
                echo "Endpoint checked:  $d"
                        nc -v -i 1 -w 5 $d $port
        done
exit*

実行する単一のIP [エンドポイント]のみを提供すると、スクリプトは正しく実行されます。

*Enter the instant stack host[s]: host1.com
 Enter the list of destinations [IP]: **test.ip1.com**
Enter the port # associated with the destination: 443
connectivityTest.sh                                                                                                                                        100%  520     1.8MB/s   00:00
##############################################################
Working on host1 to check connectivity for following end points
##############################################################
test.ip1.com 
443
test.ip1.com 
Endpoint checked:  test.ip1.com
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to ip1:443.
Ncat: Idle timeout expired (1000 ms).*

ただし、複数のエンドポイントを提供すると、スクリプトは2番目のIP /エンドポイントをポート番号として使用します。

*Enter the instant stack host[s]: host1.com 
Enter the list of destinations [IP]: **test.ip1.com test.ip2.com**
Enter the port # associated with the destination: 443
connectivityTest.sh                                                                                                                                        100%  520     1.8MB/s   00:00
##############################################################
Working on host1 to check connectivity for following end points
##############################################################
test.ip1.com
**test.ip2.com**
test.ip1.com
Endpoint checked:  test.ip1.com
Ncat: Version 7.50 ( https://nmap.org/ncat )
**Ncat: Invalid port number "test.ip2.com". QUITTING.***

また、これはスクリプトがSSHを介して実行された場合にのみ発生します。サーバーから直接スクリプトを実行すると、複数のIPで正常に動作します。このレベルでは、何か間違っていることは明らかです。

*ssh user@$h . /var/tmp/connectivityTest.sh "$dest_list" "$port"*

それからこの解決策を見つけました。一重引用符の下に入力変数を追加しました。

*ssh user@$h . /var/tmp/ "'$dest_list'" "'$port'"*

これで複数のIPで動作します。

*Enter the instant stack host[s]: host1.com
Enter the list of destinations [IP]: **test.ip1.com test.ip2.com test.ip3.com test.ip4.com**
Enter the port # associated with the destination: 443
connectivityTest.sh                                                                                                                                        100%  525     1.5MB/s   00:00
##############################################################
Working on host1 to check connectivity for following end points
##############################################################
test.ip1.com test.ip2.com 
443
test.ip1.com test.ip2.com 
Endpoint checked:  test.ip1.com
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to ip1:443.
Ncat: Idle timeout expired (1000 ms).
Endpoint checked:  test.ip2.com
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to ip2:443.
Ncat: Idle timeout expired (1000 ms).*

解決しましたが、どのように解決するのかわかりません。アポストロフィ走るものの違いローカルスクリプトとSSHによるスクリプトの実行。そしてどのようにアポストロフィ走るものの違い複数入力スクリプトと単一入力スクリプトの比較

どんな洞察力でも役に立ちます。よろしくお願いします! ! !

ベストアンサー1

あなたならどうなりますか?

ssh user@$h . /var/tmp/connectivityTest.sh '$dest_list' '$port'

回避策と同じように機能する場合、問題は変数が拡張されるシステム(ローカルまたはリモート)にあることです。

おすすめ記事