#!/usr/bin/env python # coding=utf-8 # nohup curl -s https://internal.up-gram.com/shell/ipv6_update.xml |python xxxx >/dev/null 2>&1 & from xml.etree.ElementTree import ElementTree, Element import shlex import sys def bash(cmd): ''' 执行执行bash命令 ''' return shlex.os.system(cmd) def read_string(content): root = ElementTree.fromstring(content) def read_xml(in_path): ''' 读取并解析xml文件 in_path: xml路径 return: ElementTree ''' tree = ElementTree() tree.parse(in_path) return tree def write_xml(tree, out_path): ''' 将xml文件写出 tree: xml树 out_path: 写出路径 ''' tree.write(out_path, encoding="utf-8") def ipv6_config(ipv6): listen_ip =server['listen_ip'] if ipv6 not in listen_ip: server['listen_ip'] = '{0}|{1}'.format(listen_ip,ipv6) public_ip = server['public_ip'] if '|' in public_ip and ipv6 not in public_ip: public_ip_list = public_ip.split('|') public_ip_list[0] = '{0}#{1}'.format(public_ip_list[0],ipv6) server['public_ip'] = '#'.join(public_ip_list) print(public_ip_list) elif '#' in public_ip and ipv6 not in public_ip: public_ip_list = public_ip.split('#') public_ip_list[0] = '{0}#{1}'.format(public_ip_list[0],ipv6) server['public_ip'] = '#'.join(public_ip_list) print(public_ip_list) if __name__ == '__main__': ipv6 = sys.argv[1] tree = read_xml('/etc/dingtone/xedge-config.xml') root = tree.getroot() server = root.find('server').attrib ipv6_config(ipv6) bash('mv /etc/dingtone/xedge-config.xml /etc/dingtone/xedge-config.xml_bak') write_xml(tree,'/etc/dingtone/xedge-config.xml') # 写入配置 bash("sed -i 's/ldSkyVPN-WebServer-995438515.us-west-1.elb.amazonaws.com/webserver.skyvpnapi.com/g' /etc/dingtone/xedge-config.xml") bash('sed -i "s/0.0.0.0/0::0/g" /etc/dingtone/xedge-config.xml') bash('/root/restartvpn.sh')