def get_log(self): """ Logging user command and output. 记录用户的日志 """ banned_list = ['/', '\0', '*', '?'] tty_log_dir = os.path.join(LOG_DIR, 'tty') date_today = datetime.datetime.now() date_start = date_today.strftime('%Y%m%d') time_start = date_today.strftime('%H%M%S') today_connect_log_dir = os.path.join(tty_log_dir, date_start) filename = '%s_%s_%s' % (self.username, self.asset_name, time_start) for banned_char in banned_list: filename = filename.replace(banned_char, '_') log_file_path = os.path.join(today_connect_log_dir, filename) try: mkdir(os.path.dirname(today_connect_log_dir), mode=777) mkdir(today_connect_log_dir, mode=777) except OSError as e: logger.debug('创建目录 %s 失败,请修改%s目录权限 With error msg: %s' % (today_connect_log_dir, tty_log_dir, e)) raise ServerError('创建目录 %s 失败,请修改%s目录权限' % (today_connect_log_dir, tty_log_dir)) try: log_file_f = open(log_file_path + '.log', 'a') log_time_f = open(log_file_path + '.time', 'a') except IOError as e: logger.debug('创建tty日志文件失败, 请修改目录%s权限 With error msg: %s' % (today_connect_log_dir, e)) raise ServerError('创建tty日志文件失败, 请修改目录%s权限' % today_connect_log_dir) if self.login_type == 'ssh': # 如果是ssh连接过来,记录connect.py的pid,web terminal记录为日志的id pid = os.getpid() self.remote_ip = remote_ip # 获取远端IP else: pid = 0 log = Log(user=self.username, host=self.asset_name, remote_ip=self.remote_ip, login_type=self.login_type, log_path=log_file_path, start_time=date_today, pid=pid) log.save() if self.login_type == 'web': log.pid = log.id # 设置log id为websocket的id, 然后kill时干掉websocket log.save() log_file_f.write('Start at %s\r\n' % datetime.datetime.now()) return log_file_f, log_time_f, log
def get_log(self): """ Logging user command and output. 记录用户的日志 """ tty_log_dir = os.path.join(LOG_DIR, "tty") date_today = datetime.datetime.now() date_start = date_today.strftime("%Y%m%d") time_start = date_today.strftime("%H%M%S") today_connect_log_dir = os.path.join(tty_log_dir, date_start) log_file_path = os.path.join(today_connect_log_dir, "%s_%s_%s" % (self.username, self.asset_name, time_start)) try: mkdir(os.path.dirname(today_connect_log_dir), mode=0777) mkdir(today_connect_log_dir, mode=0777) except OSError: logger.debug("创建目录 %s 失败,请修改%s目录权限" % (today_connect_log_dir, tty_log_dir)) raise ServerError("创建目录 %s 失败,请修改%s目录权限" % (today_connect_log_dir, tty_log_dir)) try: log_file_f = open(log_file_path + ".log", "a") log_time_f = open(log_file_path + ".time", "a") except IOError: logger.debug("创建tty日志文件失败, 请修改目录%s权限" % today_connect_log_dir) raise ServerError("创建tty日志文件失败, 请修改目录%s权限" % today_connect_log_dir) if self.login_type == "ssh": # 如果是ssh连接过来,记录connect.py的pid,web terminal记录为日志的id pid = os.getpid() self.remote_ip = remote_ip # 获取远端IP else: pid = 0 log = Log( user=self.username, host=self.asset_name, remote_ip=self.remote_ip, login_type=self.login_type, log_path=log_file_path, start_time=date_today, pid=pid, ) log.save() if self.login_type == "web": log.pid = log.id # 设置log id为websocket的id, 然后kill时干掉websocket log.save() log_file_f.write("Start at %s\r\n" % datetime.datetime.now()) return log_file_f, log_time_f, log