i am trying to create a script to get the Service status from different Remote servers:
#!/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
I get very chaotic output that look like this:
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
is it possible to make it look the following way:
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
CodePudding user response:
You might use GNU AWK
for converting from two-liners to one-liners as follows, let file.txt
content be
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
then
awk '{ORS=NR%2?"\n":" ";print}' file.txt
output
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
Explanation: If number of current row (NR
) is odd (i.e. remainder of division by 2 is non-zero) then print this line followed by newline otherwise use space (condition?value1:value2
is AWK
's ternary operator, if condition is meet value1
is used, otherwise value2
is used). In other words join every even line and subsequent line with space between them.
(tested in gawk 4.2.1)
CodePudding user response:
You can set services variable from this:
isengine_services=( ds.rc DFDServer ISFAgents ODFEngine)
to be this:
isengine_services="ds.rc DFDServer ISFAgents ODFEngine"
and replace the internal loop:
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
with this:
echo $h >> tmp.txt
sshpass -p "mysecret" ssh -o StrictHostKeyChecking=no "${USERNAME}"@"${h}" "systemctl status $isengine_services" >> tmp.txt