Example #1
0
def create_user_bind_host(g_obj):
    g_obj_bind_hosts_list = session.query(
        table_init.Group_Bind_Host).filter(table_init.Group_Bind_Host.group_id
                                           == g_obj.id).all()  #该group的所有bind实例
    for i in g_obj_bind_hosts_list:
        i.users = g_obj.users
    session.commit()
Example #2
0
def create_user(argvs):
    if '-f' in argvs:
        user_file = argvs[argvs.index("-f") + 1]
    else:
        print_err("Usage:\ncreate_users -f <the user_file path>", quit=True)

    source = yaml_parser(user_file)
    if source:
        for key, val in source.items():
            print(key, val)
            obj = table_init.User(username=key, password=val.get('password'))
            # session.add(obj)
            # session.commit()
            if val.get('group'):

                groups = session.query(table_init.Group).filter(
                    table_init.Group.name.in_(val.get('group'))).all()
                print(groups)
                if not groups:
                    print_err('Group %s not exist' % val.get('users'),
                              quit=True)
                else:
                    obj.groups = groups
            session.add(obj)
            session.commit()
Example #3
0
def posix_shell(chan,user_obj,choose_host,choose_group):
    import select

    oldtty = termios.tcgetattr(sys.stdin)
    try:
        tty.setraw(sys.stdin.fileno())
        tty.setcbreak(sys.stdin.fileno())
        chan.settimeout(0.0)
        cmd = ''

        tab_key = False
        while True:
            r, w, e = select.select([chan, sys.stdin], [], [])
            if chan in r:
                try:
                    x = u(chan.recv(1024))
                    if tab_key:
                        if x not in ('\t','\r\n'):
                            #print('tab:',x)
                            cmd += x
                        tab_key = False
                    if len(x) == 0:
                        sys.stdout.write('\r\n*** EOF\r\n')
                        break
                    sys.stdout.write(x)
                    sys.stdout.flush()
                except socket.timeout:
                    pass

            if sys.stdin in r:
                x = sys.stdin.read(1)
                if '\r' != x:
                    cmd +=x      #输入字符不包含回车,则命令还未输入完成,包含回车且输入字符长度大于0,则记录日志
                if '\r' == x and len(cmd)>0:
                    log_item = table_init.Log_audit(user_id=user_obj.id,
                                               user_name=user_obj.username,
                                          host_ip=choose_host.ip,
                                          login_user=choose_group.name,
                                          action_type='cmd',
                                          cmd=cmd ,
                                          date=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")

                                          )
                    session.add(log_item)
                    session.commit()
                    cmd = ''

                if '\t' == x:
                    tab_key = True
                # if len(x) == 0:
                #     break
                chan.send(x)

    finally:
        termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)
Example #4
0
def create_group(argvs):
    if '-f' in argvs:
        groups_file = argvs[argvs.index("-f") + 1]
    else:
        print_err("Usage:\ncreate_groups -f <the group_file path>", quit=True)
    source = yaml_parser(groups_file)
    print(source)
    if source:
        for key, val in source.items():
            group_obj = table_init.Group(name=key,
                                         login_passwd=val.get('password'))
            session.add(group_obj)
        session.commit()
Example #5
0
def create_group_bind_host(argvs):
    if '-f' in argvs:
        bind_file = argvs[argvs.index("-f") + 1]
    else:
        print_err("Usage:\ncreate_group_bind_host -f <the group_file path>",
                  quit=True)
    source = yaml_parser(bind_file)
    if source:
        for bind_ins, val in source.items():
            g_obj = session.query(table_init.Group).filter(
                table_init.Group.name == val.get('groupname')).first()
            assert g_obj
            host_obj = session.query(table_init.Host).filter(
                table_init.Host.hostname.in_(val.get('hostname'))).all()
            print(host_obj)
            g_obj.bind_hosts = host_obj
            session.commit()
            create_user_bind_host(g_obj)
Example #6
0
def create_hosts(argvs):
    '''
    create hosts
    :param argvs:
    :return:
    '''
    if '-f' in argvs:
        hosts_file = argvs[argvs.index("-f") + 1]  # -f参数后一位为文件路径
    else:
        print_err("Usage:\ncreate_hosts -f <the host_file path>", quit=True)
    source = yaml_parser(hosts_file)  #文件交由yaml_parser处理,处理完的结果返回
    if source:
        print(source)
        for key, val in source.items():
            print(key, val)
            obj = table_init.Host(hostname=key,
                                  ip=val.get('ip'),
                                  port=val.get('port') or 22)
            session.add(obj)
        session.commit()