堡垒机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'
=  file (ip_file)
ip_dic  =  {}
num  =  0
while  True :
         line  =  f.readline()
         if  len (line)  = =  0 : break
         num  + = 1
         ip_dic[num]  =  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[option],
                 =  file (log_file, 'a' )
                 f.write( '\nLogin Info: connect to %s\n'  % ip_dic[option])
                 f.close()
                 user  =  raw_input ( 'username:' ).strip()
                 cmd  =  'ssh %s@%s'  % (user,ip_dic[option])
                 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
[root @yunwei  sean] # 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 然后启动

[root@yunwei bin]# ./ajaxterm

AjaxTerm at http://localhost:8022/

使用浏览器访问http://192.168.1.160:8022 

切记关闭selinux和iptables命令分别为selinxuenable 0 、service iptables stop

wKiom1TIlpeDut4JAAFPB7hFjZE189.jpg










本文转自 xinsir999 51CTO博客,原文链接:http://blog.51cto.com/xinsir/1609194,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值