首先感谢老男孩提供Ajaxterm修改本,他修改了Ajaxterm中ajaxterm.py,使得Ajaxterm 能记录历史命令,该记录保存在Ajiaxterm目录当中。
原理:Ajaxterm 是一个web shell ,可在本地通过web登录本地系统。利用nginx的反代理,使得Ajaxterm 能远程登录系统。我们要做的就是在堡垒机中实现一个简单的用户shell,其它用户登录堡垒机,使用这个shell再去登录其他系统,ajaxterm记录下用户的操作记录。原理图如下:
实现步骤:
1、nginx 下载安装,本人的系统为centos6.7,可以源码安装,也可以yum install,不过使用yum要配置安装源,这在nginx官网有说明
2、nginx 反向代理设置,修改/etc/nginx/nginx.conf 配置文件:增加以下几行:
server{
listen 80;
server_name 192.168.56.102;
location / {
proxy_pass http://127.0.0.1:8022;
}
其中server_name 是你的堡垒机的ip地址
3、堡垒机自制shell,使用的是python2.7写的,我的文件名:formachine.py,文件内容:
# !/usr/local/bin/python
#_*_ coding:utf-8 _*_
import os
f= open("ip.txt",'r')
lines = f.readlines()
f.close()
while True:
#print "Please select which machine that you want to login:"
i = 0;
machine = {}
for ip in lines:
i += 1
machine[i]=ip
print '%d:'%i,ip
s = int(raw_input( "Please select which machine that you want to login:").strip())
# print s
try:
if s in machine.keys():
user = raw_input("username:").strip()
os.system("ssh %s@%s"%(user,machine[s]))
#iprint("ssh %s"%machine[s])
except Exception,e:
print "choice error,please select again"
logout
写完后记得把它放到用户的 .bash_profile中,比如我的 .bash_profile:
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
./formachine.py
主要的事情做好了,剩下的就是启动Ajaxterm 和nginx.,但是不要忘了关闭防火墙和selinux:service iptables stop; set enforce 0;
运行效果:
堡垒机记录的操作记录:
Mon Jan 11 15:28:12 2016 shformachine
Mon Jan 11 15:28:16 2016 zhangdeping
Mon Jan 11 15:28:23 2016 1
Mon Jan 11 15:28:31 2016 zhangdeping
Mon Jan 11 15:28:34 2016 xxx
Mon Jan 11 15:28:44 2016 zhangdeping
Mon Jan 11 15:28:49 2016 logout
Mon Jan 11 15:28:54 2016 3
Mon Jan 11 15:36:42 2016 formachine
Mon Jan 11 15:36:47 2016 zhangdeping
Mon Jan 11 15:36:58 2016 1
Mon Jan 11 15:37:02 2016 zhangdeping
Mon Jan 11 15:37:09 2016 zhangdeping
Mon Jan 11 15:37:13 2016 ls
Mon Jan 11 15:37:27 2016 mkdir xxx
Mon Jan 11 15:37:33 2016 rm xxx