diff --git a/connect.py b/connect.py index 435adf258..c1535e27d 100755 --- a/connect.py +++ b/connect.py @@ -21,6 +21,7 @@ django.setup() from juser.models import User from jasset.models import Asset +from jlog.models import Log try: import termios @@ -93,16 +94,21 @@ def set_win_size(sig, data): pass -def posix_shell(chan, user, host): +def posix_shell(chan, username, host): """ Use paramiko channel connect server and logging. """ connect_log_dir = os.path.join(LOG_DIR, 'connect') - today = time.strftime('%Y%m%d') - date_now = time.strftime('%Y%m%d%H%M%S') + timestamp_start = int(time.time()) + today = time.strftime('%Y%m%d', time.localtime(timestamp_start)) + date_now = time.strftime('%Y%m%d%H%M%S', time.localtime(timestamp_start)) + today_connect_log_dir = os.path.join(connect_log_dir, today) - log_filename = '%s_%s_%s.log' % (user, host, date_now) + log_filename = '%s_%s_%s.log' % (username, host, date_now) log_file_path = os.path.join(today_connect_log_dir, log_filename) + user = User.objects.get(username=username) + asset = Asset.objects.get(ip=host) + pid = os.getpid() if not os.path.isdir(today_connect_log_dir): try: @@ -112,10 +118,13 @@ def posix_shell(chan, user, host): alert_print('Create %s failed, Please modify %s permission.' % (today_connect_log_dir, connect_log_dir)) try: - log = open(log_file_path, 'a') + log_file = open(log_file_path, 'a') except IOError: alert_print('Create logfile failed, Please modify %s permission.' % today_connect_log_dir) + log = Log(user=user, asset=asset, log_path=log_file_path, start_time=timestamp_now, pid=pid) + log.save() + old_tty = termios.tcgetattr(sys.stdin) try: tty.setraw(sys.stdin.fileno()) @@ -135,8 +144,8 @@ def posix_shell(chan, user, host): break sys.stdout.write(x) sys.stdout.flush() - log.write(x) - log.flush() + log_file.write(x) + log_file.flush() except socket.timeout: pass @@ -147,8 +156,12 @@ def posix_shell(chan, user, host): chan.send(x) finally: + timestamp_end = time.time() termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_tty) - log.close() + log_file.close() + log.is_finished = True + log.end_time = timestamp_end + log.save() def get_user_host(username): diff --git a/docs/developer_doc.txt b/docs/developer_doc.txt index a34f16bc6..d24cacdcd 100644 --- a/docs/developer_doc.txt +++ b/docs/developer_doc.txt @@ -31,5 +31,6 @@ connect.py逻辑说明: 匹配到1个则继续 查询该服务器是否支持ldap 如果是,获得ldap用户密码登陆 如果否,查询授权表,查看该服务器授权的角色,并返回对应账号密码,登陆 - connect函数是登陆函数,采用pramiko 使用channel登陆,posix_shell 来完成交互,并记录日志 - signal模块来完成窗口改变导致的tty大小随之改变 \ No newline at end of file + connect函数是登陆函数,采用paramiko 使用channel登陆,posix_shell 来完成交互,并记录日志 + signal模块来完成窗口改变导致的tty大小随之改变 + PyCrypt是对称加密类 \ No newline at end of file diff --git a/jlog/models.py b/jlog/models.py index b1bcbe14d..920068fd9 100644 --- a/jlog/models.py +++ b/jlog/models.py @@ -5,7 +5,7 @@ from jasset.models import Asset class Log(models.Model): - username = models.ForeignKey(User) + user = models.ForeignKey(User) asset = models.ForeignKey(Asset) log_path = models.CharField(max_length=100) start_time = models.IntegerField() diff --git a/jumpserver.conf b/jumpserver.conf index 14b53b0b6..4c8f6485a 100644 --- a/jumpserver.conf +++ b/jumpserver.conf @@ -7,4 +7,12 @@ user = jumpserver password = mysql345 database = jumpserver +[ldap] +host_url = ldap://127.0.0.1:389 +base_dn = dc=jumpserver,dc=org +root_dn = cn=admin,dc=jumpserver,dc=org +root_pw = secret234 + +[web] +key = 88aaaf7ffe3c6c04