Ad

Trying To Get Port Number (regex)

- 1 answer

I got an output over telnet (telnetlib) from a PDU and I am trying to get the port number from the output for each servername. I managed to get the port names by using (?!E000)[A-Z]([^\sPDU]+)\d. I'll be happy for assistance getting the port number for each server using a loop and regex. I've got this regex so far for output the port number [^E000][^A-Z]( \d)|[^E000][^A-Z](\d\d)

This is what I got so far:

ports = "1","2","3","4"
PDUS = "192.168.10.12","192.168.10.11"
password = 'password'
user = 'apc'

  for PDU in PDUS:
            for port in ports:
                print "Connecting to"+" "+PDU+" "+"Telnet"
                tn = telnetlib.Telnet(PDU)

                print "Logging in"
                tn.read_until('User Name :')
                tn.write(user + b'\r\n')
                if password:
                    tn.read_until('Password  :')
                    tn.write(password + b'\r\n')

                    print "Checking port Status"
                    tn.write('olStatus All\n')
                    tn.write('exit\n')

                print (output)
                output = (tn.read_all())
                servername = re.search(r'(?!E000)[A-Z]([^\sPDU]+)\d', output, re.MULTILINE)
                server = re.search(r'server01|server02|server03', output)
                if servername:
                    print servername.group(0)
                    print server.group(0)

The output is:

Schneider Electric                      Network Management Card AOS      v6.4.4
(c) Copyright 2016 All Rights Reserved  RPDU 2g APP                      v6.4.4
-------------------------------------------------------------------------------
Name      : PDU2                                      Date : 08/11/2019
Contact   : Unknown                                   Time : 03:17:18
Location  : Unknown                                   User : Super User
Up Time   : 4 Days 0 Hours 7 Minutes                  Stat : P+ N4+ N6+ A+


Type ? for command listing
Use tcpip command for IP address(-i), subnet(-s), and gateway(-g)

apc>olStatus All  
E000: Success  
 1: Empty: On  
 2: server01: On  
 3: server02: On  
 4: server03: On  
 5: server04: On  
 6: server05: On  
 7: server06: On  
 8: server07: On  
 9: Empty: On  
10: server08: On  
11: server09: On  
12: server10: On  
13: server11: On  
14: server12: On  
15: server13: On  
16: server14: On  
17: Empty: On  
18: server15: On  
19: server16: On  
20: server17: On  
21: server18: On  
22: server19: On  
23: serverspare01: On  
24: serverspare02: On  

apc>exit

This is a server using python 2.7 and I trying using multiple regex but could not find the correct one.

I need to get "2" in one variable and "server01" in another variable for all ports.

Ad

Answer

I used the regex from PySaad, but with "re.sub".
servername = re.sub(r"\s+", "", servername)
See ==> in the code.

ports = "1","2","3","4"
PDUS = "192.168.10.12","192.168.10.11"
password = 'password'
user = 'apc'

for PDU in PDUS:
        for port in ports:
            print "Connecting to"+" "+PDU+" "+"Telnet"
            tn = telnetlib.Telnet(PDU)

            print "Logging in"
            tn.read_until('User Name :')
            tn.write(user + b'\r\n')
            if password:
                tn.read_until('Password  :')
                tn.write(password + b'\r\n')

                print "Checking port Status"
                tn.write('olStatus All\n')
                tn.write('exit\n')

            print (output)
            output = (tn.read_all())
            servername = re.search(r'(?!E000)[A-Z]([^\sPDU]+)\d', output, re.MULTILINE)
        ==> servername = re.sub(r"\s+", "", servername
            server = re.search(r'server01|server02|server03', output)
            if servername:
                print servername.group(0)
                print server.group(0)
Ad
source: stackoverflow.com
Ad