Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added snmpwalk convert support for Opaque extra types UInt64 and Int64 #141

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 44 additions & 3 deletions snmpsim/grammar/walk.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,52 @@ def _string_filter(value):

@staticmethod
def _opaque_filter(value):
if value.upper().startswith('FLOAT: '):
opaque_tag = ''
match = re.match(r'^(\w+: +)', value)
if match:
opaque_tag = match.group(1).upper()
value = value[len(opaque_tag):]

if opaque_tag.startswith('FLOAT: '):
# .1.3.6.1.4.1.6574.4.3.1.1.0 = Opaque: Float: 100.000000
return encoder.encode(univ.Real(float(value[7:])))

else:
return [int(y, 16) for y in value.split(' ')]
elif opaque_tag.startswith('UINT64: '):
# .1.3.6.1.4.1.2021.10.1.6.2 = Opaque: UInt64: 18446744073709551614
# there should be BER encoder, but really I not how do this,
# I just convert to HEX value instead
hex = '{:X}'.format(int(value))
add = len(hex) % 2
hex = add * '0' + hex
parts = [hex[i:i + 2] for i in range(0, len(hex), 2)]

# value len
hex = '{:X}'.format(len(parts))
add = len(hex) % 2
hex = add * '0' + hex
parts = [hex[i:i + 2] for i in range(0, len(hex), 2)] + parts

# UInt64 subtag is 9f7b
value = '9F 7B ' + ' '.join(parts)

elif opaque_tag.startswith('INT64: '):
# .1.3.6.1.4.1.2021.10.1.6.3 = Opaque: Int64: 9223372036854775806
# .1.3.6.1.4.1.2021.10.1.6.4 = Opaque: Int64: -2
hex = '{:X}'.format(int(value) & (2**64-1))
add = len(hex) % 2
hex = add * '0' + hex
parts = [hex[i:i + 2] for i in range(0, len(hex), 2)]

# value len
hex = '{:X}'.format(len(parts))
add = len(hex) % 2
hex = add * '0' + hex
parts = [hex[i:i + 2] for i in range(0, len(hex), 2)] + parts

# Int64 subtag is 9f7a
value = '9F 7A ' + ' '.join(parts)

return [int(y, 16) for y in value.split(' ')]

@staticmethod
def _bits_filter(value):
Expand Down