Python从telnetlib输出

时间:2017-04-06 05:10:36

标签: python regex

使用telnetlib我从路由器中提取路由信息,并希望使用模式提取WAN IP地址。 telnet会话的输出位于变量中,行以\ n分隔。

来自telnet会话的内容。

l= tn.read_all()
>>> l
'\r\nip -f inet addr\r\nexit\r\nadmin4asus@RT-AC68U:/tmp/home/root# ip -f inet addr\r\n1: lo: <LOOPBACK,MULTICAST,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN \r\n    inet 127.0.0.1/8 brd 127.255.255.255 scope host lo\r\n2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000\r\n    inet 24.6.29.214/21 brd 24.6.31.255 scope global eth0\r\n7: br0: <BROADCAST,MULTICAST,ALLMULTI,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN \r\n    inet 192.168.11.1/24 brd 192.168.11.255 scope global br0\r\n8: tun21: <POINTOPOINT,MULTICAST,NOARP,PROMISC,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 100\r\n    inet 10.8.0.1 peer 10.8.0.2/32 scope global tun21\r\nadmin4asus@RT-AC68U:/tmp/home/root# exit\r\n'
>>> 

现在我的代码。

l= tn.read_all()
for line in l.split('\n'):
    match= re.findall( r'([0-9]+(?:\.[0-9]+){3}).*scope global eth0', line)
    if match is not None:
        print('Found ' + line)

我原本期望匹配的一行打印。

Found     inet 24.6.29.214/21 brd 24.6.31.255 scope global eth0

但我随处可见。

Found 
Found ip -f inet addr
Found exit
Found admin4asus@RT-AC68U:/tmp/home/root# ip -f inet addr
Found 1: lo: <LOOPBACK,MULTICAST,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
Found     inet 127.0.0.1/8 brd 127.255.255.255 scope host lo
Found 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
Found     inet 24.6.29.214/21 brd 24.6.31.255 scope global eth0
Found 7: br0: <BROADCAST,MULTICAST,ALLMULTI,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN 
Found     inet 192.168.11.1/24 brd 192.168.11.255 scope global br0
Found 8: tun21: <POINTOPOINT,MULTICAST,NOARP,PROMISC,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 100
Found     inet 10.8.0.1 peer 10.8.0.2/32 scope global tun21
Found admin4asus@RT-AC68U:/tmp/home/root# exit
Found 

我无法弄清楚我的代码失败的原因。如果专家有更好的方法(有解释)那就太棒了。

修改:

Jan的答案肯定更加pythonic,但我对python缺乏了解使我更喜欢vks,这对我来说更容易理解。我给了他们一个&#39; ^&#39;并且将vks标记为首选(在某种意义上&#39;我更喜欢&#39;)。

我最终使用了&#39; \ r \ n&#39;在&#39;分裂&#39; cmd和以下代码。

def get_asus_wan_ip():
    "Gets WAN IP from ASUS router"

    import telnetlib
    import re

    ASUS_IP=   '192.168.1.1'
    ASUS_USER= 'xxxxxxxx'
    ASUS_PASS= 'yyyyyyyy'
    tn = telnetlib.Telnet(ASUS_IP)

    tn.read_until("login: ")
    tn.write(ASUS_USER + "\n")
    tn.read_until("Password: ")
    tn.write(ASUS_PASS + "\n")
    tn.write("ifconfig eth0\n")
    tn.write("exit\n")
    l= tn.read_all()
    for line in l.split('\r\n'):
        match= re.findall( r'^\s+inet addr:([0-9]+(?:\.[0-9]+){3}).*', line)
        if match:
            break

    wan_ip= match[0]
    return wan_ip

2 个答案:

答案 0 :(得分:3)

import re
x="""'\r\nip -f inet addr\r\nexit\r\nadmin4asus@RT-AC68U:/tmp/home/root# ip -f inet addr\r\n1: lo: <LOOPBACK,MULTICAST,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN \r\n    inet 127.0.0.1/8 brd 127.255.255.255 scope host lo\r\n2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000\r\n    inet 24.6.29.214/21 brd 24.6.31.255 scope global eth0\r\n7: br0: <BROADCAST,MULTICAST,ALLMULTI,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN \r\n    inet 192.168.11.1/24 brd 192.168.11.255 scope global br0\r\n8: tun21: <POINTOPOINT,MULTICAST,NOARP,PROMISC,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 100\r\n    inet 10.8.0.1 peer 10.8.0.2/32 scope global tun21\r\nadmin4asus@RT-AC68U:/tmp/home/root# exit\r\n'"""
for line in x.split('\n'):
    match= re.findall(r'(?:[0-9]+(?:\.[0-9]+){3}).*scope global eth0', line)
    if match:
        print('Found ' + line)

输出:Found inet 24.6.29.214/21 brd 24.6.31.255 scope global eth0

代码问题:

1)re.findall返回list,因此它不能None它可以为空。所以在if条件下使用它。

2)re.findall只返回组中的组。如果你想要整行,那就让第一组不被捕获。

答案 1 :(得分:2)

您可以通过列表理解大大缩小代码:

import re

string = """
1: lo: <LOOPBACK,MULTICAST,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    inet 127.0.0.1/8 brd 127.255.255.255 scope host lo
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
    inet 24.6.29.214/21 brd 24.6.31.255 scope global eth0
7: br0: <BROADCAST,MULTICAST,ALLMULTI,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN 
    inet 192.168.11.1/24 brd 192.168.11.255 scope global br0
8: tun21: <POINTOPOINT,MULTICAST,NOARP,PROMISC,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 100
    inet 10.8.0.1 peer 10.8.0.2/32 scope global tun21
"""

rx = re.compile(r'\b(\d+(?:\.\d+){3})\b.*scope global eth0')
matches = [line
            for line in string.split("\n")
            for match in [rx.search(line)]
            if match]
print(matches)
# ['    inet 24.6.29.214/21 brd 24.6.31.255 scope global eth0']

或者如果您更喜欢filter()lambda()

matches = list(filter(lambda x: rx.search(x), string.split("\n")))
print(matches)