0

I have the following IP in hex: 0xac141315

I would like to convert it to its DDN equivalent. I used to use '.'.join([str(x) for x in 0xac141315.asNumbers()]) when 0xac141315 had the following type: <class 'CiscoNetworkAddress'>

Or is there a way to convert the string back to CiscoNetworkAddress?

2 Answers 2

1

According to CISCO-TC MIB, CiscoNetworkAddress is actually an octet string. There are many ways to turn octet string into a DDN. For example, with ipaddress module from Python 3 stdlib:

>>> from pysnmp.hlapi import OctetString
>>> ip = OctetString(hexValue='ac141315')  # this is what you have
>>>
>>> ipaddress.IPv4Address(ip.asOctets())
IPv4Address('172.20.19.21')

Or you can turn it into SNMP SMI IP address:

>>> from pysnmp.hlapi import IpAddress
>>> IpAddress(ip.asOctets()).prettyPrint()
'172.20.19.21'

Also, you could get a sequence of integer octets right from your existing object:

>>> ip.asNumbers()
(172, 20, 19, 21)

Keep in mind, that CiscoNetworkAddress is designed to hold many different address types, not just IPv4. So you should probably apply the IPv4 conversion only when it's actually IPv4 address.

Sign up to request clarification or add additional context in comments.

Comments

0

Here is a complete example of how to convert to decimal ip:

#!/usr/bin/python  
import sys  
# Must be (0xXXXXXXXX)  
if len(sys.argv)< 2:  
 print("Usage: %s (HEX FORMAT, for example 0xAC141315)" % sys.argv[0])  
sys.exit(0)  
user_option = sys.argv[1]  
hex_data=user_option[2:] 

#Check if length = 8  
if len(hex_data)< 8:  
hex_data = ''.join(('0',hex_data))  

def hex_to_ip_decimal(hex_data):  
 ipaddr = "%i.%i.%i.%i" % (int(hex_data[0:2],16),int(hex_data[2:4],16),int(hex_data[4:6],16),int(hex_data[6:8],16))  
return ipaddr  
result=hex_to_ip_decimal(hex_data)  
print result 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.