以下是我的脚本,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