我有一个配置网络接口的功能。 它向用户请求新的IP地址/netmask/gateway/和DNS,并将新值写入/etc/network/interfaces文件: 在写入文件之前,我有另一个名为“vaildIP”的函数,它检查用户输入的IP地址是否正确,然后将新值传递给interfaces文件。你知道吗
address = vaildIP ('Address[current value is <no-value>:')
netmask = vaildIP('Netmask[current value is < no - value >]:')
gateway = vaildIP('Gateway[current value is < no - value >]:')
dns = raw_input('DNS servers [current value is <no-value>]:') or "8.8.8.8"
所以我的问题是如何读取/etc/network/interfaces文件并将当前值添加到另一个没有值的变量中
例如,它应该是:
现在是:没有价值
address = vaildIP ('Address[current value is <no-value>:')
我希望是:
address = vaildIP ('Address[current value is 192.168.1.34:') * or any other ip
到目前为止,我设法打开文件并打印值,但我无法将值传递到其no-value
所在的位置:
with open("/etc/network/interfaces", "r+") as file:
file_content = file.readlines()
for line in file_content:
if line.startswith("address") or line.startswith("netmask") or line.startswith("gateway") or line.startswith("dns-nameservers"):
print 'current values are -- {lineip}'.format(lineip = line)
有人能帮我弄清楚吗
相关问题 更多 >
编程相关推荐