我有一个下面的脚本,该脚本正在运行,但似乎太慢了,我还不熟悉Shell,因为它仍在学习过程中,因此没有太多的想法使它变得更聪明.
I have a below script which is working but seems too slow, I'm not expert on the shell as still in learning process hence not getting much idea to make it more smarter.
对此表示感谢.
我的代码:
#!/bin/bash CurrntTime=$(date +'%m/%d/%Y %T') read -rsp $'Please Enter password below: ' SSHPASS export SSHPASS echo -e "\n" printf "%s %-38s %s\n" "Server_Name CPU Memory Swap" printf '%s\n' "-------------------------------------------------------" for server in $(cat /home/user1/mem) do cpu_info=$(sshpass -e ssh -q -t $server sudo grep processor /proc/cpuinfo | awk 'NF==3{count++} END {printf count}') mem_info=$(sshpass -e ssh -q -t $server sudo free -g | awk '/Mem:/{printf $2}') swap_info=$(sshpass -e ssh -q -t $server sudo free -g | awk '/Swap:/{printf $2}') printf "%s %-38s %s\n" "$server $cpu_info $mem_info $swap_info" done | tee -a 2>/dev/null脚本输出:
$ ./cpu-memory-count.sh Please Enter password below: Server_Name CPU Memory Swap ------------------------------------------------------- udc0150.exmapl.udc 8 15 2 udc0196.exmapl.udc 8 15 2 udc0193.exmapl.udc 8 15 2 udc0160.exmapl.udc 8 15 2 udc0146.exmapl.udc 1 15 2注意:我不会将输出保存到任何文件中,而只是将其打印在控制台上.
Note: I am not saving output to any of the file rather just getting them printed on the console.
由于安全原因,我们无法使用 ansible 和 key-baed 访问,因此我将 sshpass 与ssh一起使用成为 sudo ,需要root特权才能获取信息.
Due to some security reasons we are not able to use ansible and key-baed access hence i used sshpass along with ssh need to become sudo where it required root privileges to get the information.
推荐答案
也就是说,我可以看到一个函数,将其导出,然后从 xargs 内部运行 bash .GNU xargs 具有 -P 选项可以并行运行内容:
That said, I could see a function, export it, then run bash from inside xargs. GNU xargs has -P option to run stuff in parallel:
work() { server=$1 # ONE connection tmp=$(ssh "$server" bash -c 'nproc; free -g') # parsing later cpu_info=$(<<<"$tmp" awk 'NR==1') mem_info=$(<<<"$tmp" awk '/Mem:/{printf $2}') swap_info=$(<<<"$tmp" awk '/Swap:/{printf $2}') # outputting printf "%-40s %5s %5s %5s\n" "$server" "$cpu_info" "$mem_info" "$swap_info" } export -f work < /home/user1/mem xargs -P0 -n1 -d'\n' bash -c 'work "$@"' _能否请您解释一下工作" $ @""_,因为我还在学习.如果可以的话,那将是真正的帮助
Could you please explain 'work "$@"' _ , as i'm still learning. if you could that will be really helpfu
</home/user1/mem 将文件/home/user1/mem 的内容重定向到命令的标准输入.该命令是 xargs . xargs 从标准输入中读取用换行符分隔的 -d'\ n'数据,并将 -n1 传递给一个参数到随后的命令.因此它执行 bash -c'work'$ @'''_< the_line> 每行. -P0 使其同时执行.
< /home/user1/mem redirects the content of the file /home/user1/mem to standard input of the command. The command is xargs. xargs reads -d'\n' data separates with newlines (lines...) from the standard input, and passes -n1 one argument to the command that followed. So it executes bash -c 'work "$@"' _ <the_line> on each line. -P0 makes it do that concurrently.
从 man bash 中我们可以阅读:
-c如果存在-c选项,则从第一个非选项参数command_string读取命令.如果后面有参数command_string,第一个参数分配给$ 0 ,其余所有参数分配给位置参数.分配-ment设置为$ 0会设置外壳的名称,该名称用于警告和错误消息.
-c If the -c option is present, then commands are read from the first non-option argument command_string. If there are arguments after the command_string, the first argument is assigned to $0 and any remaining arguments are assigned to the positional parameters. The assign‐ ment to $0 sets the name of the shell, which is used in warning and error messages.
和:
@从1开始扩展到位置参数.在执行单词拆分的情况下,这会扩展每个位置参数比喻成一个单独的单词;如果不在双引号中,则这些单词会进行分词.在分词的环境中如果不执行,则扩展为单个单词,每个位置参数之间用空格分隔.当膨胀发生在两倍之内用引号引起来,每个参数扩展为一个单独的单词.即"$ @"等价于"$ 1""$ 2"... 如果出现双引号扩展在一个单词内,第一个参数的扩展与原始单词的开头部分连接,最后一个参数的扩展与参数与原始单词的最后部分结合在一起.当不存在位置参数时,"$ @"表示位置参数.和$ @扩展为空(即,它们被删除了.)
@ Expands to the positional parameters, starting from one. In contexts where word splitting is performed, this expands each positional pa‐ rameter to a separate word; if not within double quotes, these words are subject to word splitting. In contexts where word splitting is not performed, this expands to a single word with each positional parameter separated by a space. When the expansion occurs within double quotes, each parameter expands to a separate word. That is, "$@" is equivalent to "$1" "$2" ... If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word. When there are no positional parameters, "$@" and $@ expand to nothing (i.e., they are removed).
在 bash -c'work'$ @'中_< the_line> :
- 命令 bash 用4个参数执行- -c work"$ @" _ 和行内容
- bash看到 -c
- 下一个参数 work"$ @" 是要执行的脚本
- "$ @" 将扩展为"$ 1""$ 2".. . 实际上,这里每次都只有1个自变量,因此无论如何"$ @" 都将等于"$ 1" .
- command bash is executed with 4 arguments - -c work "$@" _ and the line content
- bash sees -c
- the next argument work "$@" is the script to execute
- "$@" will expand to "$1" "$2" ...
- Actually, here there will be only 1 argument each time, so "$@" is going to be just equal to "$1" anyway.
更多推荐
获取内存,CPU和交换次数的脚本
发布评论