def connect(self): print(self.width, self.height) if self.scope["user"].is_anonymous: self.close(code=1007) else: self.accept() try: self.ssh.load_system_host_keys() self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) self.ssh.connect(self.ip, int(self.port), self.username, self.password, timeout=5) except Exception as e: fort_logger.error('用户{}通过webssh连接{}失败!原因:{}'.format( self.username, self.ip, e)) self.send('用户{}通过webssh连接{}失败!原因:{}'.format( self.username, self.ip, e), close=True) self.chan = self.ssh.invoke_shell(term='ansi', width=self.width, height=self.height) # 设置如果3分钟没有任何输入,就断开连接 self.chan.settimeout(60 * 3) self.t1.setDaemon(True) self.t1.start()
def connect(self): self.accept() username = self.server.username try: self.ssh.load_system_host_keys() self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) self.ssh.connect(self.host_ip, int(self.server.port), username, CryptPwd().decrypt_pwd(self.server.password), timeout=5) except Exception as e: fort_logger.error('用户{}通过webssh连接{}失败!原因:{}'.format( username, self.host_ip, e)) self.send('用户{}通过webssh连接{}失败!原因:{}'.format( username, self.host_ip, e)) self.close() self.chan = self.ssh.invoke_shell(term='xterm', width=self.width, height=self.height) # 设置如果3分钟没有任何输入,就断开连接 self.chan.settimeout(60 * 3) self.t1.setDaemon(True) self.t1.start()
def fort_record(login_user, fort, remote_ip, start_time, login_status_time, record_file): try: FortRecord.objects.create(login_user=login_user, fort=fort, remote_ip=remote_ip, start_time=start_time, login_status_time=login_status_time, record_file=record_file) except Exception as e: fort_logger.error('添加用户操作记录失败,原因:{}'.format(e))
def admin_file(filename, txts, header=None): try: if header: f = open(filename, 'a') f.write(json.dumps(header) + '\n') for txt in txts: f.write(json.dumps(txt) + '\n') f.close() else: with open(filename, 'a') as f: for txt in txts: f.write(txt) except Exception as e: fort_logger.error('添加用户操作记录文件失败,原因:{}'.format(e))
def record(self): record_path = os.path.join(settings.MEDIA_ROOT, self.chan.record_dir, self.chan.scope['user'].username, time.strftime('%Y-%m-%d')) if not os.path.exists(record_path): os.makedirs(record_path, exist_ok=True) record_file_name = '{}.{}.cast'.format(self.chan.ip, time.strftime('%Y%m%d%H%M%S')) record_file_path = os.path.join(record_path, record_file_name) header = { "version": 2, "width": self.chan.width, "height": self.chan.height, "timestamp": round(self.start_time), "title": "ssh", "env": { "TERM": os.environ.get('TERM'), "SHELL": os.environ.get('SHELL', '/bin/bash') }, } login_status_time = self.format_time(time.time() - self.start_time) login_user = self.chan.scope['user'] login_server = r'{}@{}'.format(self.chan.username, self.chan.ip) try: if login_user.is_superuser: admin_file.delay(record_file_path, self.stdout, header) AdminRecord.objects.create( admin_login_user=login_user, admin_server=login_server, admin_remote_ip=self.chan.remote_ip, admin_start_time=self.current_time, admin_login_status_time=login_status_time, admin_record_file=record_file_path.split('media/')[1], admin_record_cmds='\n'.join(self.chan.cmd)) else: fort_file.delay(record_file_path, self.stdout, header) FortRecord.objects.create( login_user=login_user, fort=login_server, remote_ip=self.chan.remote_ip, start_time=self.current_time, login_status_time=login_status_time, record_file=record_file_path.split('media/')[1], record_cmds='\n'.join(self.chan.cmd)) except Exception as e: fort_logger.error('数据库添加用户操作记录失败,原因:{}'.format(e))
def record(self): record_path = os.path.join(settings.MEDIA_ROOT, 'admin_ssh_records', self.chan.scope['user'].username, time.strftime('%Y-%m-%d')) if not os.path.exists(record_path): os.makedirs(record_path, exist_ok=True) record_file_name = '{}.{}.cast'.format(self.chan.host_ip, time.strftime('%Y%m%d%H%M%S')) record_file_path = os.path.join(record_path, record_file_name) header = { "version": 2, "width": self.chan.width, "height": self.chan.height, "timestamp": round(self.start_time), "title": "Demo", "env": { "TERM": os.environ.get('TERM'), "SHELL": os.environ.get('SHELL', '/bin/bash') }, } # admin_file.delay(record_file_path, self.stdout, header) login_status_time = time.time() - self.start_time if login_status_time >= 60: login_status_time = '{} m'.format(round(login_status_time / 60, 2)) elif login_status_time >= 3600: login_status_time = '{} h'.format( round(login_status_time / 3660, 2)) else: login_status_time = '{} s'.format(round(login_status_time)) try: AdminRecord.objects.create( admin_login_user=self.chan.scope['user'], admin_server=self.chan.host_ip, admin_remote_ip=self.chan.remote_ip, admin_start_time=self.current_time, admin_login_status_time=login_status_time, admin_record_file=record_file_path.split('media/')[1]) except Exception as e: fort_logger.error('数据库添加用户操作记录失败,原因:{}'.format(e))