> CentOS > CentOS教程 >

Centos安装EasyXMS 多线程批量管理系统

EasyXMS是一个用于批量管理Linux/Unix服务器的简易系统,如:多线程批量执行命令、多线程批量上传文件等功能,EasyXMS使用Python2.7编写,并使用paramiko模块来实现SSH连接和SFTP功能。

关注项目主页以获取最新版本:

http://git.oschina.net/leedays/EasyXMS

 

有什么优势?

 

  1. 简单易用

  2. 轻量级,只需要一个脚本文件,不需要安装Client端

  3. 安装部署快,一键安装所需的环境

 

二、主要功能

 

批量执行命令
批量上传文件
记录每次输入的命令到文件
记录每次执行命令的结果到文件

 

三、环境安装

 

1.Python2.7

      下载地址:http://www.python.org

 

2.easy_install 工具(是Python安装模块的一个工具,像yum,可以自动解决依赖)

     下载地址: http://peak.telecommunity.com/dist/ez_setup.py

 

3.PyCrypto 2.1+ 模块(PyCrypto是使用Python编写的加密工具包)

      下载地址:https://www.dlitz.net/software/pycrypto/

 

4.paramiko 模块(是用Python编写的支持SSH协议的模块)

      使用easy_install 进行安装

 

具体的安装过程,参看上一篇文章:

http://linux5588.blog.51cto.com/65280/1275180

 

想省事? 可下载博客附件的一键安装脚本,来安装paramiko模块需要的环境

请使用 < source 脚本名 > 的方式执行脚本

 

四.运行过程中产生的文件

 

在脚本的执行过程中,默认是会在当前目录下生成以下文件

 

1. server.conf

该文件是用于存储各个服务器的连接信息如 IP地址:端口:用户名:密码,存储的信息是经过简单的加密,当然使用的可逆的加密算法,如下所示每行代表一个IP信息:

 

1
2
MTkyLjE2OC4xMDAuMjAxOjIyOnJvb3Q6MTIzNDU2
MTkyLjE2OC4xMDAuMjAyOjIyOnJvb3Q6MTIzNDU2

 

2.paramiko.log

  该文件是paramiko模块在运行过程中产生的日志文件

 

3.command_history.log

  该文件是记录执行过的命令

 

4.command_result_history.log

  该文件是记录执行过的命令的结果

 

五、怎么执行脚本?

 

1. 使用 python 脚本名 方式来执行

 

1
python EasyXMS.py

 

2. 使用 脚本的绝对路径来执行,注意加上可执行权限

 

1
./EasyXMS.py

 

  如果出现以下情况,使用dos2unix转换一下即可

 

  如果没有dos2unix,那么在Windows上使用像EmEditor这类的文本编辑器(不要使用Windows自带的记事本),把换行符修改为仅Unix,即可

 

六、操作演示

 

点击这里看动态演示

 

1.主菜单

 

输入? 即可获得主菜单帮助

 

 

功能描述:

1
2
3
4
5
6
7
8
0 增加服务器的IP信息到配置文件(server.conf 以下都指的是该配置文件)
1 加载一个包含多个服务器IP信息的文件,用于批量添加服务器到
2 列出当前配置文件中存在的服务器
3 从配置文件中删除指定的服务器信息
4 清空配置文件
5 批量执行命令
6 批量上传文件
7 清屏

 

2.选项 0 增加服务器的IP信息到配置文件

 

3.选项1 加载一个包含多个服务器IP信息的文件

 

准备一个文本文件,里面的包含这些内容 IP地址:端口(22端口可以不写):用户名:密码 每行一个 例如:

192.168.100.204:root:123456

192.168.100.205:root:123

192.168.100.206:root:123

 

4.选项 2  列出当前配置文件中存在的服务器

 

 

5.选项 3 从配置文件中删除指定的服务器信息

 

 

6.选项 4 清空配置文件

 

 

7.选项 5 批量执行命令

 

 

8.选项 6 批量上传文件

 

 

9.选项 7 清屏

 

 

