简而言之,我希望将单行输出字符串拆分为多个行,并将每个原始行标记为主机名。
主机名ProcessName CPUCore
output=$(ssh -q -o "StrictHostKeyChecking yes“$ssh_host 'ps -eo comm,lastcpu x grep rrcp')
如何通过返回输出循环插入循环,如下所示。
rrcpd 17 rrcpd 0重写为
Hostname1 rrcpd 17
Hostname1 rrcpd 0备注
在每个主机上运行的命令。
output=$(ssh -q -o "StrictHostKeyChecking yes“$ssh_host 'ps -eo comm,lastcpu x grep rrcp')
当我移除grep时,输出可以返回3-5进程,因此我希望能够拆分每个输出,并将主机名添加到raw中来标记每个输出。我正在检查每个主机上正在运行哪些进程。
rrcpd 0 rrcp_mon.sh 24
rrcpd 0
rrcpd 0
rrcpd 0 rrcpd 1
rrcpd 0 rrcpd 0 rrcp_mon 24 rrcp_mon 24
rrcpd 0
rrcpd 0
rrcpd 17 rrcpd 0
ard 9 ssh 32 httpd 21 bax 22
我已经修改了下面提供的内容,并设法使它开始工作。
#!/bin/bash
for remote in $(cat ssh_hosts2.txt)
do
ssh -q -o StrictHostKeyChecking=yes "$remote" ps -eo comm,lastcpu |
sed "s/^/$remote /"
done发布于 2018-08-13 11:04:42
请试试这个,
我已经将给定的示例输出保存在/tmp/test.txt文件中。
xargs -n 2 | sed 's/^/Hostname1 /' -n意味着每行的最大参数。例如;
cat /tmp/test.txt | xargs -n 2 | sed 's/^/Hostname1 /g'
Hostname1 rrcpd 17
Hostname1 rrcpd 0
Hostname1 Cheese 3
Hostname1 cakes 8
Hostname1 Cats 9
Hostname1 dogs 3发布于 2018-08-13 11:48:24
您会看到所显示的数据类型( ssh命令的输出中每行超过两个字段)的唯一原因是将ssh+ps调用的输出收集到一个变量中,然后在没有正确引用的情况下输出它,比如在output=$(ssh ...)后面跟着echo $output而不是echo "$output"。幸运的是,您根本不需要收集变量中的数据。
while read remote; do
ssh -n -q -o StrictHostKeyChecking=yes "$remote" ps -eo comm,lastcpu |
awk -v host="$remote" '/rrcp/ { print host, $1, $2 }'
done 在这里,awk直接从ssh命令读取,并且在代码中执行grep最初完成的工作。它假定它将获得带有两个字段的行,并在每一对字段前面加上给定的主机名(从hosts.txt读取)。
或者,
while read remote; do
ssh -n -q -o StrictHostKeyChecking=yes "$remote" ps -eo comm,lastcpu |
sed "s/^/$remote /"
done 在这里,我们只需在每行前面加上从文本文件中读取的主机名。
https://unix.stackexchange.com/questions/462274
复制相似问题