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