用pthon来写个跳板机

 

1、需求

程序一:
1、后台管理
- 堡垒机上创建用户和密码(堡垒机root封装的类,UserProfile表)
- .bashrc 
/usr/bin/python3 /data/bastion.py
exit

2、后台管理
- 服务器上创建用户和密码 或 公钥上传
- 服务器账号 -> 人 关联

程序二:
3、用户登录

- ssh 堡垒机用户名@堡垒机IP
- 获取当前用户 os.environ[‘USER‘]
- 获取当前用户的主机列表
- 获取选中的主机下的所有用户
- 选择任何一个用户

2、实现思路

堡垒机执行流程:

  1. 管理员为用户在服务器上创建账号(将公钥放置服务器,或者使用用户名密码)
  2. 用户登陆堡垒机,输入堡垒机用户名密码,现实当前用户管理的服务器列表
  3. 用户选择服务器,并自动登陆
  4. 执行操作并同时将用户操作记录

注:配置.brashrc实现ssh登陆后自动执行脚本,如:/usr/bin/python /home/wupeiqi/menu.py

那么需要用到的点:

  1. 1、使用 ORM/Schema Type/SQL Expression Language/Engine/ConnectionPooling/Dialect 所有组件对数据进行操作。根据类创建对象,对象转换成SQL,执行SQL。
  2. 2、paramiko模块,基于SSH用于连接远程服务器并执行相关操作。

具体实现流程:

  1. 设计表机构
  2. 创建表结构
  3. 利用paramiko模块去实现跳板机底层的ssh连接并执行相关操作
  4. 将底层的连接封装成跳板机用户对指定主机组和用户的操作并记录日志

3、表结构设计

 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 表结构示例

表结构设计

用pthon来写个跳板机的更多相关文章

  1. 那就用pthon来写个跳板机吧

    1.需求 程序一: 1.后台管理 - 堡垒机上创建用户和密码(堡垒机root封装的类,UserProfile表) - .bashrc /usr/bin/python3 /data/bastion.py ...

  2. Shell跳板机sshstack

    笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 源码地址: https://github.com/sshstack/sshstack 为什么要写shell跳板机? ...

  3. shell开源跳板机sshstack

    笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 源码地址: https://github.com/sshstack/sshstack 为什么要写shell跳板机? ...

  4. linux跳板机开发之trap信号机应用

    场景1:公司新招聘了一个配置管理员,他的工作是负责将公司开发人员写的新代码依次分发到办公室测试环境.IDC测试环境和正式线上环境.因此公司需要开发一个程序,当配置管理员登录服务器,只能进入分发的管理界 ...

  5. Shell实现跳板机,为什么用跳板机

    整理自:http://blog.chinaunix.net/uid-22101889-id-3167454.html 注意:请谨慎使用,到现在为止,使用了,我还没找到改回去的方法. 1.     问题 ...

  6. jmeter连接配置带跳板机(SSH)的mysql服务器

    jmeter连接配置mysql服务器时,如果数据库服务器没有通过ssh连接,则只需要配置相应的jdbc参数就可以了,即请求域名或ip地址:3306,如果数据库服务器是通过SSH连接的,那需要通过中间远 ...

  7. 用meterpreter实现跳板机

      meterpreter跳板机 背景:渗透测试者A拿到了B主机的控制权,但没有拿到ssh密码(不能打ssh隧道).现需横向移动渗透内网主机C,为了避免动作过大不便直接在B上对C进行渗透,其中C不能出 ...

  8. CentOS 7 搭建Jumpserver跳板机(堡垒机)

    跳板机概述: 跳板机就是一台服务器,开发或运维人员在维护过程中首先要统一登录到这台服务器,然后再登录到目标设备进行维护和操作 跳板机缺点:没有实现对运维人员操作行为的控制和审计,使用跳板机的过程中还是 ...

  9. Jumpserver跳板机的搭建和部署

    1.需要搭云yum仓库wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo 2. ...

随机推荐

  1. 一篇文章,读懂Netty的高性能架构之道

    一篇文章,读懂Netty的高性能架构之道 Netty是由JBOSS提供的一个java开源框架,是一个高性能.异步事件驱动的NIO框架,它提供了对TCP.UDP和文件传输的支持,作为一个异步NIO框架, ...

  2. UVA-1252 Twenty Questions (状压DP)

    题目大意:有n件物品,每件物品有m个特征,可以对特征进行询问,询问的结果是得知某个物体是否含有该特征,要把所有的物品区分出来(n个物品的特征都互不相同)最小需要多少次询问? 题目分析:定义dp(s,a ...

  3. After reading a picture than out a picture

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletExcepti ...

  4. java 判断字符串IP合法性以及获取IP的数值形式

    /** * 计算传入的IP地址的数字IP*/ public static long getIpNum(String ip) { long ipNum = 0; if (StringUtils.isNo ...

  5. springboot笔记(一)

    1.为什么一定要实现Iterable接口? http://blog.csdn.net/albert0420/article/details/44922325 而Iterable则不然,每次调用都会返回 ...

  6. snagit12个人爱好

  7. PHP:第二章——PHP中的foreach语句

    foreach语句提供了遍历数组的 <?php header("Content-Type:text/html;charset=utf-8"); $arr=array(&quo ...

  8. learning uboot enable protect console

    reference :https://github.com/lentinj/u-boot/blob/master/doc/README.autoboot how to enable protect s ...

  9. DELPHI 5种运行程序的方法具体应用实例(带参数)

    http://www.02t.cn/article/code/102.html https://msdn.microsoft.com/en-us/library/windows/desktop/ms6 ...

  10. git 添加review的相关操作

    git主机上涉及到的操作: 185 git branch 186 git pull 187 git branch 188 git checkout -b wangcheng 189 git log 1 ...