【发布时间】:2017-01-08 05:48:24
【问题描述】:
我的/etc/hosts 文件中有以下行
我要做的是,在/etc/hosts 文件中找到gs2 行并获取当前IP 地址。这就是我所拥有的,但它没有找到 DNS 或返回 IP 地址。它告诉我我当前的 IP 地址是“无”。
try:
with open('/etc/hosts', 'r') as f:
for line in f:
host_ip = re.findall(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b.+(?=gs2)", line)
if host_ip:
current_ip = host_ip[0].strip()
else:
current_ip = 'None'
except:
current_ip = 'Unknown'
c.execute('INTERT INTO status VALUES(?,?,?,?,?,?)',
('Current Configured IP', current_ip))
不确定是什么问题。任何帮助将不胜感激。
【问题讨论】:
标签: python ubuntu-server