python批量上传_python实现批量远程执行命令及批量上传下载文件

#!/usr/bin/env python#-*- coding: utf-8 -*-#@Time : 2018/1/23 9:34#@Author : xuxuedong#@Site :#@File : ArgvHandler.py#@Software: PyCharm

importos, sys,datetime,paramiko,time,traceback,threading,loggingimportcommands,subprocessfrom logging importhandlersclassArgvHandler(object):def __init__(self,argv_list):

self.argvs=argv_list

self.parse_argv()defparse_argv(self):if len(self.argvs)>5:#if hasattr(self,str(self.argvs[1])):

#func = getattr(self,str(self.argvs[1]))

#func()

self.argv_ckeck()print("get_status",self.servers_file,self.local,self.remote,self.cmd)

self.server_get()#else:

#self.help_msg()

elif len(self.argvs)== 4:

self.argv_ckeck()print("cmd",self.cmd )

self.cmd= self.cmd.strip().split(',')print("cmd_list",self.cmd)

self.server_get()#else:

else:

self.help_msg()defserver_get(self):

self.get_servers(self.servers_file)print("self.SERVERS",self.SERVERS)#self.ssh_connect(self.SERVERS)

self.val =[]for server inself.SERVERS:print("server_list",server['host'])

self.ip= server['host']

user= server['user']

passwd= server['passwd']

port= server['port']print("val",self.ip,user,passwd,port)

t1= threading.Thread(target=self.ssh_connect,args=[self.ip,port,user,passwd,self.cmd])

t1.start()

t1.join()#print("getname:",t1.getName())

#self.val.append(t1)

#for i in self.val:

#print("i",i)

#i.join()

#print("---main---")

#print("self.val",self.val)

#return self.val

deflogger(self,log_message):

self.log= logging.getLogger(__name__)

self.log.setLevel(logging.INFO)#create console handler and set level to debug

ch =logging.StreamHandler()

ch.setLevel(logging.INFO)

ti= time.strftime('%Y-%m-%d',time.localtime(time.time()))

log_file= os.getcwd()+"/"+"logs/"+ti+"-"+"access.log"

#create file handler an"d set level to warning

iflog_file:

self.fh=logging.FileHandler(log_file)

self.fh.setLevel(logging.WARNING)else:

self.fh= handlers.TimedRotatingFileHandler(filename=log_file,when="S",interval=5,backupCount=3)

self.fh.setLevel(logging.WARNING)#create formatter

self.formatter = logging.Formatter('%(asctime)s %(module)s:%(lineno)d %(message)s')

self.fh.setFormatter(self.formatter)

self.log.addHandler(self.fh)

self.log.error(log_message)

self.log.warning(log_message)

self.log.info(log_message)defhelp_msg(self):

msg= '''-f

1.upload file use "up"

example: python cmd.py -f server.txt upload_file_name remote_file_name up

2.download file use "get"

example: python cmd.py -f server.txt download_file_name remote_file_name up

3.remote_file_name must path

example :

python cmd.py -f server.txt auto.mysql.sh /service/script/aouto.mysql.sh get

python cmd.py -f server.txt auto.mysql.sh /service/script/aouto.mysql.sh up

-c

1.Host batch execution command

example: python cmd.py -c server.txt "ls -l /service/script,uname -a"

server.text format:lists_format:192.168.223.128,root,123456,22,1(ip,user,passwd,port,status)'''

print(msg)defargv_ckeck(self):

logs_path= os.getcwd() + u'/logs'

ifos.path.exists(logs_path):ifos.path.isfile(logs_path):print u"日志目录" + logs_path + u"存在,但是文件,终止"sys.exit(1)else:

os.mkdir(logs_path)if sys.argv[1] == "-f":

self.servers_file= sys.argv[2]

self.local= sys.argv[3]

self.remote= sys.argv[4]

self.cmd= sys.argv[5]if sys.argv[1] == "-c":

self.servers_file= sys.argv[2]

self.cmd= sys.argv[3]print(self.cmd)#except:

#pass

## self.help_msg()

#

defget_servers(self,file):print("file:",file)

