1 #-*- coding: utf-8 -*-
2 #批量操作linux服务器(执行命令,上传,下载)
3 #!/usr/bin/python
4 import paramiko
5 import datetime
6 import os
7 import threading
8 def ssh2(ip,username,passwd,cmd):
9 try:
10 paramiko.util.log_to_file('paramiko________.log')
11 ssh = paramiko.SSHClient()
12 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
13 ssh.connect(ip,22,username,passwd,timeout=5)
14 for m in cmd:
15 stdin,stdout,stderr = ssh.exec_command(m)
16 #stdin.write("Y") #简单交互,输入 ‘Y’
17 out = stdout.readlines()
18 # outerr = stderr.readlines()
19 #屏幕输出
20 for o in out:
21 print o,
22 # for i in outerr:
23 # print i,
24 print '%s\tOK\n'%(ip)
25 ssh.close()
26 except :
27 print '%s\tError\n'%(ip)
28 def download(ip, username, passwd, local_dir, remote_dir):
29 try:
30 paramiko.util.log_to_file('paramiko_download.log')
31 t = paramiko.Transport((ip,22))
32 t.connect(username=username,password=passwd)
33 sftp = paramiko.SFTPClient.from_transport(t)
34 files = sftp.listdir(remote_dir)
35
36
37 for f in files:
38 print ''
39 print '############################'
40 print 'Beginning to download file from %s %s ' % (ip, datetime.datetime.now())
41 print 'Downloading file:', os.path.join(remote_dir, f)
42 sftp.get(os.path.join(remote_dir, f), os.path.join(local_dir, f))#下载
43 print 'Download file success %s ' % datetime.datetime.now()
44 print ''
45 print '############################'
46 t.close()
47 except:
48 print "connect error!"
49
50
51 def upload(ip, username, passwd, local_dir, remote_dir):
52 try:
53 paramiko.util.log_to_file('paramiko_upload.log')
54 t = paramiko.Transport((ip, 22))
55 t.connect(username=username, password=passwd)
56 sftp = paramiko.SFTPClient.from_transport(t)
57 #files = sftp.listdir(remote_dir)
58 files = os.listdir(local_dir)
59 for f in files:
60 print ''
61 print '############################'
62 print 'Beginning to upload file to %s %s ' % (ip, datetime.datetime.now())
63 print 'Uploading file:', os.path.join(local_dir, f)
64 sftp.put(os.path.join(local_dir, f), os.path.join(remote_dir, f))#上传
65 print 'Upload file success %s ' % datetime.datetime.now()
66 print ''
67 print '############################'
68 t.close()
69 except:
70 print "connect error!"
71 print ip, "fail!"
72
73
74 if __name__=='__main__':
75 # cmd = ['ls -lh /export/servers/mysql/log/mysql.log']#你要执行的命令列表
76 cmds=open("D:\\batch\\cmds.txt") #从文件读取命令
77 cmd=cmds.readlines()
78 cmds.close()
79 username = "root" #用户名
80 #passwd = "xxxx" #单台服务器时启用----------------------
81 #ip='192.168.12.19' #单台服务器时启用----------------------
82 local_dir = "D:\\batch\\tmp" #本地服务器目录
83 remote_dir = "/tmp/" #远程服务器目录
84 #threads = [] #多线程
85 print "Begin......"
86 hosts=open("D:\\batch\\list.txt") #本地服务器列表
87 host=hosts.readlines()
88 hosts.close()
89 for list in host:
90 line = list.split()
91 ip = line[0].strip()
92 pwd = line[1].strip()
93 # print ip,
94 # print pwd,
95 # b=threading.Thread(target=upload,args=(ip,username,pwd,local_dir,remote_dir))
96 # b.start()
97 a=threading.Thread(target=ssh2,args=(ip,username,pwd,cmd))
98 a.start()