CrossingTheRoad2020
CrossingTheRoad2020

Reputation: 61

Python Multiline String Find

I have an Cisco ASA with a VPN tunnel configured. I call the CLI command via API and it returns this multiline string:

\nSession Type: LAN-to-LAN\n\nConnection   : 192.168.1.10\nIndex        : 11701                  IP Addr      : 192.168.1.10\nProtocol     : IKEv2 IPsecOverNatT\nEncryption   : IKEv2: (1)AES256  IPsecOverNatT: (1)AES256\nHashing      : IKEv2: (1)SHA256  IPsecOverNatT: (1)SHA256\nBytes Tx     : 0                      Bytes Rx     : 0\nLogin Time   : 23:14:43 EST Fri Dec 3 2021\nDuration     : 0h:11m:50s\n\n

I can't figure out how to get only the "Bytes Rx" plus the number out beside it. I've tried searching it like this, but it returns "Bytes Tx":

import re
regex_parse = re.compile(r'[a-zA-Z]+\s[a-zA-Z][a-zA-Z]\s+:\s[0-9]+')

multilinestring = webhook_api_call()
for item in multilinestring:
    a = regex_parse.search(item)
print(a.group(0))

Output:

Bytes Tx     : 0

I want to only get Bytes Rx and the number out beside it

Upvotes: 1

Views: 147

Answers (1)

Rob G
Rob G

Reputation: 683

Looks like you are trying to parse the result of sh vpn-sessiondb l2l from a Cisco ASA. The output is pretty standard, so I would skip the regex and do the following:

multilinestring = webhook_api_call()
lines = multilinestring.split("\n")
for l in lines:
    if l.find("Bytes Tx") != -1:
        print("Bytes Rx" + l.partition("Bytes Rx")[2])

Output:

Bytes Rx      : 0

Good luck with your code!

Upvotes: 1

Related Questions