Browse Source

记录日志

pull/6/head
ibuler 10 years ago
parent
commit
0d48884e19
  1. 29
      connect.py
  2. 3
      docs/developer_doc.txt
  3. 2
      jlog/models.py
  4. 8
      jumpserver.conf

29
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):

3
docs/developer_doc.txt

@ -31,5 +31,6 @@ connect.py逻辑说明:
匹配到1个则继续
查询该服务器是否支持ldap 如果是,获得ldap用户密码登陆
如果否,查询授权表,查看该服务器授权的角色,并返回对应账号密码,登陆
connect函数是登陆函数,采用pramiko 使用channel登陆,posix_shell 来完成交互,并记录日志
connect函数是登陆函数,采用paramiko 使用channel登陆,posix_shell 来完成交互,并记录日志
signal模块来完成窗口改变导致的tty大小随之改变
PyCrypt是对称加密类

2
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()

8
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

Loading…
Cancel
Save