#!/usr/bin/env python # coding=utf-8 from xml.etree.ElementTree import ElementTree, Element import shlex import sys import datetime # wget http://internal.up-gram.com/shell/public_ip_update.xml.py $1 $2 $3 # python <(curl -sL http://internal.up-gram.com/shell/public_ip_update.xml.py) {0} {1} {2}".format(lan,ip,floating_ip) 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") if __name__ == '__main__': lan = sys.argv[1] ip = sys.argv[2] floating_ip = sys.argv[3] tree = read_xml('/etc/dingtone/xedge-config.xml') root = tree.getroot() server = root.find('server').attrib server['listen_ip'] = "{0}|{1}".format(lan,ip) server['public_ip'] = "{0}|{1}".format(ip,floating_ip) print(server) cmd = "mv /etc/dingtone/xedge-config.xml /etc/dingtone/xedge-config.xml_{}bak".format(datetime.datetime.now().strftime('%Y%m%d%H')) bash(cmd) 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('/root/restartvpn.sh')