聪明bo

导航

ftp批量上传、删除、检查~~~

  1 import os
  2 import sys
  3 import xlrd
  4 import socket
  5 import subprocess
  6 from ftplib import FTP
  7 from tqdm import tqdm
  8 socket.setdefaulttimeout(5)
  9 if not os.path.exists(os.getcwd()+"\\麦田网络运维信息档案.xls"):
 10     print('程序所在目录必须放个"麦田网络运维信息档案.xls"')
 11     input()
 12     sys.exit()
 13 excel = xlrd.open_workbook('麦田网络运维信息档案.xls')
 14 table = excel.sheets()[0]
 15 ip_dic = {}
 16 for i in range(table.nrows):
 17     tmp = []
 18     if len(table.cell(i, 3).value.strip().split('.')) == 4 and "已撤" not in table.cell(i, 1).value.strip():
 19         tmp.append(table.cell(i, 1).value.strip())
 20         ip = table.cell(i, 3).value.strip()
 21         ip = ip.split('.')
 22         ip = ip[0:3]
 23         ip.append('252')
 24         ip = '.'.join(ip)
 25         tmp.append(ip)
 26         ip_dic[i] = tmp
 27     else:
 28         continue
 29 
 30 
 31 def ping(ip):
 32     ret = subprocess.getoutput('ping -n 2 -l 0 -w 1000 {}'.format(ip))
 33     return 'ms' in ret
 34 
 35 def ftpconnect(host, username, password):
 36     ftp = FTP()
 37     ftp.encoding = 'utf-8'
 38     ftp.connect(host, 21)
 39     ftp.login(username, password)
 40     file_list = []
 41     ftp.dir("", file_list.append)
 42     filename = '\n'.join(file_list)
 43     if ' 共享文件夹' in filename:
 44         ftp.cwd('/共享文件夹')
 45     elif ' 店内共享' in filename:
 46         ftp.cwd('/店内共享')
 47     elif ' 店内文件共享' in filename:
 48         ftp.cwd('/店内文件共享')
 49     else:
 50         raise RuntimeError('directory error')
 51     return ftp
 52 
 53 
 54 def uploadfile(ftp, remotepath, localpath):
 55     bufsize = 1024
 56     fp = open(localpath, 'rb')
 57     ftp.storbinary('STOR '+remotepath, fp, bufsize)
 58     ftp.set_debuglevel(2)
 59     fp.close()
 60 
 61 
 62 def up_start(ip, filename):
 63     ftp = ftpconnect(ip, "admin", "admin")
 64     file_list = []
 65     ftp.dir("", file_list.append)
 66     ll = '\n'.join(file_list)
 67     if ' '+filename in ll:
 68         ftp.close()
 69         return
 70     uploadfile(ftp, "./" + filename, os.getcwd()+"\\"+filename)
 71     ftp.close()
 72 
 73 
 74 def check_start(ip, filename):
 75     ftp = ftpconnect(ip, "admin", "admin")
 76     file_list = []
 77     ftp.dir("", file_list.append)
 78     ll = '\n'.join(file_list)
 79     if ' '+filename in ll:
 80         ftp.close()
 81         return True
 82     ftp.close()
 83     return False
 84 
 85 
 86 def delete_start(ip, filename):
 87     ftp = ftpconnect(ip, "admin", "admin")
 88     file_list = []
 89     ftp.dir("", file_list.append)
 90     ll = '\n'.join(file_list)
 91     if ' '+filename in ll:
 92         ftp.delete(filename)
 93         ftp.close()
 94         return True
 95     ftp.close()
 96     return False
 97 
 98 
 99 def upload(flnm):
