试图获取端口号(正则表达式)

Trying to get port number (regex)

我通过 telnet (telnetlib) 从 PDU 获得了输出,我正在尝试从每个服务器名称的输出中获取端口号。 我设法使用 (?!E000)[A-Z]([^\sPDU]+)\d 获取了端口名称。 我很乐意帮助您使用循环和正则表达式获取每个服务器的端口号。 到目前为止,我有这个正则表达式用于输出端口号 [^E000][^A-Z]( \d)|[^E000][^A-Z](\d\d)

这是我目前得到的:

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)

输出为:

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

这是一个使用 python 2.7 的服务器,我尝试使用多个正则表达式但找不到正确的。

我需要在所有端口的一个变量中得到“2”,在另一个变量中得到 "server01"。

您可以尝试使用以下代码:

string= "2: server01: On"
print re.split(r":\s+", string)  #['2', 'server01', 'On']

:     # Splits on it
\s+   # Match one or more whitespace characters

了解有关正则表达式的更多信息:

https://www.regular-expressions.info/

我使用了 PySaad 的正则表达式,但使用了 "re.sub"。
servername = re.sub(r"\s+", "", servername)
请参阅代码中的 ==>。

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)