用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. C#实现的UDP收发请求工具类实例

    本文实例讲述了C#实现的UDP收发请求工具类.分享给大家供大家参考,具体如下: 初始化: ListeningPort = int.Parse(ConfigurationManager.AppSetti ...

  2. mRNA基本概念

    mRNA是由DNA的一条链转录而来的(可以是正链,也可以是反链),DNA是由非编码区和编码区组成,编码区也有其特殊的结构,主要有外显子和内含子组成. mRNA的一个重要性质就是可变剪切,也就是同一个编 ...

  3. Recursive Queries CodeForces - 1117G (线段树)

    题面: 刚开始想复杂了, 还以为是个笛卡尔树.... 实际上我们发现, 对于询问(l,r)每个点的贡献是$min(r,R[i])-max(l,L[i])+1$ 数据范围比较大在线树套树的话明显过不了, ...

  4. 一、Object类

    1.Object类是所有类的父类 声明一个类的时候,实际上已经默认继承了Object类 package property; public class Hero extends Object{ Stri ...

  5. OC Foundation框架—结构体

    一.基本知识 Foundation—基础框架.框架中包含了很多开发中常用的数据类型,如结构体,枚举,类等,是其他ios框架的基础. 如果要想使用foundation框架中的数据类型,那么包含它的主头文 ...

  6. nginx支持返回相对路径

    一.问题:http://192.168.72.4/bm-crm/  局域网可以访问[实际指向了192.168.80.1:8081/brm-crm] http://59.41.111.24:8280/b ...

  7. python的日志模块logging和syslog

    syslog模块是在unix环境下工作的模块,不能用于windows,在windows环境下可以使用logging模块. 一.syslog syslog模块可以用于记录系统运行的信息,这个模块提供的方 ...

  8. laravel通过Eloquent ORM实现CURD

    //Eloquent ORM public function orm1() { //all(); 返回所有数据: /*$students=Student::all(); dd($students);* ...

  9. PHP:第四章——PHP数组查找,替换,过滤,判断相关函数

    <pre> <?php //查找,替换,过滤,判断相关函数 header("Content-Type:text/html;charset=utf-8"); /*知 ...

  10. VM VirtualBox虚拟机与物理主机之间的复制

    物理主机: 系统:Ubuntu 11.04 X86_64 虚拟机: 系统:Windows XP Pack3 点击虚拟机的 设备->安装增强功能即可 安装后两系统之间的复制,粘贴可正常使用,如同一 ...