1

I'm new to Python and to pexpect, but not to TCL expect. I have a super simple script that connects to a Cisco router via SSH and queries the arp table for a mac address. I want to retrieve the corresponding IP address. Here are the lines of interest:

branchConnection.sendline('sh ip arp | i 00c0.b7')
time.sleep(1)
branchConnection.expect('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')
print(str(branchConnection.match.group(0)))

It prints b'10.198.7.10'

When I look at the pexpect.pty_spawn.spawn object it shows:

buffer (last 100 chars): b'             2   00c0.b790.b8c6  ARPA   GigabitEthernet0/0.100\r\nRTR-01#'
before (last 100 chars): b'sh ip arp | i 00c0.b7\r\nInternet  '
after: b'10.198.7.10'

Everything is encased in b' '

I'm assuming this is normal? Can someone please explain what is going on? I'm having trouble finding anything that explains what this means. How can I get pexpect to just return the IP address without b''?

0xDECAFBAD
  • 154
  • 1
  • 10
  • 1
    see here http://stackoverflow.com/questions/6269765/what-does-the-b-character-do-in-front-of-a-string-literal it means it is a byte string. – Doon Feb 23 '16 at 18:19
  • `group(0)` is the entire match. you can have a try of `group(1)` to see the difference . – Quinn Feb 23 '16 at 18:33

1 Answers1

1

b means bytes. If you need a string out of it you can use decode. You will need to know what encoding it is using, but if you don't know, you can try utf-8 which is pretty common

print(branchConnection.match.group(0).decode("utf-8"))


>>> b'10.198.7.10'.decode("utf-8")
'10.198.7.10'
Doon
  • 19,719
  • 3
  • 40
  • 44