对于不相关的大型项目,我需要从本地系统或远程系统收集系统统计信息。由于我以任何一种方式收集相同的统计数据,因此我通过将统计信息收集命令存储在Bash关联数组中来防止代码重复。
declare -A stats_cmds
# Actually contains many more key:value pairs, similar style
stats_cmds=([total_ram]="$(free -m | awk '/^Mem:/{print $2}')")
我可以像这样收集本地系统统计信息:
get_local_system_stats()
{
# Collect stats about local system
complex_data_structure_that_doesnt_matter=${stats_cmds[total_ram]}
# Many more similar calls here
}
我的脚本的前提条件是~/.ssh/config
设置为ssh $SSH_HOSTNAME
无需任何用户输入即可运行。我想要这样的东西:
get_remote_system_stats()
{
# Collect stats about remote system
complex_data_structure_that_doesnt_matter=`ssh $SSH_HOSTNAME ${stats_cmds[total_ram]}`
}
我已尝试过单引号,双引号,反引号以及我能想象的各种组合。某些组合会导致stats命令执行得太早(bash: 7986: command not found
),其他组合会导致语法错误,其他组合返回null(stats命令周围的单引号),但没有将正确的结果存储在我的数据结构中。
如何通过SSH评估存储在关联数组中的命令,并将结果存储在本地脚本的数据结构中?
答案 0 :(得分:2)
确保在分配阵列时,阵列中存储的命令不会扩展!
另请注意,嵌套单引号时需要复杂的引用样式。有关解释,请参阅this SO帖子。
stats_cmds=([total_ram]='free -m | awk '"'"'/^Mem:/{print $2}'"'"'')
然后启动您的ssh
:
sh "$ssh_hostname" "${stats_cmds[total_ram]}"
(是的,我降低了你的变量名,因为Bash中的大写变量名真的很恶心)。然后:
get_local_system_stats() {
# Collect stats about local system
complex_data_structure_that_doesnt_matter=$( ${stats_cmds[total_ram]} )
# Many more similar calls here
}
和
get_remote_system_stats() {
# Collect stats about remote system
complex_data_structure_that_doesnt_matter=$(ssh "$ssh_hostname" "${stats_cmds[total_ram]}")
}
答案 1 :(得分:1)
首先,我将建议一种对现有实施进行微小更改的方法。然后,我将展示更接近最佳实践的东西。
鉴于您现有的代码:
declare -A remote_stats_cmds
remote_stats_cmds=([total_ram]='free -m | awk '"'"'/^Mem:/{print $2}'"'"''
[used_ram]='free -m | awk '"'"'/^Mem:/{print $3}'"'"''
[free_ram]='free -m | awk '"'"'/^Mem:/{print $4}'"'"''
[cpus]='nproc'
[one_min_load]='uptime | awk -F'"'"'[a-z]:'"'"' '"'"'{print $2}'"'"' | awk -F "," '"'"'{print $1}'"'"' | tr -d " "'
[five_min_load]='uptime | awk -F'"'"'[a-z]:'"'"' '"'"'{print $2}'"'"' | awk -F "," '"'"'{print $2}'"'"' | tr -d " "'
[fifteen_min_load]='uptime | awk -F'"'"'[a-z]:'"'"' '"'"'{print $2}'"'"' | awk -F "," '"'"'{print $3}'"'"' | tr -d " "'
[iowait]='cat /proc/stat | awk '"'"'NR==1 {print $6}'"'"''
[steal_time]='cat /proc/stat | awk '"'"'NR==1 {print $9}'"'"'')
......人们可以在本地评估如下:
result=$(eval "${remote_stat_cmds[iowait]}")
echo "$result" # demonstrate value retrieved
...或远程如下:
result=$(ssh "$hostname" bash <<<"${remote_stat_cmds[iowait]}")
echo "$result" # demonstrate value retrieved
不需要单独的表格。
现在,让我们谈谈完全不同的方式:
# no awful nested quoting by hand!
collect_total_ram() { free -m | awk '/^Mem:/ {print $2}'; }
collect_used_ram() { free -m | awk '/^Mem:/ {print $3}'; }
collect_cpus() { nproc; }
......然后,在本地进行评估:
result=$(collect_cpus)
......或者,远程评估:
result=$(ssh "$hostname" bash <<<"$(declare -f collect_cpus); collect_cpus")
...或者,使用collect_
前缀迭代定义的函数并执行以下两项操作:
declare -A local_results
declare -A remote_results
while IFS= read -r funcname; do
local_results["${funcname#collect_}"]=$("$funcname")
remote_results["${funcname#collect_}"]=$(ssh "$hostname" bash <<<"$(declare -f "$funcname"); $funcname")
done < <(compgen -A function collect_)
...或者,在一次通过中将所有项目收集到一个远程阵列中,避免额外的SSH往返和而不是eval
或以其他方式进行从远程系统收到的结果带来的安全风险:
remote_cmd=""
while IFS= read -r funcname; do
remote_cmd+="$(declare -f "$funcname"); printf '%s\0' \"$funcname\" \"\$(\"$funcname\")\";"
done < <(compgen -A function collect_)
declare -A remote_results=( )
while IFS= read -r -d '' funcname && IFS= read -r -d '' result; do
remote_results["${funcname#collect_}"]=$result
done < <(ssh "$hostname" bash <<<"$remote_cmd")