Issue
I have script which works for me at least but the padding or say it markers for aligning or justifying the columns are not working good and i'm struggling to get the best solutions for it.
Below is the script which basically gets the Os Release version
and Perl version
running on the system.
if you look at the Result section the outer lining or markers which i defined are getting destorted .
Script:
#!/bin/bash
printf "\n"
marker=$(printf "%0.s-" {1..65})
printf "|$marker|\n"
printf "|%-10s | %-13s | %29s |\n" "Hostname" "RedHat Vesrion" "Perl Version"
printf "|$marker|\n"
remote_connect() {
target_host=$1
marker=$(printf "%0.s-" {1..65})
remote_data=($(
ssh -i ssh_prod "root@${target_host}" -o StrictHostKeyChecking=no -o PasswordAuthentication=no "
rhelInfo=\$(cat /etc/redhat-release | awk 'END{print \$7}')
perlInfo=\$(rpm -qa | grep -i mod_perl)
echo \$rhelInfo \$perlInfo
"))
rhelInfo=${remote_data[0]}
perlInfo=${remote_data[1]}
if [[ $? -eq 0 ]]
then
printf "|%-10s | %-13s | %20s |\n" "$target_host" "$rhelInfo" "$perlInfo"
else
printf "|%-10s | %-13s | %20s |\n" "$target_host" "Unable to get the ssh connection"
fi
} 2>/dev/null
export -f remote_connect
< CVE-2011-2767-hostsList.txt xargs -P30 -n1 -d'\n' bash -c 'remote_connect "$@"' --
Result:
|-----------------------------------------------------------------|
|Hostname | RedHat Vesrion | Perl Version |
|-----------------------------------------------------------------|
|fsx9015 | | |
|fsx1258 | | |
|fsx1195 | | |
|fsx1063 | | |
|fsx1241 | 6.9 | mod_perl-2.0.4-12.el6_10.x86_64 |
|fsx1242 | 6.9 | mod_perl-2.0.4-12.el6_10.x86_64 |
|fsx1210 | 6.7 | mod_perl-2.0.4-12.el6_10.x86_64 |
|fsx1484 | 6.9 | mod_perl-2.0.4-12.el6_10.x86_64 |
Required:
|-----------------------------------------------------------------|
|Hostname | RedHat Vesrion| Perl Version |
|-----------------------------------------------------------------|
|fsx9015 | | |
|fsx1258 | | |
|fsx1195 | | |
|fsx1063 | | |
|fsx1241 | 6.9 | mod_perl-2.0.4-12.el6_10.x86_64 |
|fsx1242 | 6.9 | mod_perl-2.0.4-12.el6_10.x86_64 |
|fsx1210 | 6.7 | mod_perl-2.0.4-12.el6_10.x86_64 |
|fsx1484 | 6.9 | mod_perl-2.0.4-12.el6_10.x86_64 |
please help me understand or get a better way of doing it.
Solution
I'd suggest you don't try to start formatting until you have all the data at hand: each column should be as wide as the longest value in it.
Also, to make your code simpler, use column
instead of the "fancy" table. I'm sure there's a utility that can draw ASCII table borders, but is it really necessary?
This version just prints comma-separated values from the ssh connections, then uses column
to add the whitespace.
Note that I'm using a quoted heredoc for the code to send to the target host, which simplifies quoting.
#!/bin/bash
remote_connect() {
target_host=$1
if remote_data=$(
ssh -i ssh_prod "root@${target_host}" -o StrictHostKeyChecking=no -o PasswordAuthentication=no <<'END_SSH'
rhelInfo=$(awk 'END{print $7}' /etc/redhat-release)
perlInfo=$(rpm -qa | grep -i mod_perl)
echo "$rhelInfo,$perlInfo"
END_SSH
)
then
echo "${target_host},${remote_data}"
else
echo "${target_host},?,?"
fi
} 2>/dev/null
export -f remote_connect
{
echo "Hostname,RedHat Version,Perl Version"
< CVE-2011-2767-hostsList.txt xargs -P30 -n1 -d'\n' bash -c 'remote_connect "$@"' --
} | column -s"," -t
Answered By - glenn jackman Answer Checked By - Marie Seifert (WPSolving Admin)