七、注意事项

 

1.不能使用vi或者vim编辑远程文件

 

2.目前不支持tab键补全命令

 

3.在按 Backspace 键出现以下情况时

可用如下方式来解决:

 

如果你使用的SecureCRT,可以这样解决

 

如果使用的是Xshell,可以这样解决:

 

putty 使用下来没这个问题,但是如果有这个问题,找到类似设置的地方 设置一下即可

 

八.脚本内容

 

#!/usr/bin/env python
# coding=utf-8
#---------------------------------------------------------------------
# Name:         EasyXMS
# Purpose:      Multithreading Batch Execution Commands and Upload Files
# Version:      1.0
# Author:       LEO
# BLOG:         http://linux5588.blog.51cto.com
# EMAIL:        chanyipiaomiao@163.com
# Created:      2013-7-29
# Copyright:    (c) LEO 2013
#---------------------------------------------------------------------
frombase64 importencodestring,decodestring
fromos.path importbasename,exists,isfile
fromsys importexit,platform
fromtime importstrftime
fromQueue importQueue
fromos importsystem
importthreading
importparamiko
# 控制输出类
classPrintHelp(object):
def__init__(self):
"""定义命令列表"""
self.cmd_list =[' Add      Server ',
' Load     File',
' List     Servers ',
' Delete   Server ',
' Empty    File',
' Execute  Command ',
' Upload   File ',
' Clear    Screen',
]
self.name ='EasyXMS'
self.example ='*** Example *** : '
self.example_ip ='%s192.168.1.1:22:root:123456 192.168.1.3:test:123456 '%self.example
self.example_delete ='%s192.168.1.1 192.168.1.2 '%self.example
self.example_filepath ='%s: /tmp/1.txt /tmp'%self.example
defprintPrompt(self):
"""脚本运行提示符"""
return"%s( ? Help ) >>> "%self.name
defloopPrintCmd(self):
"""循环打印命令列表"""
print
print"Please Choose A Number : "
print
fori, v inenumerate(self.cmd_list):
print"%5s %-5s"%(i, v)
print
defprintHead(self):
"""输出头部提示信息"""
print_string ='Welcome to Use < %s > , Please Input < ? > '\
'Get Help Or Direct < Enter > Exit'%self.name
string_temp ="-"*len(print_string)
printstring_temp
print
printprint_string
print
printstring_temp
print
defwantQuit(self):
"""退出提示"""
return"Want Quit? (y/n) [y|Enter]: "
defprintInputWrong(self):
"""提示命令输入有误"""
print
print"Wrong Input,Enter < ? > Get Help Or Enter Exit ! "
print
defprintFileError(self, e):
"""提示读取主机配置文件出错"""
print"Oops! Read File Occur Wrong : %s"%e
defprintFileNotExistOrEmpty(self):
"""提示主机配置文件不存在或者为空,请创建或者添加IP地址到文件"""
print
print"The File [ %s ] Not Exist Or Empty,"\
" Enter < 0/1 > to Create Or Add Server IP."%configfile
print
defprintInvalidIP(self,info):
"""提示无效的信息"""
print"Invalid Format [ %s ] !!! "%info
print
defprintIPFormat(self):
"""添加服务器IP地址的格式"""
print
printself.example_ip
defprintDeleteIPFormat(self):
"""删除服务器IP时的格式"""
print
printself.example_delete
print
defprintInputIP(self):
"""提示输入IP地址以空格分开"""
return"Enter Server Info : "
defprintInputIPForDelete(self):
"""提示输入服务器的IP地址"""
return"Enter Server IP (Only IP Address) : "
defprintAddIPSuccessful(self, ip):
"""提示增加服务器IP到配置文件成功"""
print"Add Server [ %s ] to [ %s ] Successful !!! "%(ip, configfile)
print
defprintRemoveIPSuccessful(self,ip):
"""提示从配置文件中删除IP地址成功"""
print"Remove [ %s ] From [ %s ] Successful !!!"%(ip,configfile)
print
defprintIPNotInFile(self, ip):
"""输出IP地址不在配置文件中"""
print"%s is Not in [ %s ]"%(ip, configfile)
print
defprintIPAlreadyInFile(self, ip):
"""输出IP地址已经在配置文件中"""
print"IP [ %s ] Already in [ %s ] !!!"%(ip, configfile)
print
defyouSureEmptyFile(self):
"""提示用户是否确认清空文件"""
return"Are You Sure Empty [ %s ] (y/n) : "%configfile
defemptySuccessful(self):
"""输出清空文件成功"""
print"Empty Config File [ %s ] Successful !!!"%configfile
print
defemptyFileFailure(self, e):
"""输出清空文件失败"""
print"Empyt File %s Failure !!! ( %s )"%(configfile, e)
defenterCommand(self):
"""提示输入命令"""
return"Enter Command( q|Q Quit ) : "
defenterFilePath(self):
"""提示输入文件的路径"""
return"Enter [ Local ] Path And < Remote > Path : "
defenterFilePath2(self):
"""加载文件时,提示输入文件路径"""
return"Enter File Path( File Include ip:username:password ) : "
definvaildCommand(self):
"""输出无效的命令"""
print"Invaild Command !!!"
print
definvaildFilePath(self,filepath):
"""无效的文件路径"""
print"Invaild File Path ' %s ' !!!"%filepath
print
defprintSSHBePatient(self):
"""输出正在初始化SSH连接,请耐心等待"""
print"Initializing < SSH > Connections, Please Be Patient ..."
print
defprintSFTPBePatient(self):
"""输出正在初始化SFTP连接,请耐心等待"""
print"Initializing < SFTP > Connections, Please Be Patient ..."
print
defprintCommandResult(self,ip,cmd,out,error):
"""输出命令执行的结果"""
result ="======== [ %s ] Execute Command: ' %s ',The Result is : \n\n%s%s"%(ip,cmd,out,error)
printresult
returnresult
defprintCanNotConnectIP(self,ip):
"""输出不能连接到该IP地址"""
print"[ Error ] ... Can't Connect This IP ' %s ', Please Check !!!"%ip
print
defuploadFileOK(self, src, ip):
"""上传文件成功"""
print"Upload File < %s > to %s ... OK "%(src, ip)
print
defuploadFileError(self, src, ip, e):
"""上传文件出现错误"""
print"Upload File < %s > to %s ... Error: %s "%(src, ip, e)
print
defprintFilePath(self):
"""输出上传文件时路径的例子"""
print
printself.example_filepath
defprintArgsNotenough(self):
"""输出参数不够提示"""
print"Arguments Not Enough !!!"
print
defgetDateTime(self):
"""获取日期和时间"""
returnstrftime('%Y-%m-%d %H:%M:%S')
defreturnDateString(self):
"""返回一个日期加星号的字符串用于标识命令执行的结果"""
return'\n********** %s **********\n\n'%self.getDateTime()
defprintCannotCreateFile(self,filename,error):
"""提示无法创建文件"""
print
print"Can't Create < %s > !!! : %s"%(filename, error)
print
# 控制输入类
classInputValue(object):
def__init__(self):
self.inputValue =None
defsetInputvalue(self):
"""提示输入命令"""
self.inputValue =raw_input(printhelp_obj.printPrompt())
returnself.inputValue
defcloseConnections(self):
"""关闭连接函数"""
ssh_connect_pool_dict =connectSSH_obj.getConnectionPool()
sftp_connect_pool_dict =connectSFTP_obj.getConnectionPool()
ifssh_connect_pool_dict:
fori inssh_connect_pool_dict:
ssh_connect_pool_dict[i].close()
ifsftp_connect_pool_dict:
forj insftp_connect_pool_dict:
sftp_connect_pool_dict[j].close()
defexitFunction(self):
"""退出本程序"""
try:
print
quitp =raw_input(printhelp_obj.wantQuit())
print
exceptEOFError:
self.closeConnections()
print
print
exit(0)
ifnotquitp orquitp =='y'orquitp =='Y':
self.closeConnections()
exit(0)
else:
self.loopGetValue()
defloopGetValue(self):
"""循环得到用户的输入,并判断"""
try:
whileself.setInputvalue():
ifself.inputValue =='?':
printhelp_obj.loopPrintCmd()
elifself.inputValue =='0':
readwriteconfigFile_obj.writeIPToFile()
elifself.inputValue =='1':
readwriteconfigFile_obj.loadFile()
elifself.inputValue =='2':
readwriteconfigFile_obj.readIPFromFile()
elifself.inputValue =='3':
readwriteconfigFile_obj.deleteIPFromFile()
elifself.inputValue =='4':
readwriteconfigFile_obj.emptyConfigFile()
elifself.inputValue =='5':
startExecAction_obj.startExecCommand()
elifself.inputValue =='6':
startExecAction_obj.startSFTP()
elifself.inputValue =='7':
startExecAction_obj.clearScreen()
else:
printhelp_obj.printInputWrong()
else:
self.exitFunction()
exceptEOFError :
print
self.exitFunction()
exceptKeyboardInterrupt:
print
self.exitFunction()
# 操作配置文件类
classReadWriteConfigFile(object):
defreadIPAsDict(self):
"""读取所有的IP地址为一个字典
像这样: {'192.168.1.1':{'port':22,'user':root,'pwd':123456}, }
"""
ip_dict ={}
try:
data =open(configfile,'r')
config_file =data.readlines()
ifconfig_file:
fori inconfig_file:
i =decodestring(i)
tempstring =i.split(':')
ip =tempstring[0]
ifip notinip_dict:
ip_dict[ip] ={'port':int(tempstring[1]),
'user':tempstring[2],
'pwd':tempstring[3]}
except:
pass
# 如果文件句柄被打开过,则关闭
try:
data.close()
except:
pass
returnip_dict
defreadIPFromFile(self):
"""返回一个IP地址列表"""
ip_list =self.readIPAsDict().keys()
ifip_list:
print
fori inip_list:
printi
print
else:
printhelp_obj.printFileNotExistOrEmpty()
deffileAppendObject(self,filename):
"""以追加方式打开文件并返回对象"""
data =None
error =None
try:
data =open(filename,'a')
exceptIOError,e:
error =e
returndata,error
defwriteFile(self,data,server_list):
"""写文件函数,用于写服务器的配置信息"""
ip_list =self.readIPAsDict().keys()
forip_info inserver_list:
ip,info =self.checkIPInfo(ip_info)
ifip andinfo:
ifip notinip_list:
data.write(encodestring(info))
printhelp_obj.printAddIPSuccessful(ip)
else:
printhelp_obj.printIPAlreadyInFile(ip)
defloadFile(self):
"""加载一个包含 IP地址:端口:用户名:密码 的文件批量进行添加"""
data2,error =self.fileAppendObject(configfile)
ifdata2:
print
filename =raw_input(printhelp_obj.enterFilePath2())
print
iffilename:
try:
data =open(filename,'r')
file_ip_list =data.readlines()
data.close()
self.writeFile(data2, file_ip_list)
exceptIOError,e:
printhelp_obj.printFileError(e)
print
data2.close()
else:
printhelp_obj.printArgsNotenough()
else:
printhelp_obj.printCannotCreateFile(configfile,error)
defcheckIPInfo(self,ipinfo):
"""简单的检查输入的IP等信息格式是否正确"""
ip,info =None,None
maohao_num =ipinfo.count(':')
ifmaohao_num in(2,3):
ifipinfo.count('.') ==3:
info_list =ipinfo.split(':')
ifmaohao_num ==3:
ip, port, user, passwd =info_list
ifmaohao_num ==2:
ip, user, passwd =info_list
port =22
ifpasswd:
info ='%s:%s:%s:%s'%(ip, port, user, passwd)
else:
printhelp_obj.printInvalidIP(ipinfo)
else:
printhelp_obj.printInvalidIP(ipinfo)
else:
printhelp_obj.printInvalidIP(ipinfo)
returnip,info
defwriteIPToFile(self):
"""写入IP地址到配置文件"""
data,error =self.fileAppendObject(configfile)
ifdata:
printhelp_obj.printIPFormat()
print
hosts =raw_input(printhelp_obj.printInputIP())
print
ifnothosts:
printhelp_obj.printArgsNotenough()
else:
server_list =hosts.split()
self.writeFile(data,server_list)
data.close()
else:
printhelp_obj.printCannotCreateFile(configfile, error)
defdeleteIPFromFile(self):
"""从配置文件中删除指定的IP地址"""
ip_from_configfile_dict =self.readIPAsDict()
ifip_from_configfile_dict:
printhelp_obj.printDeleteIPFormat()
hosts =raw_input(printhelp_obj.printInputIPForDelete())
print
iflen(hosts) ==0:
printhelp_obj.printArgsNotenough()
else:
delete_ip_list =hosts.split()
fori indelete_ip_list:
ifi inip_from_configfile_dict:
delip_from_configfile_dict[i]
printhelp_obj.printRemoveIPSuccessful(i)
else:
printhelp_obj.printIPNotInFile(i)
ifip_from_configfile_dict:
try:
data =open(configfile, 'w')
forkey,value inip_from_configfile_dict.items():
tempstring ="%s:%s:%s:%s"%(key, value['port'], value['user'], value['pwd'])
data.write(encodestring(tempstring))
exceptIOError, e:
print
printhelp_obj.printFileError(e)
print
else:
data =open(configfile, 'w')
try:
data.close()
except:
pass
else:
printhelp_obj.printFileNotExistOrEmpty()
defemptyConfigFile(self):
"""清空整个主机配置文件"""
ifself.readIPAsDict():
print
ok =raw_input(printhelp_obj.youSureEmptyFile())
print
ifok =='y'orok =='Y':
try:
data =open(configfile, 'w')
exceptIOError,e:
printhelp_obj.emptyFileFailure(e)
# 如果文件句柄被打开过,则关闭
try:
data.close()
printhelp_obj.emptySuccessful()
except:
pass
else:
printhelp_obj.printFileNotExistOrEmpty()
defreadCommandHistory(self):
"""读取历史执行命令"""
command_history_list =[]
try:
data =open(command_history,'r')
forcmd indata.readlines():
command_history_list.append(cmd.strip('\n'))
data.close()
exceptIOError:
pass
returncommand_history_list
defwriteCommandToFile(self,cmd):
"""命令写入到文件"""
command_history_list =self.readCommandHistory()
data,error =self.fileAppendObject(command_history)
ifdata:
ifcmd notincommand_history_list:
data.write(cmd+'\n')
data.close()
else:
printhelp_obj.printCannotCreateFile(command_history, error)
defwriteCommandResultToFile(self, result_list):
"""命令输出结果写入到文件"""
data,error =self.fileAppendObject(command_result_history)
ifdata:
data.write(printhelp_obj.returnDateString())
data.write(''.join(result_list))
data.write('\n')
data.close()
else:
printhelp_obj.printCannotCreateFile(command_result_history,error)
# 连接SSH和SFTP类的父类
classConnect(object):
def__init__(self):
self.connect_pool_dict ={}
self.ip_list =[]     
self.thread_num =None
defgetIPList(self):
"""获取SSH/SFTP连接成功的IP地址"""
returnself.ip_list
defgetThreadNum(self):
"""获取到线程数量,是通过计算IP地址数量得到"""
returnself.thread_num
defgetConnectionPool(self):
"""获取SFTP/SSH连接池信息"""
returnself.connect_pool_dict
# 连接SSH类
classConnectSSH(Connect):
defconnectSSH(self):
"""连接到SSH服务"""
paramiko.util.log_to_file(EasyXMS_log)
server_dict =readwriteconfigFile_obj.readIPAsDict() # 首先获得ServerIP地址列表
ifserver_dict:
# 这一段就是判断,在ssh的connect_pool_dict这里面现存的IP地址列表跟从配置文件读取出来的列表是否一致
# 如果不一致,那么以配置文件读取出来的IP地址列表为准,删除多余的连接
ifself.connect_pool_dict:
connect_pool_list =self.connect_pool_dict.keys()
fori inconnect_pool_list:
ifi notinserver_dict:
delself.connect_pool_dict[i]
self.ip_list.remove(i)
# 这一段是增加 成功进行SSH连接的IP地址到self.connect_pool_dict中去,同时也增加到成功IP地址列表去
forip,value inserver_dict.items():
ifip notinself.connect_pool_dict:
conn =paramiko.SSHClient()
conn.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
conn.connect(ip,value['port'],value['user'],value['pwd'], timeout=0.8)
self.connect_pool_dict[ip] =conn
self.ip_list.append(ip)
except:
printhelp_obj.printCanNotConnectIP(ip)
self.thread_num =len(self.ip_list)
else:
printhelp_obj.printFileNotExistOrEmpty()
defsetCommand(self,command):
"""设定命令行输入的命令"""
self.command =command
defgetCommand(self):
"""返回命令"""
returnself.command
# 连接到SFTP类
classConnectSFTP(Connect):
defconnectSFTP(self):
"""连接到SFTP服务"""
paramiko.util.log_to_file(EasyXMS_log)
server_dict =readwriteconfigFile_obj.readIPAsDict() # 首先获得ServerIP地址列表
ifserver_dict:
# 这一段就是判断,在sftp的connect_pool_dict这里面现存的IP地址列表跟从配置文件读取出来的列表是否一致
# 如果不一致,那么以配置文件读取出来的IP地址列表为准,删除多余的连接
ifself.connect_pool_dict:
connect_pool_list =self.connect_pool_dict.keys()
fori inconnect_pool_list:
ifi notinserver_dict:
delself.connect_pool_dict[i]
self.ip_list.remove(i)
# 这一段是增加 成功进行SFTP连接的IP地址到self.connect_pool_dict中去,同时也增加到成功IP地址列表去
forip,value inserver_dict.items():
ifip notinself.connect_pool_dict:
conn =paramiko.Transport((ip, value['port']))
try:
conn.connect(username=value['user'], password=value['pwd'])
sftp =paramiko.SFTPClient.from_transport(conn)
self.connect_pool_dict[ip] =sftp
self.ip_list.append(ip)
except:
printhelp_obj.printCanNotConnectIP(ip)
self.thread_num =len(self.ip_list)
else:
printhelp_obj.printFileNotExistOrEmpty()
defgetDirectoryName(self, filename, dest):
"""拼接目标文件名"""
ifdest.endswith('/'):
dest ='%s/%s'%(dest.rstrip('/'), filename)
else:
dest ='%s/%s'%(dest, filename)
returndest
defsetFilePath(self,filepath):
"""设定当前命令行输入的文件路径"""
self.filepath =filepath
defgetFilePath(self):
"""返回命令行输入的文件路径"""
returnself.filepath
# 多线程类
classMutilThreadControl(threading.Thread):
defsetConnectionPool(self, pool):
"""设置使用那个连接池 有SSH和SFTP连接池"""
self.init_pool =pool
definitIPQueueAndtConnectionPool(self):
"""初始一个队列并把IP地址放入队列,返回IP地址列表的长度(指定产生线程的数量)和队列"""
ifself.init_pool =='ssh':
ip_list =connectSSH_obj.getIPList()
connect_pool_dict =connectSSH_obj.getConnectionPool()
elifself.init_pool =='sftp':
ip_list =connectSFTP_obj.getIPList()
connect_pool_dict =connectSFTP_obj.getConnectionPool()
thread_num =len(ip_list)
ip_queue =Queue(thread_num)
forip inip_list:
ip_queue.put(ip)
returnip_queue, thread_num, connect_pool_dict
defrun(self):
"""开始多线程执行命令和SFTP上传文件"""
ip_queue, threads_num, connect_pool_dict =self.initIPQueueAndtConnectionPool()
result_list =[]
ifself.init_pool =='ssh':
cmd =connectSSH_obj.getCommand()
fori inxrange(threads_num):
ip =ip_queue.get()
stdin,stdout,stderr =connect_pool_dict[ip].exec_command(cmd)
result =printhelp_obj.printCommandResult(ip, cmd, stdout.read(), stderr.read())
result_list.append(result)
readwriteconfigFile_obj.writeCommandResultToFile(result_list)
elifself.init_pool =='sftp':
file_path =connectSFTP_obj.getFilePath().split()
iflen(file_path) ==2:
src,dest =file_path
ifexists(src) andisfile(src):
filename =basename(src)
dest =connectSFTP_obj.getDirectoryName(filename, dest)
fori inxrange(threads_num):
ip =ip_queue.get()
try:
connect_pool_dict[ip].put(src, dest)
printhelp_obj.uploadFileOK(src, ip)
exceptIOError, e:
printhelp_obj.uploadFileError(src, ip, e)
else:
printhelp_obj.invaildFilePath(src)
else:
printhelp_obj.printArgsNotenough()
# 执行指定的动作(执行命令 上传文件 清屏)
classStartExecAction(object):
defstartExecCommand(self):
"""开始多线程执行命令"""
mutilThreadControl_ssh_obj =MutilThreadControl()
mutilThreadControl_ssh_obj.setConnectionPool('ssh')
ifreadwriteconfigFile_obj.readIPAsDict():
print
cmd =raw_input(printhelp_obj.enterCommand())
print
ifcmd:
ifcmd =='q'orcmd =='Q':
pass
else:
readwriteconfigFile_obj.writeCommandToFile(cmd)
connectSSH_obj.setCommand(cmd)             
ifnotconnectSSH_obj.getConnectionPool(): 
printhelp_obj.printSSHBePatient()      
connectSSH_obj.connectSSH()                
mutilThreadControl_ssh_obj.start()        
mutilThreadControl_ssh_obj.join()         
self.startExecCommand()                    
else:
printhelp_obj.invaildCommand()
else:
printhelp_obj.printFileNotExistOrEmpty()
defstartSFTP(self):
"""开始上传文件"""
mutilThreadControl_sftp_obj =MutilThreadControl()
mutilThreadControl_sftp_obj.setConnectionPool('sftp')  
ifreadwriteconfigFile_obj.readIPAsDict():
printhelp_obj.printFilePath()
print
filepath =raw_input(printhelp_obj.enterFilePath())
print
iffilepath:
connectSFTP_obj.setFilePath(filepath)        
ifnotconnectSFTP_obj.getConnectionPool():
printhelp_obj.printSFTPBePatient()       
connectSFTP_obj.connectSFTP()                  
mutilThreadControl_sftp_obj.start()           
mutilThreadControl_sftp_obj.join()            
else:
printhelp_obj.printArgsNotenough()
else:
printhelp_obj.printFileNotExistOrEmpty()
defclearScreen(self):
"""清屏"""
clear_screen ={'win32':'cls','linux2':'clear',
'linux':'clear','darwin':'clear'}[platform]
system(clear_screen)
if__name__ =='__main__':
configfile ='server.conf'
EasyXMS_log ='paramiko.log'
command_history ='command_history.log'
command_result_history ='command_result_history.log'
printhelp_obj =PrintHelp()                        
printhelp_obj.printHead()                      
readwriteconfigFile_obj =ReadWriteConfigFile()   
connectSSH_obj =ConnectSSH()                      
connectSFTP_obj =ConnectSFTP()              
startExecAction_obj =StartExecAction()            
input_obj =InputValue()                          
input_obj.loopGetValue()

 

出自 “雷纳科斯的博客” 博客,请务必保留此出处http://linux5588.blog.51cto.com/65280/1293677

(责任编辑:IT)