複数のホストにファイルを送信する

複数のホストにファイルを送信する

以下は私のスクリプトで、Forループが機能します。完璧。うまくいかないのは、ホスト名を取得し、すべてのホスト名を介して「SendFiles」ループを実行することです。この問題を解決する方法を学びます。 Phy_Hosts変数からホスト名を取得します。

vmfarm1これでホストで7回だけ繰り返されます。#!/usr/bin/env bash環境も使用する必要がありますか?

#!/bin/bash
#Location where bash scripts are located.

phy_ssh=/opt/wiki_scripts/servers.sh 
vm_ssh=/opt/wiki_scripts/virtualservers.sh

#Hosts where we will ssh into.
Phy_Hosts=(vmfarm1 p12 barclay maximus backupfirefly accountant 10.6.6.90)

SendFiles () {
        host=$1
        ssh root@${Phy_Hosts} 'bash -s' < ${phy_ssh}
        ssh root@${Phy_Hosts} cat /root/phy_machines.txt >>  /opt/wiki_scripts/phy_machines.txt
}

hostCount=${#Phy_Hosts[@]}

# backup databases
for ((i=0; i<${hostCount}; i++))
do
        host=${Phy_Hosts[$i]}
        SendFiles ${host}
done

exit 0

編集する:

Currently:

#!/bin/bash
# Location where bash scripts are located.
phy_ssh=/opt/wiki_scripts/servers.sh 
vm_ssh=/opt/wiki_scripts/virtualservers.sh

# Hosts where we will ssh into.
Phy_Hosts=( vmfarm1 p12 barclay maximus backup firefly accountant 10.6.6.90 )

vmfarm1=( icinga ldap mail openvpn dns redmine owncloud www git)

maximus=( elasticsearch jenkins egcut demo )
firefly=( client )
texta=( live 10.6.6.92 10.6.6.93 )

SendFiles () {
        local host="$1"
        ssh "root@$host" 'bash -s' <"$phy_ssh"
        ssh "root@$host" cat /root/phy_machines.txt
}

SendFiles1 () {
        local host1="$1"
        ssh "root@$host1" 'bash -s' <"$vm_ssh"
        ssh "root@$host1" cat /root/vm_machines.txt
}


# Save the following data to the phy_machine file.
for host in "${Phy_Hosts[@]}"; do
        SendFiles "$host"
done >>/opt/wiki_scripts/phy_machines.txt

# Save the following data to the vm_machine file.
for host1 in "${vmfarm1[@]}"; do
        SendFiles1 "$host1"
done >>/opt/wiki_scripts/vm_machines.txt

# Save the following data to the vm_machine file.
for host1 in "${maximus[@]}"; do
        SendFiles1 "$host1"
done >>/opt/wiki_scripts/vm_machines.txt

# Save the following data to the vm_machine file.
for host1 in "${firefly[@]}"; do
        SendFiles1 "$host1"
done >>/opt/wiki_scripts/vm_machines.txt

# Save the following data to the vm_machine file.
for host1 in "${texta[@]}"; do
        SendFiles1 "$host1"
done >>/opt/wiki_scripts/vm_machines.txt

ベストアンサー1

提案:

#!/bin/bash

# Location where bash scripts are located.
phy_ssh=/opt/wiki_scripts/servers.sh 

# Hosts where we will ssh into.
Phy_Hosts=( vmfarm1 p12 barclay maximus backupfirefly accountant 10.6.6.90 )

SendFiles () {
        local host="$1"
        ssh "root@$host" 'bash -s' <"$phy_ssh"
        ssh "root@$host" cat /root/phy_machines.txt
}

# backup databases
for host in "${Phy_Hosts[@]}"; do
        SendFiles "$host"
done >/opt/wiki_scripts/phy_machines.txt
  • インデックスを使わずに配列を直接繰り返すことができます。
  • host関数で変数を使用したことはありません。
  • 未使用の変数の削除vm_ssh、二重引用符の変数の拡張、exit 0最後の不要な変数の削除など、一般的なクリーンアップです。
  • 出力リダイレクトが関数内からループに移動されましたfor。これは必要ではなく、ssh関数の最初の呼び出しで何かを出力したいのですが、関数をより明確にする場合は、実際には間違っている可能性があります。

数回のコメントを繰り返した後:

関数を一度だけ定義しますSendFiles(後で定義すると、以前の定義が上書きされます)。それを奪ってください。みんな特定のホストセットに対して実行するには情報が必要です。

#!/bin/bash

# Location where bash scripts are located.
phy_ssh=/opt/wiki_scripts/servers.sh
vm_ssh=/opt/wiki_scripts/virtualservers.sh
others_ssh=/opt/wiki_scripts/others.sh

# Hosts where we will ssh into.
Phy_Hosts=( vmfarm1 p12 barclay maximus backupfirefly accountant 10.6.6.90 )
Vm_Hosts=( icinga.stacc.ee ldap.stacc.ee mail.stacc.ee openvpn.stacc.ee dns.stacc.ee redmine.stacc.ee owncloud.stacc.ee www.stacc.ee git.stacc.ee )
SomeOther_list ( more machines )

SendFiles () {
        local host="$1"
        local script="$2"
        local remotefile="$3"

        ssh "root@$host" 'bash -s' <"$script"
        ssh "root@$host" cat "$remotefile"
}

# backup databases
for host in "${Phy_Hosts[@]}"; do
        SendFiles "$host" "$phy_ssh" /root/phy_machines.txt
done >/opt/wiki_scripts/phy_machines.txt

for host in "${Vm_Hosts[@]}"; do
        SendFiles "$host" "$vm_ssh" /root/vm_machines.txt
done >/opt/wiki_scripts/vm_machines.txt

# and then, for example
for host in "${SomeOther_list[@]}"; do
        SendFiles "$host" "$others_ssh" /root/other_machines.txt
done >/opt/wiki_scripts/other_machines.txt

おすすめ記事