从0开发一个堡垒机
设计一下数据库
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from django.db import models from django.contrib.auth.models import User # Create your models here. class IDC(models.Model): name = models.CharField(max_length=64, unique=True) def __str__(self): return self.name class Host(models.Model): """ 存储所有主机信息 """ hostname = models.CharField(max_length=64,unique=True) ip_addr = models.GenericIPAddressField(unique=True) port = models.IntegerField(default=22) idc = models.ForeignKey("IDC") enabled = models.BooleanField(default=True) #host_users = models.ManyToManyField("HostUser") def __str__(self): return "%s-%s" %(self.hostname,self.ip_addr) class HostGroup(models.Model): """ 主机组 """ name = models.CharField(max_length=64,unique=True) host_user_binds = models.ManyToManyField("HostUserBind") def __str__(self): return self.name class HostUser(models.Model): """ 存储远程主机的用户信息 """ auth_type_choices = ((0,"ssh-password"),(1,"ssh-key")) auth_type = models.SmallIntegerField(choices=auth_type_choices) username = models.CharField(max_length=32) password = models.CharField(blank=True,null=True,max_length=128) def __str__(self): return "%s-%s-%s" %(self.get_auth_type_display(),self.username,self.password) class Meta: unique_together = ("username","password") class HostUserBind(models.Model): host = models.ForeignKey("Host") host_user = models.ForeignKey("HostUser") def __str__(self): return "%s-%s" % (self.host, self.host_user) class Meta: unique_together = ("host","host_user") class AuditLog(models.Model): """ 审计日志 """ class Account(models.Model): """ 堡垒机账户 """ user = models.OneToOneField(User) name = models.CharField(max_length=64) host_user_binds = models.ManyToManyField("HostUserBind", blank=True) host_groups = models.ManyToManyField("HostGroup", blank=True)
admin注册一下刚建的数据库
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from django.contrib import admin from audit import models # Register your models here. admin.site.register(models.Host) admin.site.register(models.HostUser) admin.site.register(models.HostGroup) admin.site.register(models.HostUserBind) admin.site.register(models.Account) admin.site.register(models.IDC) admin.site.register(models.AuditLog)
安装openssh,sshpass
从网上下载也可以,不过得改openssh源码 链接:https://pan.baidu.com/s/1PtRg6n_AUlC20_79QtZGog 密码:jzh1 我的已经下载了,直接编译安装即可 进入openssh chmod +x mkinstalldirs chmod +x configure ./configure make && make install sshpass同上 注意有可能会报错误configure: error: OpenSSL version header not found.这是因为openssh需要openssl,此时需要安装一下openssl的开发模块` 在ubuntu上安装openssl dev组件 sudo apt-get install zlib1g sudo apt-get install zlib1g-dev sudo apt-get install libssl-dev 再重新执行configure就应该没问题了
$ sudo vim /etc/sudoers
%用户名 ALL=NOPASSWD:ALL #/usr/bin/strace,/usr/bin/python3