Continuing my theme of using Python to help with Cisco product administration, here's how to convert timestamps and IP addresses to human-readable format.
Timestamps are pretty easy. They're recorded in UNIX epoch time and there are lots of examples available showing how to convert them using Excel or various scripting tools. In Python:
import time
def time_to_string(time_value):
"""convert Unix epoch time to a human readable string"""
return time.strftime("%m/%d/%Y %H:%M:%S",time.localtime(float(time_value)))
It took me a while to figure out how to convert IPv4 addresses, since CUCM uses signed 32-bit integers to represent IP addresses and Python's long integers can be of infinite length. First, a review of how to do the conversion manually is in order. Let's say that a CUCM CDR lists an IP address as "-2126438902". First, we convert this signed 32-bit integer to hex using Windows calculator:
-2126438902 = 0x81411E0A
Next, we break the hex number into 1-byte chunks, and reverse the order:
0x0A
0x1E
0x41
0x81
Finally, we convert each byte to decimal and put them together into an IP address:
10.30.65.129
Here's the Python function to do the conversion:
And here it is in the interpreter showing that it works for reasonable input types:
>>> int_to_ip('-2126438902')
'10.30.65.129'
>>> int_to_ip(-2126438902)
'10.30.65.129'
>>> int_to_ip(-2126438902L)
'10.30.65.129'
I don't have access to an IPv6-aware CUCM install, so you're on your own for that!
No comments:
Post a Comment