记录日志

pull/6/head
ibuler 2014-12-26 23:58:11 +08:00
parent 5e8605981b
commit 0d48884e19
4 changed files with 33 additions and 11 deletions

View File

@ -21,6 +21,7 @@ django.setup()
from juser.models import User from juser.models import User
from jasset.models import Asset from jasset.models import Asset
from jlog.models import Log
try: try:
import termios import termios
@ -93,16 +94,21 @@ def set_win_size(sig, data):
pass pass
def posix_shell(chan, user, host): def posix_shell(chan, username, host):
""" """
Use paramiko channel connect server and logging. Use paramiko channel connect server and logging.
""" """
connect_log_dir = os.path.join(LOG_DIR, 'connect') connect_log_dir = os.path.join(LOG_DIR, 'connect')
today = time.strftime('%Y%m%d') timestamp_start = int(time.time())
date_now = time.strftime('%Y%m%d%H%M%S') 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) 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) 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): if not os.path.isdir(today_connect_log_dir):
try: 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)) alert_print('Create %s failed, Please modify %s permission.' % (today_connect_log_dir, connect_log_dir))
try: try:
log = open(log_file_path, 'a') log_file = open(log_file_path, 'a')
except IOError: except IOError:
alert_print('Create logfile failed, Please modify %s permission.' % today_connect_log_dir) 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) old_tty = termios.tcgetattr(sys.stdin)
try: try:
tty.setraw(sys.stdin.fileno()) tty.setraw(sys.stdin.fileno())
@ -135,8 +144,8 @@ def posix_shell(chan, user, host):
break break
sys.stdout.write(x) sys.stdout.write(x)
sys.stdout.flush() sys.stdout.flush()
log.write(x) log_file.write(x)
log.flush() log_file.flush()
except socket.timeout: except socket.timeout:
pass pass
@ -147,8 +156,12 @@ def posix_shell(chan, user, host):
chan.send(x) chan.send(x)
finally: finally:
timestamp_end = time.time()
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_tty) 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): def get_user_host(username):

View File

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

View File

@ -5,7 +5,7 @@ from jasset.models import Asset
class Log(models.Model): class Log(models.Model):
username = models.ForeignKey(User) user = models.ForeignKey(User)
asset = models.ForeignKey(Asset) asset = models.ForeignKey(Asset)
log_path = models.CharField(max_length=100) log_path = models.CharField(max_length=100)
start_time = models.IntegerField() start_time = models.IntegerField()

View File

@ -7,4 +7,12 @@ user = jumpserver
password = mysql345 password = mysql345
database = jumpserver 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