动漫系统管理脚本_第1张图片


动漫系统管理脚本_第2张图片

动漫系统管理脚本_第3张图片

动漫系统管理脚本_第4张图片

动漫系统管理脚本_第5张图片

mysql模块
#!/usr/bin/python
#^_^coding:utf-8^_^
'''
ATUH:SunFx
MAIL:[email protected]
QQ:598824458
DATE:2015/03/18
'''

import MySQLdb


def mysqlc(select):

    'select host info'
    redata = []
    conn=MySQLdb.connect(host="xxxx",user="root",passwd="xxxx",db="xx",charset="utf8")  
    cursor = conn.cursor() 
    if not select.find('TH'):
       jg = '%s%%' % select 
       ip = ''
       select = cursor.execute("select * from host where pname like '%s' or pip like '%s'" % (jg,ip)) 
    else:
       jg = ''
       ip = select 
       select = cursor.execute("select * from host where pname like '%s' or pip like '%s'" % (jg,ip)) 
    for line in cursor.fetchall():
       redata.append(line)
    return redata
配置文件
#!/usr/bin/python
#^_^coding:utf-8^_^
'''
auth:sunfx
email:[email protected]
qq:598824458
date:2015/03/18
'''
import os

def Makecf(ip,name):

    'make network and hostname file'
    conf_path = '%s/conf' % os.getcwd()

    ndict = {
    'ip':ip,
    'name':name,
    'netmask':'255.255.0.0',
    'ONBOOT':'yes',
    'conf_path':conf_path
    }

    nbr = 'DEVICE=br0\nTYPE=Bridge\nBOOTPROTO=static\nIPADDR=\
%(ip)s\nNETMASK=%(netmask)s\nONBOOT=%(ONBOOT)s' % ndict
    neth = 'DEVICE=eth0\nBRIDGE=br0\nBOOTPROTO=static\nONBOOT=yes'
    hostname = 'NETWORKING=yes\nHOSTNAME=%(name)s' % ndict

    with file('ifcfg-br0','w') as f:
        f.write(nbr)
    with file('ifcfg-eth0','w') as f:
        f.write(neth)
    with file('network','w') as f:
        f.write(hostname)
 
    status = os.system('scp ifcfg-br0 ifcfg-eth0 root@%(ip)s:/etc/sysconfig/network-scripts/ >/dev/null' % ndict )
    os.system('scp  network root@%(ip)s:/etc/sysconfig/ >/dev/null' % ndict )
    os.system('scp  %(conf_path)s/hosts root@%(ip)s:/etc/ >/dev/null' % ndict )
    os.system('scp  %(conf_path)s/qemu.conf root@%(ip)s:/etc/libvirt >/dev/null' % ndict )

    rmlist = ['ifcfg-br0','ifcfg-eth0','network']
    [ os.remove(line) for line in rmlist ]

    if status == 0:
       print '\033[32;1mStart %(ip)s network now....\033[0m' % ndict
       sn = os.system('ssh %(ip)s "service network restart" >/dev/null' % ndict)
       if sn == 0:
           print '\033[32;1m%(ip)s Start libvirtd restart.....\033[0m' % ndict
           os.system('ssh %(ip)s "service libvirtd restart" 2>/dev/null 1>/dev/null' % ndict)
           return True
    else:
       print '\033[31;1m%请查看(ip)s 是否开机\033[0m'
       return False
main脚本
#!/usr/bin/python
#^_^coding:utf-8^_^
'''
auth:sunfx
email:[email protected]
qq:598824458
date:2015/03/18
'''
import os
import sys
import tab

'自己编写模块,方便后期进行修改.'
sys.path.append('%s/conf' % os.getcwd())
from disiso import *
from vmanager import *
from conf import Makecf
from mysql import mysqlc
from virsh import manamgekvm

menu = '''
请根据下面的序号去选择你需要的功能:

     (1) 配置物理机和虚拟机底层环境
     (2) 配置kvm虚拟机(内存、cpu、mac)
     (3) 虚拟机管理
     (4) 镜像管理
     (5) 查看日志(开发中....)

输入1,2,3,4 选择相应的功能,'q'退出程序
'''
menu1 = '''
请根据实际需求输入单台IP或输入机柜名称:

   例如:(ip) 172.16.10.1 
        (机柜) TH108

输入b返回上一级
'''
menu2 = '''
请根据实际需求输入下列选项:
    (1) 修改CPU核心数 (默认24核心)
    (2) 修改虚拟机内存 (默认24G内存)
    (3) 修改虚拟机MAC

注意!
   修改完成不会立刻生效 需要把虚拟机关机在开机!
'''

menu3 = '''
请根据实际需求输入下列选项:
    (1) 虚拟机开机
    (2) 虚拟机关机(慎用会强制关闭虚拟机)
    (3) 虚拟机状态查询
输入b返回上一级
'''

menu4 = '''
请根据实际需求输入下列选项:
    (1) 查看镜像
    (2) 分发镜像
    (3) 删除镜像  

输入b返回上一级
'''