self.SERVERS=[]try:

with open(file,'r') as file:whileTrue:

line=file.readline()if notline:break

try:

res= line.strip().split(',')

server={'host': res[0],'user': res[1],'passwd': res[2],'port': int(res[3]),#'status': int(res[4])

}

self.SERVERS.append(server)except:

self.logger(self.SERVERS)returnself.SERVERSexcept:pass

#out_log = u"failed to read server lists file:{0}\n{1}".format(file, traceback.format_exc())

#self.logger(out_log, 'error')

#sys.exit(1)

defupload__callback(self,trans, total):#sys.stdout.write('Data Transmission %10d [%3.2f%%]\r' %(a, a*100/int(b)))

#sys.stdout.flush()

print 'Data upload %10d [%3.2f%%]\r' % (trans, trans*100/int(total)),;if trans >=total:

self.logger('Data Transmission %10d [100.00%%]' % trans, 'info', False)#和上面一样:作用始终保持同一行,即新打印出来的一行,替换掉之前的那行,保持同一行,不换行。

defdownload__callback(self,trans, total):#sys.stdout.write('Data Transmission %10d [%3.2f%%]\r' %(a, a*100/int(b)))

#sys.stdout.flush()

print 'Data download %10d [%3.2f%%]\r' % (trans, trans*100/int(total)),;if trans >=total:

self.logger('Data Transmission %10d [100.00%%]' % trans, 'info', False)#和上面一样:作用始终保持同一行,即新打印出来的一行,替换掉之前的那行,保持同一行,不换行。

defssh_connect(self,host, port, user, passwd,status):

self.ssh=paramiko.SSHClient()try:#paramiko.util.log_to_file('logs/params.log')

self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #允许连接不在know_hosts文件中的主机

#ssh.connect(host, port, user, passwd, timeout=5, banner_timeout=5, auth_timeout=5)

self.ssh.connect(host, port, user, passwd, timeout=3, banner_timeout=3)#print("self.ssh",self.ssh)

if status=='up':

obj= getattr(self,'up'+ '_file')

obj(self.ssh,self.local,self.remote)print("obj",obj)print("status",status)elif status=='get':

obj= getattr(self,'get'+ '_file')

obj(self.ssh,self.ip,self.remote)print("obj",obj)print("status",status)else:print("command is:",status)for item instatus:print("item",item)

stdin, stdout, stderr=self.ssh.exec_command(item)

self.err_list=stderr.readlines()for item instdout.readlines():#print item,

printitemif len( self.err_list ) >0:print 'ERROR:' +self.err_list[0]

exit()exceptException as e:

out_log= u'ip: {0}\tuser:{1}\ttimeout'.format(host,user)

err_log=traceback.format_exc()

out_log= u'{0}\n{1}'.format(out_log, err_log)

self.logger(out_log)#exit()

#self.ssh.close()

#return self.ssh

#def ssh_exec_cmd(self,ssh,cmd ):

#return self.ssh.exec_command(self,cmd)

defup_file(self,ssh, localfile, remotefile):try:print("obj is up")

sftp=ssh.open_sftp()print("localfile",localfile)print("remotefile",remotefile)

sftp.put(localfile, remotefile)

sftp.close()except:

out_log= u'ip: {0}\tuser:{1}\ttimeout'.format(ssh,remotefile)

err_log=traceback.format_exc()

out_log= u'{0}\n{1}'.format(out_log, err_log)

self.logger(out_log)#self.ssh.close()

defget_file(self,ssh, ip, remotefile):if len(remotefile.split('/', 10 )) > 2:

self.File= remotefile.split('/', 10 )[len(remotefile.split('/', 10 ))-1]print("FILE",self.File)try:

sftp=ssh.open_sftp()

sftp.get(remotefile,self.ip+self.File)

sftp.close()except:

out_log= u'local: {0}\tremot:{1}\ttimeout'.format(self.ip,remotefile)

err_log=traceback.format_exc()

ut_log= u'{0}\n{1}'.format(out_log, err_log)

self.logger(out_log)#self.ssh.clos

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值