여러 호스트에 파일 보내기

여러 호스트에 파일 보내기

아래는 내 스크립트이며 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

관련 정보