Python Forum

Full Version: Python Regex multiple search into separate variable?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hi,
I just want to ask how can I run multiple search using regex using a single Variable with string in it then put the search results to separate variable?

Summary of the operation:

get the ip address from the list
get command and put it on the list
loop the address
loop the commands
print

My code:
import pexpect, getpass, time, re, sys

#OPEN List of Devices
f = open("ipadd.txt")

#OPEN COMMANDS AND PUT INTO LIST
commandFile = open('commands.txt','r')
commands = [i for i in commandFile]

# ENTER CRED
user = input("Enter your remote account: ")
password = getpass.getpass()

# LOOP
for line in f:
    print ("Now accessing the device.. " + line)
    HOST = line.strip()
    child = pexpect.spawn ('telnet '+HOST)
    child.expect ('Username: ')
    child.sendline (user)
    child.expect ('Password: ')
    child.sendline (password)
    # LOOP COMMAND
    for command in commands:
        child.expect ('#')
        child.sendline(command)
        child.expect ('#')
        print("LIST OF COMMAND: ",commands)
        print("EXECUTED COMMAND: ",command)
        show = str(child.before)
        print(show)

# SEARCH VERSION
        regexoutput = re.search('Version \d{2}\.......',show)
        print ("\nVersion:",re.sub("Version ", "", str(regexoutput.group(0))))
# SEARCH IP ADDRESS
        regexoutput2 = re.search('\d\d\.\d{3}\.\d{3}\.\d',show)
        print ("\nMGMT IP:", str(regexoutput2))
Output of print("LIST OF COMMAND: ",commands):

LIST OF COMMAND: ['show ver | i Version\n', 'show ip int brief | i lan\n']
Output of print show and executed command:

Now accessing the device.. 10.200.200.2
EXECUTED COMMAND: show ver | i Version
b'show ver | i Version\r\n
Cisco IOS Software, 3600 Software (C3660-JK9S2-M), Version 12.4(25b), RELEASE SOFTWARE (fc1)\r\nROM: 3600 Software (C3660-JK9S2-M), Version 12.4(25b), RELEASE SOFTWARE (fc1)\r\ntest5'

LIST OF COMMAND: ['show ver | i Version\n', 'show ip int brief | i lan\n']
EXECUTED COMMAND: show ip int brief | i lan

b'show ip int brief | i lan\r\nVlan1 10.200.200.2 YES NVRAM up up \r\ntest5'
While I can't print the regex output?

Error Message:

Traceback (most recent call last):
File "/home/lab-station/pyp/INventory/expect.py", line 41, in <module>
print ("\nVersion:",re.sub("Version ", "", str(regexoutput.group(0))))
AttributeError: 'NoneType' object has no attribute 'group'*
NOTE: Tried Removing the 2nd command on the list (show int..) and regexoutput2 to verify if my regex pattern is working.. and verified that is working"

output is correct for show version -> Version: 12.4(25b)

having issue running multiple command and regex print.

Thank you
>>> import re
>>> help(re.search)
Help on function search in module re:

search(pattern, string, flags=0)
    Scan through string looking for a match to the pattern, returning
    a Match object, or None if no match was found.
If the regex doesn't match, it returns None, which is what your error message says you have (AttributeError: 'NoneType' object has no attribute 'group'). So check if there's a match, before trying to get groups of out of the match object.

Something like this:
# SEARCH VERSION
        regexoutput = re.search('Version \d{2}\.......',show)
        if regexoutput:
            print ("\nVersion:",re.sub("Version ", "", str(regexoutput.group(0))))
(Jan-03-2019, 05:29 PM)nilamo Wrote: [ -> ]
>>> import re
>>> help(re.search)
Help on function search in module re:

search(pattern, string, flags=0)
    Scan through string looking for a match to the pattern, returning
    a Match object, or None if no match was found.
If the regex doesn't match, it returns None, which is what your error message says you have (AttributeError: 'NoneType' object has no attribute 'group'). So check if there's a match, before trying to get groups of out of the match object.

Something like this:
# SEARCH VERSION
        regexoutput = re.search('Version \d{2}\.......',show)
        if regexoutput:
            print ("\nVersion:",re.sub("Version ", "", str(regexoutput.group(0))))


Thanks for sharing. I have added the the search version code that you've shared. But I would like to ask.. what if I have more than 1 regex to search and to print? is it ok to add multiple If statement?

example:
        regexoutput2 = re.search('\d\d\.\d{3}\.\d{3}\.\d',show)
        regexoutput3 = re.search('hostname [a-z]\w*\.[a-z]\w*',show)
        regexoutput = re.search('Version \d{2}\.......',show)   
        if regexoutput2:
            print ("MGMT IP:", str(regexoutput2.group(0)))
        if regexoutput3:
            print ("Hostname:",re.sub("Hostname ", "", str(regexoutput3.group(0))))
        if regexoutput:
            print ("Version:",re.sub("Version ", "",regexoutput.group(0)))
Yep!
(Jan-03-2019, 11:04 PM)nilamo Wrote: [ -> ]Yep!

Thanks for your man,

But I would to ask if the first code that you've given (help re.search....) is for putting an out none?
How can I use regex in if statement.. if there no match i'll display none?

Ex.
if regexoutput6:
print ("License:", str(regexoutput6.group(0)))
else:
print("Licence: none")

Is this the correct way? Thanks
What happened when you tried it?
(Jan-04-2019, 03:48 PM)nilamo Wrote: [ -> ]What happened when you tried it?

Yes, also replaced on to in says invalid syntax.
Help on function search in module re:

Help in function search in module re:
after that im having invalid syntax on search.
If you're getting an error, please share the entire error. The description is not helpful, since that doesn't show what line the error was on or what the call stack was.
Its a syntax error: invalid syntax using search on this code.

help(re.search)
Help in function [b]search[/b] in module re:
search(pattern, string, flags=0)
    Scan through string looking for a match to the pattern, returning
    a Match object, or None if no match was found.
with this error im not able to run it completely
That's not a syntax error, or an error of any kind. That's help text from the interactive prompt. If you're getting an error, please share the entire error.
Pages: 1 2