diff --git a/connect.py b/connect.py index 00a826de0..9690af7ce 100644 --- a/connect.py +++ b/connect.py @@ -67,6 +67,7 @@ class Tty(object): self.user = user self.role = role self.ssh = None + self.remote_ip = '' self.connect_info = None self.login_type = 'ssh' self.vim_flag = False @@ -192,17 +193,18 @@ class Tty(object): if self.login_type == 'ssh': # 如果是ssh连接过来,记录connect.py的pid,web terminal记录为日志的id pid = os.getpid() - remote_ip = os.popen("who -m | awk '{ print $5 }'").read().strip('()\n') # 获取远端IP - log = Log(user=self.username, host=self.asset_name, remote_ip=remote_ip, - log_path=log_file_path, start_time=date_today, pid=pid) + self.remote_ip = os.popen("who -m | awk '{ print $5 }'").read().strip('()\n') # 获取远端IP else: - remote_ip = 'Web' - log = Log(user=self.username, host=self.asset_name, remote_ip=remote_ip, - log_path=log_file_path, start_time=date_today, pid=0) - log.save() - log.pid = log.id + 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.save() + log_file_f.write('Start at %s\n' % datetime.datetime.now()) return log_file_f, log_time_f, log diff --git a/jlog/models.py b/jlog/models.py index a75b7bccd..69b667c46 100644 --- a/jlog/models.py +++ b/jlog/models.py @@ -5,6 +5,7 @@ class Log(models.Model): user = models.CharField(max_length=20, null=True) host = models.CharField(max_length=20, null=True) remote_ip = models.CharField(max_length=100) + login_type = models.CharField(max_length=100) log_path = models.CharField(max_length=100) start_time = models.DateTimeField(null=True) pid = models.IntegerField() diff --git a/run_websocket.py b/run_websocket.py index 9250dd3a8..e3a858d82 100644 --- a/run_websocket.py +++ b/run_websocket.py @@ -267,6 +267,7 @@ class WebTerminalHandler(tornado.websocket.WebSocketHandler): logger.debug('Websocket: request web terminal Host: %s User: %s Role: %s' % (asset.hostname, self.user.username, login_role.name)) self.term = WebTty(self.user, asset, login_role) + self.term.remote_ip = self.request.remote_ip self.term.get_connection() self.term.channel = self.term.ssh.invoke_shell(term='xterm') WebTerminalHandler.tasks.append(MyThread(target=self.forward_outbound)) diff --git a/templates/jlog/log_offline.html b/templates/jlog/log_offline.html index 012a58bc7..8d04d207c 100644 --- a/templates/jlog/log_offline.html +++ b/templates/jlog/log_offline.html @@ -102,10 +102,11 @@