python批量操作Linux服務器腳本,ssh密碼登錄(執行命令、上傳、下載)(一)


 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()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM