我需要一些python脚本的帮助.我需要在dhcpd文件中搜索主机entires,它们的MAC和IP,并将它打印在一行中.我能够找到主机名和IP地址,但无法弄清楚如何从if语句中获取变量以放入一行.有任何建议,代码如下:
#!/usr/bin/python
import sys
import re
#check for arguments
if len(sys.argv) > 1:
print "usage: no arguments required"
sys.exit()
else:
dhcp_file = open("/etc/dhcp/dhcpd.conf","r")
for line in dhcp_file:
if re.search(r'\bhost\b',line):
split = re.split(r'\s+', line)
print split[1]
if re.search(r'\bhardware ethernet\b',line):
ip = re.split(r'\s+',line)
print ip[2]
dhcp_file.close()
Run Code Online (Sandbox Code Playgroud)