Guacamole user-mapping file Generation script

Source: Internet
Author: User

#!/usr/bin/python#coding:utf-8from xml.dom.minidom import documentimport json,requests, Hashlib,refrom config import *def add_host (User,hostname,protocol,ip,port,host_username, host_password,os_family):    h = hostname    p =  Protocol    connection = config.createelement (' connection ')      connection.setattribute (' name ',  h)     user.appendchild (connection)      protocol = config.createelement (' protocol ')     protocol_text =  config.createtextnode (P)     protocol.appendchild (Protocol_text)      connection.appendchild (Protocol)     param = config.createelement (' param ')     param.setattribute (' name ',  ' hostname ')     param_text =  config.createtextnode(IP)     param.appendchild (param_text)     connection.appendchild (param)     param = config.createelement (' param ')      Param.setattribute (' name ',  ' Port ')     param_text = config.createtextnode ( Port)     param.appendchild (param_text)     connection.appendchild (param )     param = config.createelement (' param ')      Param.setattribute (' name ',  ' username ')     param_text =  Config.createtextnode (Host_username)     param.appendchild (param_text)      connection.appendchild (param)     param = config.createelement (' param ')     param.setattribute (' name ',  ' password ')     param_text =  config.createtextnode (Host_password)     paraM.appendchild (param_text)     connection.appendchild (param)         if os_family ==  ' win ':         param =  config.createelement (' param ')         param.setattribute (' name ',   ' enable-drive ')         param_text =  Config.createtextnode (' true ')         param.appendchild (Param_text)         connection.appendchild (param)          param = config.createelement (' param ')          param.setattribute (' name ',  ' Create-drive-path ')          Param_text = config.createtextnode (' true ')          Param.appendchild (Param_text)     &NBSp;   connection.appendchild (param)         param =  config.createelement (' param ')         param.setattribute (' name ',   ' Drive-path ')         param_text =  Config.createtextnode ('/var/ftp/pub/upload ')         param.appendchild ( Param_text)         connection.appendchild (param)         else:        param =  Config.createelement (' param ')         param.setattribute (' name ',  ') Color-scheme ')         param_text = config.createtextnode (' White-black ')         param.appendchild (param_text)          connection.appendchild (PAram)             return connectiondef  Add_user (Map,guac_username,guac_password):     user = config.createelement (' Authorize ')     user.setattribute (' Password ',  guac_password)      User.setattribute (' username ',  guac_username)     user.setattribute (' Encoding ',  ' MD5 ')     result = map.appendchild (user)     return  Resultdef init_host (host):     one_host = {}    one_host [' IP '] = host[' ip ']    one_host[' hostname '] = host[' name ']     one_host[' os '] = host[' osfamily ']    one_host[' env '] =  host[' env ']    one_host[' tag '] = host[' tag ']    # _tmp  = re.match (R ' ^[a-za-z]{3,10} ',  host[' tag '])     # one_host[' tag '] = _tmp.group ()      return one_host    def get_url (hostname):     import base64    name = hostname +  ' \x00c\x00default '      init_name = base64.b64encode (name)     # #guacamole服务器的公网ip     url =  ' http://ip:8081/#/client/'  + init_name       RETURN URL    DEF CREATE_MD5 (PWD):     m  = HASHLIB.MD5 ()     m.update (PWD)     password =  M.hexdigest ()      #print  password    return password     if __name__== ' __main__ ':     config = document ()      map = conFig.createelement (' user-mapping ')     config.appendchild (map)          # #用户是从CMDB  api get      url =  '/http cmdb.beyondhost.com/api/user/get/'     r = requests.get (URL)      users = json.loads (r.text)         # #服务器列表也是cmdb中获取的     url =  ' http://cmdb.beyondhost.com/api/get_uhost/'     r  = requests.get (URL)     hosts = json.loads (r.text)       #print  json.dumps (hosts,indent=4)     host_set = []     for host in hosts:        if host[' env ']== ' Test ':             _tmp = init_host (host)       &nBsp;     host_set.append (_tmp)     for user in  users:        guac_username = user[' username ']         guac_password = user[' Password ']         guac_group = user[' Group ']        guac_ additionalgroup = user[' Additionalgroupname ']        user  = add_user (Map, guac_username, guac_password)          for host in host_set:             if guac_group ==  ' OPS ':                 if host[' OS '] ==  ' win ':                     port =  ' 13040 '                      protocol =  ' RDP '                      hostname = host[' hostname ']                     ip = host[' IP ']                     host_username  =  ' Administrator '                      host_password = admin_passwd_test                     add_host (User,  hostname, p rotocol, ip, port, host_username, host_password,host[' OS ')                  elif host[' OS '] ==  ' Linux ':                     port =  '                      protocol =  ' ssh '                      hostname =  host[' hostname ']                     ip = host[' IP ']                     host_username =  ' Ansible '                     host_password =  ansible_passwd_test                     add_host (user, hostname, protocol, ip, port, host_ username, host_password,host[' OS ')                                   else:                 if len (Guac_additionalgroup)  > 0:                     if host[' tag '] in  guac_additionalgroup :                          if host[' os '] ==  ' win ':                              port =  ' 13040 '                               protocol =  ' RDP '                              hostname = host[ ' Hostname ']                             ip = host[' IP ']                              host_username =  ' Dev '                               host_password = dev_passwd_test                              Add_host (user, hostname, protocol, ip, port, host_username, host_password,host[' Os '])                                                       elif  host[' OS '] ==  ' Linux ':                             port =  '                               protocol =  ' ssh '                               hostname = host[' hostname ']                              ip = host[' IP ']                             host_username =   ' Ansible '                              host_password = ansible_passwd_test                              add_host (user, hostname, protocol, ip,  port, host_username, host_password,host[' OS ')                                   file_object = open ('/etc/guacamole/user-mapping.xml ', ' W ')      file_object.write (Config.toprettyxml (encoding= ' utf-8 '))     file_object.close ()




Guacamole user-mapping file Generation script

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.