程序一:
一、後臺管理
- 堡壘機上建立用戶和密碼(堡壘機root封裝的類,UserProfile表)
- .bashrc
/usr/bin/python3 /data/bastion.py
exit
二、後臺管理
- 服務器上建立用戶和密碼 或 公鑰上傳
- 服務器帳號 -> 人 關聯
程序二:
三、用戶登陸html
- ssh 堡壘機用戶名@堡壘機IP
- 獲取當前用戶 os.environ[‘USER‘]
- 獲取當前用戶的主機列表
- 獲取選中的主機下的全部用戶
- 選擇任何一個用戶python
堡壘機執行流程:sql
注:配置.brashrc實現ssh登錄後自動執行腳本,如:/usr/bin/python /home/wupeiqi/menu.pybash
那麼須要用到的點:服務器
具體實現流程:session
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 from sqlalchemy import create_engine, and_, or_, func, Table 5 from sqlalchemy.ext.declarative import declarative_base 6 from sqlalchemy import Column, Integer, String, ForeignKey, UniqueConstraint, DateTime 7 from sqlalchemy.orm import sessionmaker, relationship 8 9 Base = declarative_base() # 生成一個SqlORM 基類 10 11 12 class Host(Base): 13 __tablename__ = ‘host‘ 14 id = Column(Integer, primary_key=True, autoincrement=True) 15 hostname = Column(String(64), unique=True, nullable=False) 16 ip_addr = Column(String(128), unique=True, nullable=False) 17 port = Column(Integer, default=22) 18 19 20 class HostUser(Base): 21 __tablename__ = ‘host_user‘ 22 id = Column(Integer, primary_key=True, autoincrement=True) 23 username = Column(String(64), unique=True, nullable=False) 24 AuthTypes = [ 25 (‘p‘, ‘SSH/Password‘), 26 (‘r‘, ‘SSH/KEY‘), 27 ] 28 auth_type = Column(String(16)) 29 cert = Column(String(255)) 30 31 host_id = Column(Integer, ForeignKey(‘host.id‘)) 32 33 __table_args__ = ( 34 UniqueConstraint(‘host_id‘, ‘username‘, name=‘_host_username_uc‘), 35 ) 36 37 38 class Group(Base): 39 __tablename__ = ‘group‘ 40 id = Column(Integer, primary_key=True, autoincrement=True) 41 name = Column(String(64), unique=True, nullable=False) 42 43 44 class UserProfile(Base): 45 __tablename__ = ‘user_profile‘ 46 id = Column(Integer, primary_key=True, autoincrement=True) 47 username = Column(String(64), unique=True, nullable=False) 48 password = Column(String(255), nullable=False) 49 50 51 class Group2UserProfile(Base): 52 __tablename__ = ‘group_2_user_profile‘ 53 id = Column(Integer, primary_key=True, autoincrement=True) 54 user_profile_id = Column(Integer, ForeignKey(‘user_profile.id‘)) 55 group_id = Column(Integer, ForeignKey(‘group.id‘)) 56 __table_args__ = ( 57 UniqueConstraint(‘user_profile_id‘, ‘group_id‘, name=‘ux_user_group‘), 58 ) 59 60 61 class Group2HostUser(Base): 62 __tablename__ = ‘group_2_host_user‘ 63 id = Column(Integer, primary_key=True, autoincrement=True) 64 host_user_id = Column(Integer, ForeignKey(‘host_user.id‘)) 65 group_id = Column(Integer, ForeignKey(‘group.id‘)) 66 __table_args__ = ( 67 UniqueConstraint(‘group_id‘, ‘host_user_id‘, name=‘ux_group_host_user‘), 68 ) 69 70 71 class UserProfile2HostUser(Base): 72 __tablename__ = ‘user_profile_2_host_user‘ 73 id = Column(Integer, primary_key=True, autoincrement=True) 74 host_user_id = Column(Integer, ForeignKey(‘host_user.id‘)) 75 user_profile_id = Column(Integer, ForeignKey(‘user_profile.id‘)) 76 __table_args__ = ( 77 UniqueConstraint(‘user_profile_id‘, ‘host_user_id‘, name=‘ux_user_host_user‘), 78 ) 79 80 81 class AuditLog(Base): 82 __tablename__ = ‘audit_log‘ 83 id = Column(Integer, primary_key=True, autoincrement=True) 84 85 action_choices2 = [ 86 (u‘cmd‘, u‘CMD‘), 87 (u‘login‘, u‘Login‘), 88 (u‘logout‘, u‘Logout‘), 89 ] 90 action_type = Column(String(16)) 91 cmd = Column(String(255)) 92 date = Column(DateTime) 93 user_profile_id = Column(Integer, ForeignKey(‘user_profile.id‘)) 94 host_user_id = Column(Integer, ForeignKey(‘host_user.id‘)) 95 96 表結構示例