1

I am running a for loop against set of servers to find the cert expiry. Output of the cert command will be below for each server:

line 1
line 2
line 3
Valid From: Tuesday, 29 September 2015 10:40:13 AM To: Friday, 28 September 2018 10:40:13 AM
line 5

Goal is to extract only date part after To:

I was able to get this working with following command

sed -n -e '/Valid/ s/.*\, *//p' |awk '{print $1 " " $2 " " $3 }')"

But the problem is it is printing only the last occurence ignoring all the occurences of "Valid"

Here is the for loop I am running

for host in `cat /home/MO/server.txt`
do
for qmgr in $(ssh musr@$host dspmq)
do
certexp="$(ssh musr@$host <cert command> <path to cert> |sed -n -e '/Valid/ s/.*\, *//p' |awk '{print $1 " " $2 " " $3 }')"
echo "$host     :       $qmgr      :       $certexp" > out.txt
done
done

the output file is print only from the last server in the server.text file.

Idea was to have a file with host name, qmgr name and cert expiry date, sort it by date and then take action.

  • try >> out.txt – JJoao Sep 30 '15 at 17:04
  • Aha., thank you. I had that in the backup script and forgot to make this change. – MO12 Sep 30 '15 at 17:06
  • Not sure if I have to start a new question for this but let me ask anyway. Is there a way I can use the above mentioned time and date format to sort the contents in the file. Or do I have to convert to epoch time and then sort it? – MO12 Sep 30 '15 at 17:08
  • You would have to convert to a sortable value. epoch or iso – Sobrique Sep 30 '15 at 18:11
1
while read -r host
do
    for qmgr in $(ssh musr@$host dspmq)
    do
        ssh musr@$host <cert command> <path to cert> |
        sed -n "s/ *..:[^,]*$//;/Valid/s/.*\, */$host\t:\t$qmgr\t:\t/p"
    done
done < "/home/MO/server.txt" > out.txt
0

Rather than sed you could probably do something similar in perl:

#!/usr/bin/env perl

use strict;
use warnings;

use Time::Piece;

local $/;
my ( $to ) = <DATA> =~ m/To: (.*)$/gm;
#print $to;
# print localtime->strftime("%A, %d %B %Y %H:%M:%S %p");
my $timestamp = Time::Piece -> strptime($to, "%A, %d %B %Y %H:%M:%S %p");
print $timestamp -> epoch, " => ", $timestamp -> strftime("%Y-%m-%d %H:%M:%S");

__DATA__
line 1
line 2
line 3
Valid From: Tuesday, 29 September 2015 10:40:13 AM To: Friday, 28 September 2018 10:40:13 AM
line 5

Which as a one liner would be:

perl -0777 -e 'print Time::Piece -> strptime(<>=~m/To: (.*)$/gm, "%A, %d %B %Y %H:%M:%S %p") -> strftime("%Y-%m-%d %H:%M:%S");'

Which should extract the desired date and convert it to a sortable format. (or you could use epoch of course.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.