Esempio n. 1
0
 def add_remoteuser(self):
     '''添加远程连接用户'''
     while True:
         print('添加连接远程主机帐户'.center(50, '-'))
         username = input('请输入用户名:').strip()
         if username == 'b':
             break
         if len(username) == 0: continue
         auth_type = input('请输入认证类型(ssh-password/ssh-key):').strip()
         if auth_type == 'ssh-password':
             passwd = input('请输入密码:').strip()
             passwd2 = input('请再次输入密码:').strip()
             if passwd == passwd2:
                 ru_obj = RemoteUser(auth_type=auth_type,
                                     username=username,
                                     password=passwd)
                 try:
                     session.add(ru_obj)
                     session.commit()
                     print('添加完成')
                 except IntegrityError:
                     session.rollback()  #如果上面的事务出错,这里要用到回滚,否则再次查询时会出错
                     print('\033[31;0m该帐户已存在\033[0m')
             else:
                 print('两次密码不一致')
         elif auth_type == 'ssh-key':
             ru_obj = RemoteUser(auth_type=auth_type, username=username)
             session.add(ru_obj)
             session.commit()
             print('添加完成')
         else:
             print('请输入正确的认证类型')
         q_choice = input('是否退出?(y/n):').strip()
         if q_choice == 'y':
             break
Esempio n. 2
0
 def add_user(self):
     '''添加堡垒机用户'''
     while True:
         print('添加堡垒机用户'.center(50, '-'))
         username = input('请输入用户名:').strip()
         if username == 'b':
             break
         if len(username) == 0: continue
         passwd = input('请输入密码:').strip()
         passwd2 = input('请再次输入密码:').strip()
         if len(passwd2) == 0: continue
         if passwd == passwd2:
             u_obj = UserProfile(username=username, password=passwd)
             try:
                 session.add(u_obj)
                 session.commit()
                 print('添加完成')
             except IntegrityError:
                 session.rollback()  #如果上面的事务出错,这里要用到回滚,否则再次查询时会出错
                 print('\033[31;0m用户已存在\033[0m')
         else:
             print('两次密码不一致')
         q_choice = input('是否退出?(y/n):').strip()
         if q_choice == 'y':
             break
Esempio n. 3
0
 def add_bindhost(self):
     '''主机和使用登陆的帐户密码绑定'''
     while True:
         print('主机绑定远程连接帐户'.center(50, '-'))
         h_obj = session.query(Host).all()
         for h_index, h in enumerate(h_obj):
             print('%s. ip:%s' % (h_index, h))
         choice = input('请选择ip左边相应的序号:').strip()
         if len(choice) == 0: continue
         if choice == 'b':
             break
         if choice.isdigit():
             choice = int(choice)
             if choice < len(h_obj):
                 host_obj = h_obj[choice]
             else:
                 print('所输入的数字不在主机选择范围内')
                 continue
         else:
             print('请输入数字')
             continue
         ruse_obj = session.query(RemoteUser).all()
         for r_index, ruse in enumerate(ruse_obj):
             print('%s. %s' % (r_index, ruse))
         while True:
             ruse_choice = input('请选择该主机要绑定的帐户序号:')
             if len(ruse_choice) == 0: continue
             if ruse_choice.isdigit():
                 ruse_choice = int(ruse_choice)
                 if ruse_choice < len(ruse_obj):
                     r_obj = ruse_obj[ruse_choice]
                     bind_obj = BindHost(host_id=host_obj.id,
                                         remoteuser_id=r_obj.id)
                     try:
                         session.add(bind_obj)
                         session.commit()
                         print('绑定成功')
                     except IntegrityError:
                         session.rollback()  #如果上面的事务出错,这里要用到回滚,否则再次查询时会出错
                         print('\033[31;0m已存在\033[0m')
                     break
                 else:
                     print('所输入的数字不在主机选择范围内')
                     continue
             else:
                 print('请输入数字')
                 continue
         q_choice = input('是否退出绑定?(y/n):').strip()
         if q_choice == 'y':
             break
Esempio n. 4
0
 def add_hostgroup(self):
     '''添加主机组'''
     while True:
         print('创建主机组'.center(50, '-'))
         group_name = input('请输入主机组名:').strip()
         if group_name == 'b':
             break
         if len(group_name) == 0: continue
         group_obj = HostGroup(name=group_name)
         try:
             session.add(group_obj)
             session.commit()
             print('添加完成')
         except IntegrityError:
             session.rollback()  #如果上面的事务出错,这里要用到回滚,否则再次查询时会出错
             print('\033[31;0m主机组已存在\033[0m')
         q_choice = input('是否退出?(y/n):').strip()
         if q_choice == 'y':
             break
Esempio n. 5
0
 def add_host(self):
     '''添加远程主机'''
     while True:
         print('添加远程连接主机'.center(50, '-'))
         h_name = input('请输入主机名:').strip()
         if h_name == 'b':
             break
         if len(h_name) == 0: continue
         h_ip = input('请输入IP地址:').strip()
         if len(h_ip) == 0: continue
         port = input('请输入SSH连接端口号(默认为22):').strip()
         if len(port) == 0:
             port = 22
         h_obj = Host(hostname=h_name, ip=h_ip, port=port)
         try:
             session.add(h_obj)
             session.commit()
             print('添加完成')
         except IntegrityError:
             session.rollback()  #如果上面的事务出错,这里要用到回滚,否则再次查询时会出错
             print('\033[31;0m主机已存在,请误重复添加\033[0m')
         q_choice = input('是否退出?(y/n):').strip()
         if q_choice == 'y':
             break