用pthon来写个跳板机
用pthon来写个跳板机
1、需求
程序一:
1、后台管理
- 堡垒机上创建用户和密码(堡垒机root封装的类,UserProfile表)
- .bashrc
/usr/bin/python3 /data/bastion.py
exit
2、后台管理
- 服务器上创建用户和密码 或 公钥上传
- 服务器账号 -> 人 关联
程序二:
3、用户登录
- ssh 堡垒机用户名@堡垒机IP
- 获取当前用户 os.environ[‘USER‘]
- 获取当前用户的主机列表
- 获取选中的主机下的所有用户
- 选择任何一个用户
2、实现思路

堡垒机执行流程:
- 管理员为用户在服务器上创建账号(将公钥放置服务器,或者使用用户名密码)
- 用户登陆堡垒机,输入堡垒机用户名密码,现实当前用户管理的服务器列表
- 用户选择服务器,并自动登陆
- 执行操作并同时将用户操作记录
注:配置.brashrc实现ssh登陆后自动执行脚本,如:/usr/bin/python /home/wupeiqi/menu.py
那么需要用到的点:
- 1、使用 ORM/Schema Type/SQL Expression Language/Engine/ConnectionPooling/Dialect 所有组件对数据进行操作。根据类创建对象,对象转换成SQL,执行SQL。
- 2、paramiko模块,基于SSH用于连接远程服务器并执行相关操作。
具体实现流程:
- 设计表机构
- 创建表结构
- 利用paramiko模块去实现跳板机底层的ssh连接并执行相关操作
- 将底层的连接封装成跳板机用户对指定主机组和用户的操作并记录日志
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来写个跳板机的更多相关文章
- 那就用pthon来写个跳板机吧
1.需求 程序一: 1.后台管理 - 堡垒机上创建用户和密码(堡垒机root封装的类,UserProfile表) - .bashrc /usr/bin/python3 /data/bastion.py ...
- Shell跳板机sshstack
笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 源码地址: https://github.com/sshstack/sshstack 为什么要写shell跳板机? ...
- shell开源跳板机sshstack
笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 源码地址: https://github.com/sshstack/sshstack 为什么要写shell跳板机? ...
- linux跳板机开发之trap信号机应用
场景1:公司新招聘了一个配置管理员,他的工作是负责将公司开发人员写的新代码依次分发到办公室测试环境.IDC测试环境和正式线上环境.因此公司需要开发一个程序,当配置管理员登录服务器,只能进入分发的管理界 ...
- Shell实现跳板机,为什么用跳板机
整理自:http://blog.chinaunix.net/uid-22101889-id-3167454.html 注意:请谨慎使用,到现在为止,使用了,我还没找到改回去的方法. 1. 问题 ...
- jmeter连接配置带跳板机(SSH)的mysql服务器
jmeter连接配置mysql服务器时,如果数据库服务器没有通过ssh连接,则只需要配置相应的jdbc参数就可以了,即请求域名或ip地址:3306,如果数据库服务器是通过SSH连接的,那需要通过中间远 ...
- 用meterpreter实现跳板机
meterpreter跳板机 背景:渗透测试者A拿到了B主机的控制权,但没有拿到ssh密码(不能打ssh隧道).现需横向移动渗透内网主机C,为了避免动作过大不便直接在B上对C进行渗透,其中C不能出 ...
- CentOS 7 搭建Jumpserver跳板机(堡垒机)
跳板机概述: 跳板机就是一台服务器,开发或运维人员在维护过程中首先要统一登录到这台服务器,然后再登录到目标设备进行维护和操作 跳板机缺点:没有实现对运维人员操作行为的控制和审计,使用跳板机的过程中还是 ...
- Jumpserver跳板机的搭建和部署
1.需要搭云yum仓库wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo 2. ...
随机推荐
- English trip V1 - 2.Don't Do That Teacher:Patrick Key: 祈使句(imperatives)
什么是祈使句? What's imperatives? 求或者希望别人做什么事或者不做什么事时用的句子:带有命令的语气 In this lesson you will learn how to ...
- 1月11日Atom 插件安装。
查看已安装的Atom插件(前提:已经安装Atom) 打开终端 输入apm ls命令,回车. 未安装任何插件时,显示如下 Built-in Atom packages (89) ...此处省略... / ...
- Confluence 6 完成你的任务
很好,宇航员们,你已经令人钦佩的展示了你自己的.我们确定你新招募的员工已经对你了解的 Confluence 知识感到赞叹. 在这个指南中,我们已经完成了: 在主面板中对 Confluence 的功能进 ...
- zk如何实现watch
在客户端发送命令:stat /zhang watch 在zk server中产生如下图的调用栈: //在DataTree类中有 private final WatchManager dataWatch ...
- 微信小程序跨页面获取数据示例
index.wxml <navigator class="navs" url="{{urls}}"> 中国 </navigator> i ...
- UVALive 4168
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA08AAABMCAIAAAA6IBt+AAAgAElEQVR4nO1dybXjOg7tWJSK65yKxL
- SqlServer中的merge操作(转载)
SqlServer中的merge操作(转载) 今天在一个存储过程中看见了merge这个关键字,第一个想法是,这个是配置管理中的概念吗,把相邻两次的更改合并到一起.后来在technet上搜索发现别有 ...
- poj2892
题解: 答案=后缀-前缀-1 如果被轰了,那么就时0 在一开始加入0,n+1,保证有前缀后缀 代码: #include<cstdio> #include<cmath> #inc ...
- weblogic下更改jsp不生效的解决办法
1.删除user_projects\domains\base_domain(我自己的域名)\servers\AdminServer目录下的所有文件夹 一下为转载from:http://blog.itp ...
- 多种方法实现 python 线程池
最近在做一个爬虫相关的项目,单线程的整站爬虫,耗时真的不是一般的巨大,运行一次也是心累,,,所以,要想实现整站爬虫,多线程是不可避免的,那么python多线程又应该怎样实现呢?这里主要要几个问题(关于 ...