err23211 发表于 2015-1-29 08:49:04

堡垒机python写一个sshe脚本

目的:当客户在web端登录账户sean时候,系统自动运行某个脚本,脚本内容为各个主机的IP,等终止这个脚本后,系统自动退出当前用户。

编写ssh python脚本:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env python
import os
ip_file = '/home/sean/ip.txt'
log_file = '/usr/local/ajaxterm/share/ajaxterm/test.txt'
f = file(ip_file)
ip_dic = {}
num = 0
while True:
      line = f.readline()
      if len(line) == 0:break
      num +=1
      ip_dic = line
f.close()
while True:
   try:
      for a,b in ip_dic.items():
                print '\033[32;1m%s. %s\033[0m'%(a,b),
      option = int(raw_input('please choose one server to connect:'))
      if option in ip_dic.keys():
                print ip_dic,
                f = file(log_file,'a')
                f.write('\nLogin Info: connect to %s\n' %ip_dic)
                f.close()
                user = raw_input('username:').strip()
                cmd = 'ssh %s@%s' %(user,ip_dic)
                os.system(cmd)
      else:
                print 'Input error!'
   except ValueError:
      print 'Wrong value!'




在sean用户的宿主目录下修改.bash_profile文件,使其登录便执行脚本

1
2
3
4
5
6
7
8
9
10
11
# cat .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
python term_console.py
logout




安装Ajaxterm
这里我们有一个python写的程序,能够把ssh转换到http.从而让我们在浏览器中完成我们的远程操作.
   首先是下载ajaxterm这个软件.安装很简单
# ./configure --perfix=/usr/local/ajaxterm
# make
# make install
修改配置文件后启动qweb.QWebWSGIServer(at,ip='localhost',port=int(o.port),threaded=0,log=o.log).serve_forever()把localhost更改为0.0.0.0 然后启动
# ./ajaxterm
AjaxTerm at http://localhost:8022/
使用浏览器访问http://192.168.1.160:8022
切记关闭selinux和iptables命令分别为selinxuenable 0 、service iptables stop

页: [1]
查看完整版本: 堡垒机python写一个sshe脚本