实现目标:把key-value配置文件解析,并根据解析内容执行一定的命令操作
(注意:配置文件可能是key=value或者key=value1 value2.···的形式)
V1.0代码如下(后续继续优化):
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
This module is *** init config from aliyun os.config
"""
import os
import shutil
import base64
#run cmd
def run_cmd(args):
p = os.popen(args)
print p.read()
#read os.conf
def read_conf():
filename = 'os.conf'
conf = {}
if os.path.exists(filename):
with open(filename, 'r') as f:
for line in f.readlines():
if line == "\n":
break
kv = line.strip('\n').split("=")
conf[kv[0]] = kv[1]
else:
message = "Sorry, I cannot find the file "
print message, filename
return conf
#initialize devtname
def init_hostname(conf):
if conf.has_key('hostname'):
#config vGate cmd
set_name_cmd = '%s%s' %('system devname set ', conf['hostname'])
#run vGate cmd
run_cmd(set_name_cmd)
#modify password
def init_password(conf):
if conf.has_key('password'):
enc = conf['password']
basedec_password = base64.b64decode(enc + '=' * (-len(enc) % 4))
dec_password = basedec_password.strip('\n')
modify_password_cmd = '%s%s' %('admin inside-admin modify index-key name index-value superman passwd-type plain passwd ', dec_password)
run_cmd(modify_password_cmd)
#initialize DNS
def init_dns(conf):
if conf.has_key('dns_nameserver'):
str_dns = conf['dns_nameserver'].strip('"')
i = 0
tmp = []
tmp.append("network dns set")
for str_slice in str_dns.split(" "):
i += 1
str_dns_num = "dns" + str(i)
tmp.append(str_dns_num)
tmp.append(str_slice)
dns_cmd = " ".join(tmp)
run_cmd(dns_cmd)
#initialize eth* default max network card is 10
def init_eth(conf):
for eth_count in range(0, 11):
#define eth num like eth0 eth1 eth2...
eth_num = '%s%s' %('eth', str(eth_count))
eth_ip = '%s%s' %(eth_num, '_ip_addr')
if conf.has_key(eth_ip):
clean_cmd = "{0} {1} {2}".format('network interface', eth_num, 'ip clean')
run_cmd(clean_cmd)
eth_netmask = '%s%s' %(eth_num, '_netmask')
if conf.has_key(eth_netmask):
mask_cmd = "{0} {1} {2} {3} {4} {5}".format('network interface', eth_num, 'ip add', conf[eth_ip],'mask', conf[eth_netmask])
run_cmd(mask_cmd)
else:
ip_cmd = "{0} {1} {2} {3}".format('network interface', eth_num, 'ip add', conf[eth_ip])
run_cmd(ip_cmd)
eth_mac = '%s%s' %(eth_num, '_mac_addr')
if conf.has_key(eth_mac):
mac_cmd = "{0} {1} {2} {3}".format('network interface', eth_num, 'mac-address', conf[eth_mac])
run_cmd(mac_cmd)
eth_route = '%s%s' %(eth_num, '_route')
if conf.has_key(eth_route):
str_eth_num = "{0}{1}{2}".format('"', eth_num, '"')
str_route = conf[eth_route].strip('"')
for route_slice in str_route.split(";"):
route_dst, route_gw = route_slice.split(" ")
route_cmd = "{0} {1} {2} {3} {4} {5}".format('network route add dst', route_dst, 'gw', route_gw, 'dev', str_eth_num)
run_cmd(route_cmd)
#save system config
def cmd_save():
run_cmd('save')
#backup copy os.conf file
def backup_file():
filename = 'os.conf'
dst_dir = "/se_mnt/init_conf"
is_exists_dir = os.path.isdir(dst_dir)
if not is_exists_dir:
os.mkdir(dst_dir)
if os.path.exists(filename):
shutil.move("os.conf", "/se_mnt/init_conf/aliyun.conf")
def main():
#read conf file
net_conf = read_conf()
#init hostname
init_hostname(net_conf)
#init password
init_password(net_conf)
#init dns
init_dns(net_conf)
#init eth
init_eth(net_conf)
#cmd save
cmd_save()
#backup file
backup_file()
if __name__ == '__main__':
main()
配置文件内容参考:
hostname=abc
password=123
eth0_ip_addr=10.1.24.3
eth0_mac_addr=00:8c:fa:5e:14:23
eth0_netmask=255.255.255.0
eth0_gateway=10.1.24.1
eth0_route="10.0.0.0/8 10.171.254.1;172.16.0.0/12 10.171.254.1"
eth1_ip_addr=42.120.74.105
eth1_mac_addr=00:8c:fa:5e:14:24
eth1_netmask=255.255.255.0
eth1_gateway=42.10.74.1
eth1_route="0.0.0.0/0 42.20.74.1"
dns_nameserver="7.7.7.7 8.8.8.8"