目的:当客户在web端登录账户sean时候,系统自动运行某个脚本,脚本内容为各个主机的IP,等终止这个脚本后,系统自动退出当前用户。
编写ssh python脚本:
#!/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[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], f = 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文件,使其登录便执行脚本
[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