RHEL7主机有五块网卡,名为eth0/eth1/eth2/eth3/eth4, 为四块网卡配置IP地址。
import re
#在网卡配置文件目录下,生成IP地址的配置文件,并写入配置信息
def mk_ip(ip_num, ip, fname):
conf = '''DEVICE="eth%s"
NAME="eth%s"
ONBOOT="yes"
NM_CONTROLLED="no"
TYPE="Ethernet"
BOOTPROTO="none"
IPADDR="%s"
NETMASK="255.255.255.0"
PREFIX=24
''' % (ip_num, ip_num, ip)
with open(fname, 'w') as f1:
f1.write(conf)
#检测IP地址的格式,正确返回True,错误则返回False
def check_ip(ip):
re_ip = '(\d{1,3}\.){3}\d{1,3}$'
cmp = re.compile(re_ip)
if cmp.match(ip):
return True
else:
return False
#菜单界面,选择配置哪张网卡
def show_menu():
#cmds = dict.fromkeys(['0', '1', '2', '3', '4'], check_ip)
menu