gpt4 book ai didi

Python 从 telnetlib 输出中获取 IP 地址

转载 作者:太空宇宙 更新时间:2023-11-03 14:06:25 36 4
gpt4 key购买 nike

我使用 telnetlib 从我的路由器中提取路由信息,并希望使用一种模式提取 WAN IP 地址。 telnet session 的输出在一个变量中,行由\n 分隔。

telnet session 中的内容。

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,它对我来说更容易理解。我给了两个“^”并将 vks 标记为首选(在“我首选”的意义上)。

我最终在“split”cmd 和以下代码中使用了“\r\n”。

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

最佳答案

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)

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

你的代码有问题:

1) re.findall 返回 list ,所以它不能是 None 它可以是空的。所以在 if 条件。

2) re.findall 只返回有组的情况。如果你想要整行,让第一个组不捕获。

关于Python 从 telnetlib 输出中获取 IP 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43246177/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com