100     error = []
101     pass_list = []
102     ping_failed = []
103     for i in tqdm(ip_dic):
104         if i not in ip_dic:
105             continue
106         if not ping(ip_dic[i][1]):
107             ping_failed.append(ip_dic[i][0] + '(' + ip_dic[i][1] + ')')
108             os.system('cls')
109             print('\n上传成功:' + ' '.join(pass_list) + ' (' + str(len(pass_list)) + ')')
110             print('\n不通:' + ' '.join(ping_failed) + ' (' + str(len(ping_failed)) + ')')
111             print('\n上传失败:' + ' '.join(error) + ' (' + str(len(error)) + ')\n')
112             if not i + 1 > len(ip_dic) and i + 1 in ip_dic:
113                 print('上传中:' + ip_dic[i + 1][0] + '(' + ip_dic[i + 1][1] + ')')
114             continue
115         try:
116             up_start(ip_dic[i][1], flnm)
117         except  Exception:
118             error.append(ip_dic[i][0] + '(' + ip_dic[i][1] + ')')
119             os.system('cls')
120             print('\n上传成功:' + ' '.join(pass_list) + ' (' + str(len(pass_list)) + ')')
121             print('\n不通:' + ' '.join(ping_failed) + ' (' + str(len(ping_failed)) + ')')
122             print('\n上传失败:' + ' '.join(error) + ' (' + str(len(error)) + ')\n')
123             if not i + 1 > len(ip_dic) and i + 1 in ip_dic:
124                 print('上传中:' + ip_dic[i + 1][0] + '(' + ip_dic[i + 1][1] + ')')
125             continue
126         pass_list.append(ip_dic[i][0])
127         os.system('cls')
128         print('\n上传成功:' + ' '.join(pass_list) + ' (' + str(len(pass_list)) + ')')
129         print('\n不通:' + ' '.join(ping_failed) + ' (' + str(len(ping_failed)) + ')')
130         print('\n上传失败:' + ' '.join(error) + ' (' + str(len(error)) + ')\n')
131         if not i + 1 > len(ip_dic) and i + 1 in ip_dic:
132             print('上传中:' + ip_dic[i + 1][0] + '(' + ip_dic[i + 1][1] + ')')
133 
134 
135 def check(flnm):
136     error = []
137     pass_list = []
138     ping_failed = []
139     for i in tqdm(ip_dic):
140         if i not in ip_dic:
141             continue
142         if not ping(ip_dic[i][1]):
143             ping_failed.append(ip_dic[i][0] + '(' + ip_dic[i][1] + ')')
144             os.system('cls')
145             print('\n文件存在:' + ' '.join(pass_list) + ' (' + str(len(pass_list)) + ')')
146             print('\n不通:' + ' '.join(ping_failed) + ' (' + str(len(ping_failed)) + ')')
147             print('\n文件不存在:' + ' '.join(error) + ' (' + str(len(error)) + ')\n')
148             if not i + 1 > len(ip_dic) and i + 1 in ip_dic:
149                 print('检查中:' + ip_dic[i + 1][0] + '(' + ip_dic[i + 1][1] + ')')
150             continue
151         try:
152             if check_start(ip_dic[i][1], flnm):
153                 pass_list.append(ip_dic[i][0])
154             else:
155                 error.append(ip_dic[i][0] + '(' + ip_dic[i][1] + ')')
156         except  Exception:
157             error.append(ip_dic[i][0] + '(' + ip_dic[i][1] + ')')
158             os.system('cls')
159             print('\n文件存在:' + ' '.join(pass_list) + ' (' + str(len(pass_list)) + ')')
160             print('\n不通:' + ' '.join(ping_failed) + ' (' + str(len(ping_failed)) + ')')
161             print('\n文件不存在:' + ' '.join(error) + ' (' + str(len(error)) + ')\n')
162             if not i + 1 > len(ip_dic) and i + 1 in ip_dic:
163                 print('检查中:' + ip_dic[i + 1][0] + '(' + ip_dic[i + 1][1] + ')')
164             continue
165         os.system('cls')
166         print('\n文件存在:' + ' '.join(pass_list) + ' (' + str(len(pass_list)) + ')')
167         print('\n不通:' + ' '.join(ping_failed) + ' (' + str(len(ping_failed)) + ')')
168         print('\n文件不存在:' + ' '.join(error) + ' (' + str(len(error)) + ')\n')
169         if not i + 1 > len(ip_dic) and i + 1 in ip_dic:
170             print('检查中:' + ip_dic[i + 1][0] + '(' + ip_dic[i + 1][1] + ')')
171 
172 
173 def delete(flnm):
174     error = []
175     pass_list = []
176     ping_failed = []
177     for i in tqdm(ip_dic):
178         if i not in ip_dic:
179             continue
180         if not ping(ip_dic[i][1]):
181             ping_failed.append(ip_dic[i][0] + '(' + ip_dic[i][1] + ')')
182             os.system('cls')
183             print('\n删除成功:' + ' '.join(pass_list) + ' (' + str(len(pass_list)) + ')')
184             print('\n不通:' + ' '.join(ping_failed) + ' (' + str(len(ping_failed)) + ')')
185             print('\n删除失败:' + ' '.join(error) + ' (' + str(len(error)) + ')\n')
186             if not i + 1 > len(ip_dic) and i + 1 in ip_dic:
187                 print('删除中:' + ip_dic[i + 1][0] + '(' + ip_dic[i + 1][1] + ')')
188             continue
189         try:
190             if delete_start(ip_dic[i][1], flnm):
191                 pass_list.append(ip_dic[i][0])
192             else:
193                 error.append(ip_dic[i][0] + '(' + ip_dic[i][1] + ')')
194         except  Exception:
195             error.append(ip_dic[i][0] + '(' + ip_dic[i][1] + ')')
196             os.system('cls')
197             print('\n删除成功:' + ' '.join(pass_list) + ' (' + str(len(pass_list)) + ')')
198             print('\n不通:' + ' '.join(ping_failed) + ' (' + str(len(ping_failed)) + ')')
199             print('\n删除失败:' + ' '.join(error) + ' (' + str(len(error)) + ')\n')
200             if not i + 1 > len(ip_dic) and i + 1 in ip_dic:
201                 print('删除中:' + ip_dic[i + 1][0] + '(' + ip_dic[i + 1][1] + ')')
202             continue
203         os.system('cls')
204         print('\n删除成功:' + ' '.join(pass_list) + ' (' + str(len(pass_list)) + ')')
205         print('\n不通:' + ' '.join(ping_failed) + ' (' + str(len(ping_failed)) + ')')
206         print('\n删除失败:' + ' '.join(error) + ' (' + str(len(error)) + ')\n')
207         if not i + 1 > len(ip_dic) and i + 1 in ip_dic:
208             print('删除中:' + ip_dic[i + 1][0] + '(' + ip_dic[i + 1][1] + ')')
209 
210 
211 note ='''上传文件到所有店共享根目录输入:upload 程序同级目录中要上传的文件名
212 在所有店共享根目录删除文件输入:delete 要删除的文件名
213 在所有店共享根目录中检查是否存在某文件名的文件输入:check 要检查是否存在的文件名
214 !!!文件名中不要包含空格!!!\n'''
215 print(note)
216 while True:
217     command = input('bobo>')
218     if command.strip() == '':
219             continue
220     if command.strip() == 'help' or command.strip() == '?':
221         print(note)
222         continue
223     if len(command.split()) == 2:
224         if command.strip().split()[0] == "upload" and not os.path.exists(os.getcwd()+"\\"+command.strip().split()[1]):
225             print('没这文件啊~~~')
226             continue
227         if command.strip().split()[0] == "upload" and os.path.exists(os.getcwd()+"\\"+command.strip().split()[1]):
228             upload(command.strip().split()[1])
229             continue
230         if command.strip().split()[0] == "check":
231             check(command.strip().split()[1])
232             continue
233         if command.strip().split()[0] == "delete":
234             delete(command.strip().split()[1])
235             continue
236     print('输的都不对(lll¬ω¬)')

 

posted on 2019-08-17 22:37  聪明bo  阅读(771)  评论(0)    收藏  举报