2

I am trying to understand network automation and I need to get the device model details from 2-3 different cisco devices.

I am using paramiko in python to run my command on the device and in return, I want to extract only the device model name from the console output.

Examples of output:

Device1-

PID: DEV1-123CE-D3T        ,  VID:    unimportant string   ,  SN: 1

PID: DEV1-123CE-D3T        ,  VID:    unimportant string   ,  SN: 1

Device2-

PID:                       ,  VID:    unimportant string   ,  SN: 1

PID: ABC2-123CE-D3T        ,  VID:    unimportant string   ,  SN: 1

Device3-

PID: XYZ3-456DF-A1N                      ,  VID:    unimportant string   ,  SN: 1

PID: XYZ3-456DF-A1N                      ,  VID:    unimportant string   ,  SN: 1

I just need details of PID. If the PID is empty in the first line, then the code should search for the second PID in the output and post the match.

Relevant part of code I have so far which is only searching data in the first line and returning a 'None' value -

stdout = stdout.readlines()

lines = list(islice(stdout, 2))
for line in lines:    
    result = re.findall('PID:(.*)\t\t,', line)    
    print(result)

What changes do I need to make to my code to get the desired output?

Thanks in advance!

0

1 Answer 1

1

You can use

m = re.search(r'\bPID:\s*([^\s,]+)', line)
if m:
    print(m.group(1))

See the regex demo.

The \bPID:\s*([^\s,]+) regex matches

  • \b - a word boundary
  • PID: - a fixed string
  • \s* - zero or more whitespaces
  • ([^\s,]+) - Group 1: one or more chars other than a comma and whitespace.
  • \bPID:\s*([^\s,]+)

To find all values in a string, keep on using findall:

re.findall(r'\bPID:\s*([^\s,]+)', line)
Sign up to request clarification or add additional context in comments.

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.