python实例: setconfig.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import subprocess
import xml.etree.cElementTree as ET
#from binascii import hexlify, unhexlify

class eGW_config():

    def __init__(self):
        self.tree = ET.ElementTree(file='/home/baicells/eGW/config.xml')
        self.root = self.tree.getroot()
        
            self.firewall_enable = self.getnode('system/firewall/enable')[0]
                self.firewall_on = self.getnode('system/firewall/on')[0]
        self.selinux_enable = self.getnode('system/selinux/enable')[0]
                self.selinux_on = self.getnode('system/selinux/on')[0]
        self.ipsec_service = self.getnode('network/ipsec/service')[0]
                self.ipsec_ul = self.getnode('network/ipsec/ipsec_ul')[0]
                self.ipsec_dl = self.getnode('network/ipsec/ipsec_dl')[0]
        self.ports_name = self.getnode('network/ports/port/name')
                self.ports_addr = self.getnode('network/ports/port/addr')
                self.ports_num = len(self.ports_name)
        self.routes_dst = self.getnode('network/routes/route/dst')
                self.routes_gw = self.getnode('network/routes/route/gw')
                self.routes_num = len(self.routes_dst)
        self.iptables_enable = self.getnode('network/iptables/iptable/enable')
                self.iptables_forward_port = self.getnode('network/iptables/iptable/forward_port')
                self.iptables_sourceip = self.getnode('network/iptabels/iptable/sourceip')
                self.iptables_num = len(self.iptables_enable)
        self.plmn = self.getnode('eGW/plmn')[0]
                self.plmn = self.initPLMN(self.plmn)        
        self.charge_local_ip = self.getnode('eGW/charge_service/local_ip')[0]
                self.charge_remote_ip = self.getnode('eGW/charge_service/remote_ip')[0]
                self.charge_num = self.getnode('eGW/charge_service/charge_num')[0]
        self.enb_index = self.getnode('eGW/eNBs/eNB/index')
                self.enb_id = self.getnode('eGW/eNBs/eNB/id')
                self.enb_tac = self.getnode('eGW/eNBs/eNB/tac')
                self.enb_local_c = self.getnode('eGW/eNBs/eNB/local_c')
                self.enb_remote_c = self.getnode('eGW/eNBs/eNB/remote_c')
                self.enb_link_enable = self.getnode('eGW/eNBs/eNB/link_enable')
                self.enb_gtpu = self.getnode('eGW/eNBs/eNB/gtpu')
                self.enb_num = len(self.enb_index)
        self.epc_local_c = self.getnode('eGW/epcs/epc/local_c')
                self.epc_gtpu = self.getnode('eGW/epcs/epc/gtpu')
                self.epc_num = len(self.epc_local_c)
        self.forwarding_policy = self.getnode('eGW/forwarding_policys/forwarding_policy')
        self.forwarding_policy_num = len(self.forwarding_policy)
        self.config_path = '/home/baicells/eGW/config.txt'

    #»ñÈ¡½ÚµãÐÅÏ¢
    def getnode(self,xpath):
        self.node = []
        for elem in self.tree.iterfind(xpath):
            self.node.append(elem.text)
        return self.node

    #Ö´ÐÐshellÃüÁî
    def sendcmd(self,cmd):
        self.cmd = cmd
            self.resp = []
            self.p = subprocess.Popen(self.cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
            for line in self.p.stdout.readlines():
                    self.resp.append(line)
            return self.resp

    def initPLMN(self,plmn):
        self.plmn = plmn
            if len(self.plmn) == 5:
                    self.mcc = self.plmn[0:3]
                    self.mnc = self.plmn[3:5]
                    self.plmn = ''.join([self.mcc[1],self.mcc[0],'f',self.mcc[2],self.mnc[1],self.mnc[0]])
        elif len(self.plmn) == 6:
            self.mcc = self.plmn[0:3]
                        self.mnc = self.plmn[3:6]
                        self.plmn = ''.join([self.mcc[1],self.mnc[0],self.mnc[0],self.mcc[2],self.mnc[2],self.mnc[1]])
        return self.plmn
            
    
    def install_gtp_module(self):
        self.sendcmd('insmod /home/baicells/eGW/gtp-relay.ko')

    def config_firewall(self):

        if self.firewall_enable=='0':
            self.sendcmd('systemctl disable firewalld.service')
        elif self.firewall_enable=='1':
            self.sendcmd('systemctl enable firewalld.service')
        else:
            print "ERROR!"
        if self.firewall_on=='0':
            self.sendcmd('systemctl stop firewalld.service')
        elif self.firewall_on=='1':
            self.sendcmd('systemctl start firewalld.service')
        else:
            print "ERROR!"
    
    def config_selinux(self):

        if self.selinux_enable=='0':
            self.sendcmd('sed -i "s/SELINUX=enforcing/SELINUX=disabled/g"  /etc/selinux/config')
        elif self.selinux_enable=='1':
            self.sendcmd('sed -i "s/SELINUX=disabled/SELINUX=enforcing/g"  /etc/selinux/config')
        else:
            print "ERROR!"
        if self.selinux_on=='0':
            self.sendcmd('setenforce 0')
        elif self.selinux_on=='1':
            self.sendcmd('setenforce 1')
        else:
            print "ERROR!"
    
    def config_ipsec(self):

        if self.ipsec_service=='0':
            self.sendcmd('ipsec stop')
        elif self.ipsec_service=='1':
            self.sendcmd('ipsec start')
        else:
            print "ERROR!"

        if self.ipsec_ul=='0':
            self.sendcmd('echo 0 > /sys/module/gtp_relay/parameters/gtp_ipsec_ul')
        elif self.ipsec_ul=='1':
            self.sendcmd('echo 1 > /sys/module/gtp_relay/parameters/gtp_ipsec_ul')
        else:
            print "ERROR!"
        if self.ipsec_dl=='0':
                self.sendcmd('echo 0 > /sys/module/gtp_relay/parameters/gtp_ipsec_dl')
        elif self.ipsec_dl=='1':
                self.sendcmd('echo 1 > /sys/module/gtp_relay/parameters/gtp_ipsec_dl')
        else:
                print "ERROR!"
    
    def config_port(self):

        self.sendcmd('systemctl restart network')
        for i in range(self.ports_num):
            self.sendcmd('ifconfig %s %s' %(self.ports_name[i],self.ports_addr[i]))
            if self.ports_name[i]=='gtp1_1':
                self.gtp_a = self.ports_addr[i].split('.')[0]   
                self.gtp_b = self.ports_addr[i].split('.')[1]
                self.var = int(self.gtp_a)*256+int(self.gtp_b)
                self.sendcmd('echo %s > /sys/module/gtp_relay/parameters/gtp_lip' %self.var)
    
    def config_route(self):

        for i in range(self.routes_num):
            self.sendcmd('route add -net %s gw %s' %(self.routes_dst[i],self.routes_gw[i]))
    
    def config_iptable(self):

        for i in range(self.iptables_num):
            if self.iptables_enable[i]=='1':
                self.sendcmd('iptables -t nat -A POSTROUTING -s %s.%s.0.0/16 -o %s -j SNAT --to-source %s' %(self.gtp_a,self.gtp_b,self.iptables_forward_port[i],self.iptables_sourceip[i]))
            else:
                print "PASS"
    
    def config_egw(self):

        self.sendcmd('echo ./lccmd set_gw_plmnid %s > %s' %(self.plmn,self.config_path))
        if self.charge_local_ip is not None and self.charge_remote_ip is not None:
            if self.charge_num == None:
                self.charge_num = '1'
            self.sendcmd('echo ./lccmd set_charge_service %s %s %s >> %s' %(self.charge_local_ip,self.charge_num,self.charge_remote_ip,self.config_path))

        for i in range(self.forwarding_policy_num):
            if i > 4:
                print "Max num is 4!"
            elif i >0:  
                self.sendcmd('echo ./lccmd set_local_forwarding_policy add %s >> %s' %(self.forwarding_policy[i],self.config_path))
            else:
                print "forwarding_policy not config!"

        for i in range(self.enb_num):
            
            self.sendcmd('echo ./lccmd add_gwenb %s %s %s >> %s' %(self.enb_index[i],self.enb_id[i],self.enb_tac[i],self.config_path))
            self.sendcmd('echo ./lccmd set_gwenb_link %s 1 %s %s >> %s' %(self.enb_index[i],self.enb_local_c[i],self.enb_remote_c[i],self.config_path))
            if self.enb_link_enable[i]=='1':
                self.sendcmd('echo ./lccmd set_gwenb_link_enable %s 1 enable >> %s' %(self.enb_index[i],self.config_path))
            elif self.enb_link_enable[i]=='0':
                self.sendcmd('echo ./lccmd set_gwenb_link_enable %s 1 disable >> %s' %(self.enb_index[i],self.config_path))
            else:
                print "ERROR!"
            self.sendcmd('echo ./lccmd set_gtp_ip uplink add %s >> %s' %(self.enb_gtpu[i],self.config_path))


        for i in range(self.epc_num):
            self.sendcmd('echo ./lccmd set_enb_access_ip add %s >> %s' %(self.epc_local_c[i],self.config_path))
            self.sendcmd('echo ./lccmd set_gtp_ip downlink add %s >> %s' %(self.epc_gtpu[i],self.config_path))

def main():

    myeGW = eGW_config()
    myeGW.config_firewall()
    myeGW.config_selinux()
    myeGW.config_ipsec()
    myeGW.config_port()
    myeGW.config_route()
    myeGW.config_iptable()
    myeGW.install_gtp_module()
    myeGW.config_egw()


if __name__ == '__main__':

    main()


你可能感兴趣的:(python实例: setconfig.py)