Sign in to follow this  
Followers 0
gamarga

Regex Time Date Format - Plink Cisco Output

6 posts in this topic

Hi,

I am using plink to run some show commands on a Cisco router. Here is an example output:

ROUTER#sh caller

Active Idle

Line User Service Time Time

vty 6 drascal VTY 00:00:08 00:00:00

Vi2 uae.london.uk PPPoATM 4d04h 00:00:00 <<<<<<<<<<<

ROUTER#sh ver | i uptime

ROUTER uptime is 1 week, 2 days, 22 hours, 53 minutes <<<<<<<<<<<

I want to be able to compare the output time from sh caller to the uptime in the sh ver command and work out the difference. Only problem is the output format can change on the output from both commands, like sh caller if less than 24 hours will be hh:mm:ss format.

I am really struggling with regex and parsing strings in general. Can anyone help?

Share this post


Link to post
Share on other sites



You want to compare

Vi2 uae.london.uk PPPoATM 4d04h 00:00:00 <<<<<<<<<<<

And

ROUTER uptime is 1 week, 2 days, 22 hours, 53 minutes <<<<<<<<<<<

?

Thanks..!!

Share this post


Link to post
Share on other sites

Corect yes. The actual times I need comparing.

4d04h

1 week, 2 days, 22 hours, 53 minutes

Obviously they are different formats so I know they will need to be converted to the same formate first I guess?

Share this post


Link to post
Share on other sites

Two questions:

What about 00:00:00 in sh output? Is this always 00:00:00 and you need alway 4d04h?

What do you mean with compare? Do you want the date diff?

br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

I don't fully understand the expression 4d04h, but I just thought that you could get rid of anything that isn't a digit. Perhaps something like this. though it will need some refinement. This is just an idea, since it's also isn't exactly clear to me what you want.

$sString = StringStripWS(StringRegExpReplace("1 week, 2 days, 22 hours, 53 minutes", "[\D]+", " "), 7)
MsgBox(0, "", $sString)
 
$sString = StringStripWS(StringRegExpReplace("4d04h", "[\D]+", " "), 7)
MsgBox(0, "", $sString)
Edited by czardas

Share this post


Link to post
Share on other sites

This should give you some ideas.

#include <Array.au3>

Local $sOutput = "ROUTER#sh caller" & @CRLF & _
        "Active Idle" & @CRLF & _
        "Line User Service Time Time" & @CRLF & _
        "vty 6 drascal VTY 00:00:08 00:00:00" & @CRLF & _
        "Vi2 uae.london.uk PPPoATM 4d04h 00:00:00 <<<<<<<<<<<" & @CRLF & @CRLF & @CRLF & @CRLF & _
        "ROUTER#sh ver | i uptime" & @CRLF & _
        "ROUTER uptime is 1 week, 2 days, 22 hours, 53 minutes <<<<<<<<<<<"

Local $iTime1 = _Time2Secs(StringRegExpReplace($sOutput, ".*(?is).*ROUTER#sh caller.+ATM ([^<]+) <+.*$", "\1"))
Local $iTime2 = _Time2Secs(StringRegExpReplace($sOutput, ".*(?is).*ROUTER#sh ver.+uptime is ([^<]+) <+.*$", "\1"))
MsgBox(0, "Results", "ROUTER#sh caller: " & $iTime1 & " secs" & @CRLF & _
        "ROUTER#sh ver: " & $iTime2 & " secs" & @CRLF & _
        "Time Difference : " & Abs($iTime2 - $iTime1) & " secs")


Func _Time2Secs($sString)
    Local $iTime1Secs = 0
    If StringRegExp($sString, "(\d{2}):(\d{2}):(\d{2})") Then $sString = StringRegExpReplace($sString, "(.*) (\d{2}):(\d{2}):(\d{2}).*", "\1 \2 hours \3 minutes \4 seconds")
    Local $aArray = StringRegExp($sString, "(\d+)(?:[ ,\z]*)([^\d, ]+)", 3)
    ;_ArrayDisplay($aArray, $sString)
    For $i = 0 To UBound($aArray) - 2 Step 2
        $iTime1Secs += _NumOfSecs(Number($aArray[$i]), $aArray[$i + 1])
        ;ConsoleWrite(Number($aArray[$i]) & " " &  $aArray[$i + 1] & "  " & $iTime1Secs & @LF)
    Next
    Return $iTime1Secs
EndFunc   ;==>_Time2Secs

Func _NumOfSecs($iAmnt, $sUnits)
    Select
        Case StringRegExp($sUnits, "(?i)week")
            Return $iAmnt * 7 * 24 * 60 * 60
        Case StringRegExp($sUnits, "(?i)day|d")
            Return $iAmnt * 24 * 60 * 60
        Case StringRegExp($sUnits, "(?i)hour|h")
            Return $iAmnt * 60 * 60
        Case StringRegExp($sUnits, "(?i)minute|m")
            Return $iAmnt * 60
    EndSelect
    Return $iAmnt ; seconds
EndFunc   ;==>_NumOfSecs

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0