Add Timestamps To Your Junos CLI Output
This post is about yet another cool Junos trick you can use on your Juniper Networks devices.
Suppose you’re tracking the state of something in your network, to see how often it changes. Perhaps an interface is flapping (going up and down), or perhaps a route keeps appearing and disappearing.
As another example, perhaps you’re using the hidden Junos command “show government secrets“, which gives you the ability to monitor all government secrets in real time. Use this command to find out about the latest alien sightings, information on next week’s lottery numbers, and a full list of people called George within a one mile radius of your location (helpful for meeting new Georges). This command is so hidden that it doesn’t even work when you type it. Wow! Sounds pretty advanced, if you ask me.
Anyway, forget I said any of that. Whenever you need to type the same command multiple times to track an event change, it’s helpful to add a timestamp to the output. This makes it much easier to remember exactly when each event happened, in case you need to cross-reference the event changes with logs, or send the results to a colleague, customer, or lover.
WELL HEY GUESS WHAT – Junos gives you the ability to do exactly this!
All you need to do is type “set cli timestamp” in operational mode.
Notice that I said in operational mode. Don’t be fooled by the fact that this is a “set” command. In fact, there are some set commands that you use in operational mode to temporarily define settings for your current CLI session. These settings are lost when you log out. I taught you another example in a previous post.
Let me show you what it looks like.
SETTING A CLI TIMESTAMP
I used vLabs to generate this output – which, if you don’t know already, is a way to get hands-on with Junos for free in a virtual environment. Click here to take a look.
Without this option, the output of a command might look like this:
jcluser@vMX1> show ospf neighbor Address Interface State ID Pri Dead 10.100.12.2 ge-0/0/0.0 Full 10.100.100.2 128 37 10.100.14.2 ge-0/0/1.0 Full 10.100.100.4 128 26 10.100.13.2 ge-0/0/2.0 Full 10.100.100.3 128 37 jcluser@vMX1> show ospf neighbor Address Interface State ID Pri Dead 10.100.12.2 ge-0/0/0.0 Full 10.100.100.2 128 38 10.100.13.2 ge-0/0/2.0 Full 10.100.100.3 128 38
Looks like a neighbor went down. But when? Sadly, that information is lost to the sands of time.
Let’s now enable timestamps:
jcluser@vMX1> set cli timestamp Jan 20 17:45:59 CLI timestamp set to: %b %d %T
Notice how the timestamp seems to have a default format. We’ll explore that in a moment. For now, here’s how your ouptut changes after you deploy this command:
jcluser@vMX1> show ospf neighbor Jan 20 17:46:44 Address Interface State ID Pri Dead 10.100.12.2 ge-0/0/0.0 Full 10.100.100.2 128 38 10.100.13.2 ge-0/0/2.0 Full 10.100.100.3 128 34 jcluser@vMX1> show ospf neighbor Jan 20 17:46:52 Address Interface State ID Pri Dead 10.100.12.2 ge-0/0/0.0 Full 10.100.100.2 128 37 10.100.14.2 ge-0/0/1.0 Init 10.100.100.4 128 34 10.100.13.2 ge-0/0/2.0 Full 10.100.100.3 128 33
Your timestamp will now appear above every command you type. Very handy!
Of course, this is only helpful if you also have accurate time on your device, whether it be from setting it manually (click here for a guide) or by enabling communication with an external NTP (Network Time Protocol) server (click here for a guide).
Cancelling it is just as easy—simply type “set cli timestamp disable“.
CUSTOMISING THE TIMESTAMP
Let’s revisit the timestamp format:
jcluser@vMX1> set cli timestamp Jan 20 17:45:59 CLI timestamp set to: %b %d %T
- %b means “The first three letters of the month” (Jan).
- %d means date.
- %T means time, in the format hh:mm:ss.
There are many other options—but somewhat frustratingly, hardly any of them are documented on the Juniper website.
However, through trial and error, I believe I’m correct in saying that the format options are based off of something called “strftime”, which is a date/time function in the C and Python programming languages. There’s a full list of codes here, with examples: https://strftime.org/
Two especially useful options are %Y to set the year in a four-digit format, and %a to display the first three letters of the day. Errr. “a” for dAy?
Let me show you these options in action. To customize the timestamp, use the keyword “format“, followed by your list of options in quotation marks.
As soon as you type it, notice that you see your old timestamp one more time, followed by your new timestamp. After that, only your new timestamp will appear in future comands.
jcluser@vMX1> set cli timestamp format "%a %b %d %Y %T" Jan 20 18:03:17 Sat Jan 20 2024 18:03:17 CLI timestamp set to: %a %b %d %T jcluser@vMX1> Sat Jan 20 2024 18:03:17 jcluser@vMX1>
Other useful examples include, but are definitely not limited to:
- %A = Full name of the day (Saturday)
- %B = Full name of the month (January)
- %c = Full date/time/year (Sat Jan 20 18:15:47 2024)
- %D = Month/date/year (20/01/24)
- %G = Full year (2024)
THAT’S IT!
I hope you use this nice tip to make people happy and to do good things. Please do NOT use the “set cli timestamp” command in a quest for world domination, or as part of any plan to create an army of Super Frogs with the intelligence of a dolphin and the strength of an ox. I do not want this outcome. Not even a little bit.
Hey, thank you for checking out my web site! (When I say “web” site, I don’t mean like the webbed feet of a Super Frog. Again, I do NOT want this outcome.) If you’re new here, take a look through my older posts. I’ve written tons of posts about Junos, BGP, MPLS, OSPF, and all sort of other cool networking tech.
If you’re on Mastodon, follow me to find out when I make new posts. (2024 edit: I’m also on BlueSky nowadays too. I was once on Twitter, but I’ve given up on it, on account of… well, I don’t need to finish that sentence, do I.)
Thanks for reading! Please do not conquer the Earth!