#!/usr/bin/python # -*- coding: UTF-8 -*- import MySQLdb import random #db_ip = "localhost" db_ip = "192.168.134.84" # 打开数据库连接 db = MySQLdb.connect(db_ip, "root", "123456", "nosdb", charset='utf8' ) # 使用cursor()方法获取操作游标 cursor = db.cursor() # 使用execute方法执行SQL语句 cursor.execute("select * from masternos order by id DESC limit 1 ") # 使用 fetchone() 方法获取一条数据 data = cursor.fetchone() # 关闭数据库连接 db.close() #print " id : %d " % data[0] #print "type: %d" % data[1] #print "nos:%d" % data[3] #print "mark:%s" % data[2] master_nos = 1; #the default master nos is nos1 if data[1] == 0: # give the nos by admin master_nos = data[2] elif data[1] == 1: #give the nos with random master_nos = random.randint(1,4) #print "master_nos random:",master_nos #根据主执行体确定vlanID if master_nos == 1: vlanID = 200 elif master_nos == 2: vlanID = 300 elif master_nos == 3: vlanID = 400 elif master_nos == 4: vlanID = 500 else: vlanID = 200 fileHandle = open ('nos.conf', 'w' ) fileHandle.write (str(master_nos)) fileHandle.close() fileHandle = open ('nos_vlan.conf', 'w' ) fileHandle.write (str(vlanID)) fileHandle.close()