octet string conversion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matthew

    octet string conversion

    I am working on some software that uses SNMP to get information from
    routers and switches. I am using the pysnmp module (v2.0.8) to do the
    snmp part. The problem that I am having is that when I query a router
    for mac addresses pysnmp returns octetstrings like this:

    \000\002\263\25 4\264\351
    \010\000 \301F\021
    \010\000 \300\303[
    \000\002\263\25 4\264\241

    what I need though is hex strings like this:

    0:e0:7d:de:5:48
    0:e0:7d:c8:dc:9 f
    8:0:36:4:3b:de
    0:80:ad:3a:9e:2 b

    Can anyone tell me how to convert the octet strings to hex strings?

    Thanks very much
    -matthew
  • Terry Reedy

    #2
    Re: octet string conversion


    "Matthew" <ruach@chpc.uta h.edu> wrote in message
    news:ec1162c7.0 307281214.50b68 373@posting.goo gle.com...[color=blue]
    > I am working on some software that uses SNMP to get information from
    > routers and switches. I am using the pysnmp module (v2.0.8) to do[/color]
    the[color=blue]
    > snmp part. The problem that I am having is that when I query a[/color]
    router[color=blue]
    > for mac addresses pysnmp returns octetstrings like this:
    >
    > \000\002\263\25 4\264\351
    > \010\000 \301F\021
    > \010\000 \300\303[
    > \000\002\263\25 4\264\241
    >
    > what I need though is hex strings like this:
    >
    > 0:e0:7d:de:5:48
    > 0:e0:7d:c8:dc:9 f
    > 8:0:36:4:3b:de
    > 0:80:ad:3a:9e:2 b
    >
    > Can anyone tell me how to convert the octet strings to hex strings?[/color]

    Is this what you want?[color=blue][color=green][color=darkred]
    >>> s=repr('\000\00 2\263\254\264\3 51').replace(r' \x', ':')
    >>> s[/color][/color][/color]
    "':00:02:b3:ac: b4:e9'" # double " single ' ... single ' double "[color=blue][color=green][color=darkred]
    >>> s[2:-1][/color][/color][/color]
    '00:02:b3:ac:b4 :e9'

    Terry J. Reedy



    Comment

    • Erik Max Francis

      #3
      Re: octet string conversion

      Matthew wrote:
      [color=blue]
      > I am working on some software that uses SNMP to get information from
      > routers and switches. I am using the pysnmp module (v2.0.8) to do the
      > snmp part. The problem that I am having is that when I query a router
      > for mac addresses pysnmp returns octetstrings like this:
      >
      > \000\002\263\25 4\264\351
      > \010\000 \301F\021
      > \010\000 \300\303[
      > \000\002\263\25 4\264\241[/color]

      I presume these are the repr of the strings you get back. If so, this
      is simply in binary format. To convert, take the ord of each character
      and print the hexadecimal code:
      [color=blue][color=green][color=darkred]
      >>> S = '\010\000 \301F\021'
      >>> ':'.join(['%x' % ord(x) for x in S])[/color][/color][/color]
      '8:0:20:c1:46:1 1'

      --
      Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
      __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
      / \ Take the slow train / To my destination
      \__/ Sandra St. Victor

      Comment

      • Bengt Richter

        #4
        Re: octet string conversion

        On 28 Jul 2003 13:14:50 -0700, ruach@chpc.utah .edu (Matthew) wrote:
        [color=blue]
        >I am working on some software that uses SNMP to get information from
        >routers and switches. I am using the pysnmp module (v2.0.8) to do the
        >snmp part. The problem that I am having is that when I query a router
        >for mac addresses pysnmp returns octetstrings like this:
        >
        >\000\002\263\2 54\264\351
        >\010\000 \301F\021
        >\010\000 \300\303[
        >\000\002\263\2 54\264\241
        >
        >what I need though is hex strings like this:
        >
        >0:e0:7d:de:5:4 8
        >0:e0:7d:c8:dc: 9f
        >8:0:36:4:3b: de
        >0:80:ad:3a:9e: 2b
        >
        >Can anyone tell me how to convert the octet strings to hex strings?
        >[/color]
        Assuming that your data is really octal character representations like \nnn,
        and the spaces, F, and [ in the middle lines are typos, and that your need example
        has nothing to do with the example data,
        [color=blue][color=green][color=darkred]
        >>> data = r"""[/color][/color][/color]
        ... \000\002\263\25 4\264\351
        ... \010\000\301\02 1
        ... \010\000\300\30 3
        ... \000\002\263\25 4\264\241
        ... """[color=blue][color=green][color=darkred]
        >>> for line in data.splitlines ():[/color][/color][/color]
        ... if not line: continue # skip the first \n in data above
        ... print 'line: %r' %line
        ... oct3list = line.split('\\' )[1:]
        ... print 'oct3list: %r'%oct3list
        ... octvals = [int(oct3,8) for oct3 in oct3list]
        ... print 'octvals: %r'%octvals
        ... hexstrings = ['%02X'%octval for octval in octvals]
        ... print 'hexstrings: %r'%hexstrings
        ... print 'joined with colons => %r' % ':'.join(hexstr ings)
        ... print
        ...
        line: '\\000\\002\\26 3\\254\\264\\35 1'
        oct3list: ['000', '002', '263', '254', '264', '351']
        octvals: [0, 2, 179, 172, 180, 233]
        hexstrings: ['00', '02', 'B3', 'AC', 'B4', 'E9']
        joined with colons => '00:02:B3:AC:B4 :E9'

        line: '\\010\\000\\30 1\\021'
        oct3list: ['010', '000', '301', '021']
        octvals: [8, 0, 193, 17]
        hexstrings: ['08', '00', 'C1', '11']
        joined with colons => '08:00:C1:11'

        line: '\\010\\000\\30 0\\303'
        oct3list: ['010', '000', '300', '303']
        octvals: [8, 0, 192, 195]
        hexstrings: ['08', '00', 'C0', 'C3']
        joined with colons => '08:00:C0:C3'

        line: '\\000\\002\\26 3\\254\\264\\24 1'
        oct3list: ['000', '002', '263', '254', '264', '241']
        octvals: [0, 2, 179, 172, 180, 161]
        hexstrings: ['00', '02', 'B3', 'AC', 'B4', 'A1']
        joined with colons => '00:02:B3:AC:B4 :A1'

        and I got it wrong because I used fixed 2-char hex in upper case,
        and I'm showing the repr() of the line, which doubles the \'s,
        then you can fix it ;-)

        And get this for the data:

        line: \000\002\263\25 4\264\351
        joined with colons => '0:2:b3:ac:b4:e 9'

        line: \010\000\301\02 1
        joined with colons => '8:0:c1:11'

        line: \010\000\300\30 3
        joined with colons => '8:0:c0:c3'

        line: \000\002\263\25 4\264\241
        joined with colons => '0:2:b3:ac:b4:a 1'

        In future, please post actual data and corresponding output
        (or if you did this time, explain the anomalies). Otherwise
        it's a guessing game, and wastes all of our time except for
        silly finger exercises.

        Regards,
        Bengt Richter

        Comment

        • Mike C. Fletcher

          #5
          Re: octet string conversion

          Bengt Richter wrote:
          [color=blue]
          >On 28 Jul 2003 13:14:50 -0700, ruach@chpc.utah .edu (Matthew) wrote:
          >
          >
          >[color=green]
          >>I am working on some software that uses SNMP to get information from
          >>routers and switches. I am using the pysnmp module (v2.0.8) to do the
          >>snmp part. The problem that I am having is that when I query a router
          >>for mac addresses pysnmp returns octetstrings like this:
          >>
          >>\000\002\263\ 254\264\351
          >>\010\000 \301F\021
          >>\010\000 \300\303[
          >>\000\002\263\ 254\264\241
          >>
          >>what I need though is hex strings like this:
          >>
          >>0:e0:7d:de:5: 48
          >>0:e0:7d:c8:dc :9f
          >>8:0:36:4:3b:d e
          >>0:80:ad:3a:9e :2b
          >>
          >>Can anyone tell me how to convert the octet strings to hex strings?
          >>
          >>[/color]
          >Assuming that your data is really octal character representations like \nnn,
          >and the spaces, F, and [ in the middle lines are typos, and that your need example
          >has nothing to do with the example data,
          >[/color]
          (I'll go from the other assumption, that he has simple strings of
          octets, that is, binary data)
          [color=blue][color=green][color=darkred]
          >>> def hexify( octets ):[/color][/color][/color]
          .... return ":".join( [ '%x'%(ord(c)) for c in octets ] )
          ....[color=blue][color=green][color=darkred]
          >>> hexify ( '\010\000 \301F\021' )[/color][/color][/color]
          '8:0:20:c1:46:1 1'[color=blue][color=green][color=darkred]
          >>> hexify ( '\000\002\263\2 54\264\351' )[/color][/color][/color]
          '0:2:b3:ac:b4:e 9'

          HTH,
          Mike

          _______________ _______________ _________
          Mike C. Fletcher
          Designer, VR Plumber, Coder





          Comment

          • Matthew

            #6
            Re: octet string conversion

            Thats great! I really appreciate the help. The F and spaces and ] etc,
            are not typos they real output via pysnmp.I used Erik Max Francis'
            code and it did exactly what I needed. Thank you all for
            contributing.

            -matthew

            Comment

            Working...