print '\033[32;1m\t欢迎使用虚拟机管理脚本\033[0m'
while True:
    print '\033[32;1m%s\033[0m' % menu
    sel_input  =raw_input('>').strip()
    if sel_input == 'q':
        print '程序已经退出....'
        break
    elif sel_input == '1':
       while True:
           print '\033[33;1m%s\033[0m' % menu1
           uinput = raw_input('>')
           if uinput == 'b':
              break
           elif len(mysqlc(uinput)) >1:
              for line in mysqlc(uinput):
                  ip = line[2]
                  name = line[1]
                  Makecf(ip,name)
           else:
              ip = mysqlc(uinput)[0][2]
              name = mysqlc(uinput)[0][1]
              Makecf(ip,name)
              
    elif sel_input == '2':
        while True:
            print '\033[33;1m%s\033[0m' % menu2
            uinput = raw_input('>')
            if uinput == 'b':
                break
            elif uinput == '1':
                cpu = raw_input('请输入修改CPU的核心数(1-24) :').strip()
                while True:
                    print '\033[33;1m%s\033[0m' % menu1
                    uinput1 = raw_input('>')
                    if uinput1 == 'b':
                        break
                    elif len(mysqlc(uinput1)) == 0:
                        print '\033[31;1m请输入正确的选择\033[0m'
                    elif len(mysqlc(uinput1)) >1:
                        for line in mysqlc(uinput1):
                            ip = line[2] 
                            vmac = line[-1]
                            manamgekvm(cpu,vmac,ip)
                    else:
                        ip = mysqlc(uinput1)[0][2]
                        vmac = mysqlc(uinput1)[0][-1]
                        manamgekvm(cpu,'24',vmac,ip)
            elif uinput == '2':
                mem = raw_input('请输入修改内存大小(24、32) :').strip()
                while True:
                    print '\033[33;1m%s\033[0m' % menu1
                    uinput1 = raw_input('>')
                    if uinput1 == 'b':
                        break
                    elif len(mysqlc(uinput1)) == 0:
                        print '\033[31;1m请输入正确的选择\033[0m'
                    elif len(mysqlc(uinput1)) >1:
                        for line in mysqlc(uinput1):
                            ip = line[2] 
                            vmac = line[-1]
                            manamgekvm('24',mem,mac,ip)
                    else:
                        ip = mysqlc(uinput1)[0][2]
                        vmac = mysqlc(uinput1)[0][-1]
                        manamgekvm('24',mem,vmac,ip)
                 
            elif uinput == '3':
                while True:
                    print '\033[33;1m%s\033[0m' % menu1
                    uinput1 = raw_input('>')
                    if uinput1 == 'b':
                        break
                    elif len(mysqlc(uinput1)) == 0:
                        print '\033[31;1m请输入正确的选择\033[0m'
                    elif len(mysqlc(uinput1)) >1:
                        for line in mysqlc(uinput1):
                            ip = line[2] 
                            vmac = line[-1]
                            manamgekvm('24','24',vmac,ip)
                    else:
                        ip = mysqlc(uinput1)[0][2]
                        vmac = mysqlc(uinput1)[0][-1]
                        manamgekvm('24','24',mac,ip)
    elif sel_input == '3':
        while True:
            print '\033[33;1m%s\033[0m' % menu3
            uinput = raw_input('>')
            if uinput == 'b':
                break
            elif uinput == '1':
                while True:
                    print '\033[35;1m%s\033[0m' % menu1
                    uinput1 = raw_input('>')
                    if uinput1 == 'b':
                        break
                    elif len(mysqlc(uinput1)) == 0:
                        print '\033[31;1m请输入正确的选择\033[0m'
                    elif len(mysqlc(uinput1)) >1:
                        for line in mysqlc(uinput1):
                            ip = line[2] 
                            print '%s' % ip
                            os.system(start(ip))
                    else:
                        ip = mysqlc(uinput1)[0][2]
                        os.system(start(ip))
            elif uinput == '2':
                while True:
                    print '\033[35;1m%s\033[0m' % menu1
                    uinput1 = raw_input('>')
                    if uinput1 == 'b':
                        break
                    elif len(mysqlc(uinput1)) == 0:
                        print '\033[31;1m请输入正确的选择\033[0m'
                    elif len(mysqlc(uinput1)) >1:
                        for line in mysqlc(uinput1):
                            ip = line[2] 
                            print '%s' % ip
                            os.system(destroy(ip))
                    else:
                        ip = mysqlc(uinput1)[0][2]
                        os.system(destroy(ip))
            elif uinput == '3':
                while True:
                    print '\033[35;1m%s\033[0m' % menu1
                    uinput1 = raw_input('>')
                    if uinput1 == 'b':
                        break
                    elif len(mysqlc(uinput1)) == 0:
                        print '\033[31;1m请输入正确的选择\033[0m'
                    elif len(mysqlc(uinput1)) >1:
                        for line in mysqlc(uinput1):
                            ip = line[2] 
                            print '%s' % ip
                            os.system(vstatus(ip))
                    else:
                        ip = mysqlc(uinput1)[0][2]
                        os.system(vstatus(ip))
            else:
                print '\033[31;1m请输入正确的选择\033[0m'                   
    elif sel_input == '4':
       while True:
           print '\033[33;1m%s\033[0m' % menu4
           uinput = raw_input('>') 
           if uinput == 'b':
               break
           elif uinput == '1':
               while True:
                   print '\033[35;1m%s\033[0m' % menu1
                   uinput1 = raw_input('>')
                   if uinput1 == 'b':
                       break
                   elif len(mysqlc(uinput1)) == 0:
                       print '\033[31;1m请输入正确的选择\033[0m'
                   elif len(mysqlc(uinput1)) >1:
                       for line in mysqlc(uinput1):
                           ip = line[2] 
                           print ip
                           os.system(Show_wget_status(ip,'*.img.1'))
                   else:
                       ip = mysqlc(uinput1)[0][2]
                       os.system(Show_wget_status(ip,'*.img'))
           elif uinput == '2':
               print '开发中....(没有定义好分发服务器)'
           elif uinput == '3':
               print '开发中....太危险暂时不使用'
           else:
              print '\033[31;1m请输入正确的选择\033[0m' 
    elif sel_input == '5':
        print '开发中....'
        continue
    else:
        print '\033[31;1m请输入正确选项1,2,3,4\033[0m'