【问题标题】:Check Status of Services检查服务状态
【发布时间】:2021-11-11 12:06:24
【问题描述】:

我正在尝试创建一个脚本来从不同的远程服务器获取服务状态:

#!/bin/bash

USERNAME=vagrant

isengine=(server1 server2 server3 server4 server5)
isengine_services=( ds.rc DFDServer ISFAgents ODFEngine)


for h in "${isengine[@]}"
do
  echo "${h}" >> test.txt
  for i in "${isengine_services[@]}"
  do
     sshpass -p "mysecret" ssh  -o StrictHostKeyChecking=no  "${USERNAME}"@"${h}" "echo ${i}; systemctl status ${i} | grep Active" >> tmp.txt

  done
done

cat tmp.txt

我得到如下所示的非常混乱的输出:

server1
ds.rc
   Active: active (running) since Mon 2021-09-13 10:04:48 CEST; 3 days ago
DFDServer
   Active: active (running) since Mon 2021-09-13 10:05:02 CEST; 3 days ago
ISFAgents
   Active: active (running) since Mon 2021-09-13 10:04:46 CEST; 3 days ago
ODFEngine
   Active: active (running) since Mon 2021-09-13 10:04:20 CEST; 3 days ago

是否可以让它看起来像下面这样:

server1

ds.rc       Active: active (running) since Mon 2021-09-13 10:04:48 CEST; 3 days ago
DFDServer   Active: active (running) since Mon 2021-09-13 10:05:02 CEST; 3 days ago
ISFAgents   Active: active (running) since Mon 2021-09-13 10:04:46 CEST; 3 days ago
ODFEngine   Active: active (running) since Mon 2021-09-13 10:04:20 CEST; 3 days ago

【问题讨论】:

    标签: shell awk


    【解决方案1】:

    您可以使用 GNU AWK 将两行代码转换为一行代码,如下所示,让 file.txt 内容为

    server1
    ds.rc
       Active: active (running) since Mon 2021-09-13 10:04:48 CEST; 3 days ago
    DFDServer
       Active: active (running) since Mon 2021-09-13 10:05:02 CEST; 3 days ago
    ISFAgents
       Active: active (running) since Mon 2021-09-13 10:04:46 CEST; 3 days ago
    ODFEngine
       Active: active (running) since Mon 2021-09-13 10:04:20 CEST; 3 days ago
    

    然后

    awk '{ORS=NR%2?"\n":" ";print}' file.txt
    

    输出

    server1
    ds.rc    Active: active (running) since Mon 2021-09-13 10:04:48 CEST; 3 days ago
    DFDServer    Active: active (running) since Mon 2021-09-13 10:05:02 CEST; 3 days ago
    ISFAgents    Active: active (running) since Mon 2021-09-13 10:04:46 CEST; 3 days ago
    ODFEngine    Active: active (running) since Mon 2021-09-13 10:04:20 CEST; 3 days ago
    

    说明:如果当前行数 (NR) 是奇数(即除以 2 的余数非零),则打印此行后跟换行符,否则使用空格(condition?value1:value2AWK 的三进制运算符,如果满足条件则使用value1,否则使用value2)。换句话说,将每个偶数行和后续行连接起来,它们之间有空格。

    (在 gawk 4.2.1 中测试)

    【讨论】:

      【解决方案2】:

      您可以从此设置服务变量:

      isengine_services=( ds.rc DFDServer ISFAgents ODFEngine)
      

      变成这样:

      isengine_services="ds.rc DFDServer ISFAgents ODFEngine"
      

      并替换内部循环:

        for i in "${isengine_services[@]}"
        do
           sshpass -p "mysecret" ssh  -o StrictHostKeyChecking=no  "${USERNAME}"@"${h}" "echo ${i}; systemctl status ${i} | grep Active" >> tmp.txt
        done
      

      用这个:

      echo $h  >> tmp.txt
      sshpass -p "mysecret" ssh  -o StrictHostKeyChecking=no  "${USERNAME}"@"${h}" "systemctl status $isengine_services"  >> tmp.txt
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多