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()


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,684评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,143评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,214评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,788评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,796评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,665评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,027评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,679评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 41,346评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,664评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,766评论 1 331
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,412评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,015评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,974评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,203评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,073评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,501评论 2 343

推荐阅读更多精彩内容