Python Forum
Python Regex multiple search into separate variable?
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Regex multiple search into separate variable?
#1
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
Reply


Messages In This Thread
Python Regex multiple search into separate variable? - by searching1 - Jan-03-2019, 04:45 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  how to split socket data onto multiple clients for separate processing ConcealedFox70 0 2,030 Jan-11-2022, 08:26 PM
Last Post: ConcealedFox70

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020