-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPKFFv5.2.1.py
312 lines (272 loc) · 9.58 KB
/
PKFFv5.2.1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# The following code is written by Rohit Saxena. STUDY PURPOSE ONLY. Any unauthorized use or modification is prohibited.
import ctypes
import datetime
import ftplib
import glob
import os
import shutil
import string
from urllib.error import URLError
from urllib.request import *
import docx
import openpyxl
import logging
import threading
import time
from tqdm import tqdm
# import PyPDF2
# import re
errorCount, i, j = 0, 0, 0
rvs = []
host = "" # FTP Host URL
ftp_id = "" # FTP CLIENT ID
ftp_pw = "" # FTP CLIENT PASSWORD
_ftp_root_folder_ = 'htdocs' # FTP root folder
newdir = os.environ['COMPUTERNAME'] # FTP Main folder name
ft = []
todaysDate = str(datetime.datetime.today()) # FTP sub-folder name
ftp = ftplib.FTP()
def thread_function(name):
rstqdm = tqdm(range(100))
time.sleep(2)
# logging.info("Scan started %s: loading...", name)
for i in rstqdm:
time.sleep(1)
if i == 100:
rstqdm.clear()
logging.info("Please wait scan is in progress...")
def _rS_sign():
print("\n><----------------------------------------------><\n")
print("|""---------"" ""------------")
print("|"" ""|"" "" |")
print("|"" ""|"" "" |")
print("|""---------"" ""|")
print("|""-"" ""------------")
print("|"" ""-"" ""|")
print("|"" ""-"" ""|")
print("|"" ""-"" "" ------------")
print("The following code is written by Rohit Saxena. STUDY PURPOSE ONLY. Any unauthorized use or modification is "
"prohibited.")
print("\n><----------------------------------------------><\n")
return True
def _rSAdmin_Check():
try:
is_admin = (os.getuid() == 0)
except AttributeError:
is_admin = ctypes.windll.shell32.IsUserAnAdmin() != 0
return is_admin
def _rSInternet_Check_(): # Internet Tester
while True:
try:
urlopen('https://www.central16.in', timeout=1)
return
except URLError as e:
print('Internet Connectivity Check --> URL Error: ', e.reason) # remove print is building exe
pass
def _RsBackupWriter(rrs, pp): # local backup (unfinished)
global j
print('BACKUP FOLDER STARTED \nTotal File address written : ', len(rrs))
trs = open(pp, "a")
for item in rrs:
trs.write("%s\n" % item)
print('Backup File Created')
file1 = open(pp, "r")
print(file1.read())
file1.close()
def _RsXlsx_(SS, CC): # XLSX Reader
rs = openpyxl.load_workbook(CC, read_only=True, data_only=True)
ws = rs.active
for i in range(1, ws.max_row + 1):
for j in range(1, ws.max_column + 1):
if SS == ws.cell(i, j).value:
print("Found in cell :", ws.cell(i, j), "File Location :", CC)
rvs.append(CC)
'''
def _rsPDF_(SS, CC): # PDF reader
filepdf = PyPDF2.PdfFileReader(CC)
NumPages = filepdf.getNumPages()
for i in range(0, NumPages):
PageObj = filepdf.getPage(i)
Text = PageObj.extractText()
ResSearch = re.search(SS, Text)
print("-------------> ", ResSearch)
if ResSearch in filepdf:
print('String', ResSearch, 'File Path: ', CC)
rvs.append()
'''
def _RsTxt_(SS, CC): # TXT Reader
file1 = open(CC, 'r', encoding='UTF8', errors='ignore')
flag = 0
index = 0
for line in file1:
index += 1
if SS in line.lower():
flag = 1
break
if flag != 0:
print('String', SS, 'Found In Line: ', index, 'File Path: ', CC)
rvs.append(CC)
file1.close()
def _RsDocx_(SS, CC): # DOCX Reader
flag = 0
index = 0
doc = docx.Document(CC)
for para in doc.paragraphs:
index += 1
if SS in para.text:
flag = 1
break
if flag != 0:
print('String', SS, 'Found In Line: ', index, 'File Path: ', CC)
rvs.append(CC)
def _RsDirCheck_(rsdc): # FTP Main Folder Checker
x = 0
for g in rsdc:
if newdir in g:
x = 1
if x == 0:
print(" New Directory created ---> ", newdir)
ftp.mkd(newdir)
def _RsTodayFolderCheck_(rstfc): # FTP Sub-Folder Checker
x = 0
for g in rstfc:
if todaysDate in g:
x = 1
if x == 0:
ftp.mkd(todaysDate)
print("New Today's date folder created ---> ", todaysDate)
def _Rs_ftp_COPY_(): # FTP Client connector
print('uploading data...')
port = 21
ftp.connect(host, port)
print(ftp.getwelcome())
print("Logging in...")
ftp.login(ftp_id, ftp_pw)
ftp.cwd(_ftp_root_folder_)
ftp.retrlines('LIST', ft.append)
_RsDirCheck_(ft)
ftp.cwd(newdir)
ft.clear()
ftp.retrlines('LIST', ft.append)
_RsTodayFolderCheck_(ft)
ftp.cwd(todaysDate)
def _rS_FTP_file_transf(rvs): # FTP File uploader
try:
rs = 0
print('Uploading Data...')
while rs != len(rvs):
filename = rvs[rs]
file_name, file_extension = os.path.splitext(filename)
_FTP_File_Name_ = os.path.basename(file_name) + file_extension
_ftpNewFileNMCmd_ = "STOR %s" % _FTP_File_Name_
with open(filename, "rb") as file:
ftp.storbinary(_ftpNewFileNMCmd_, file)
rs += 1
print('Upload Complete !')
ftp.dir()
except Exception:
print(Exception)
def main():
global errorCount
if _rSAdmin_Check():
os.system('color 2')
print("<-----Admin Access----->")
else:
os.system('color 4')
print("<-----No Admin Access----->")
global i, j
global rvs
# val = input("Enter extension : ") #If specific extension only required uncheck and add val in _Rel_Path
ff = input("Enter string : ").lower()
rs = ['%s:\\' % d for d in string.ascii_uppercase if os.path.exists('%s:\\' % d)]
print('\n><----------------------------------------------><\nGENERAL DATA\nActive local drives ---> ', rs)
FtypeXLSX = '.xlsx'
FtypeTXT = '.txt'
FtypeDOCX = '.docx'
FtypePDF = '.pdf'
_Rel_Exp_ = '**/*'
_Rel_Path_ = _Rel_Exp_
# print('Selected file type = ', val)
rss = []
bck_dir = "RSSF" # LINE 154 - 166 REFERS LOCAL BACKUP (UNFINISHED)
bck_file = "RSSF.txt"
print('Reuslt folder named RSSF is created in first local drive from available drive list')
pth = os.path.join(rs[0], bck_dir)
if os.path.exists(pth): # this if will flush the old backup folder and re-create new backup folder
shutil.rmtree(pth)
os.makedirs(pth)
print('Result folder path: ', pth)
print('Result file RSSF.txt is created in hidden folder rss')
CN = os.path.join(pth, bck_file)
print('Result file location: ', CN)
file1 = open(CN, "a")
file1.close()
format = "%(asctime)s: %(message)s"
logging.basicConfig(format=format, level=logging.INFO,
datefmt="%H:%M:%S")
x = threading.Thread(target=thread_function, args=(1,))
x.start()
if _rS_sign():
print('Below files are present in system: '"\n")
while i != len(rs):
os.chdir(rs[i])
for file in glob.glob(_Rel_Path_, recursive=True):
# print(file)
completeName = os.path.join(rs[i], file)
# print('File path: ', completeName)
file_name, file_extension = os.path.splitext(completeName)
if file_extension == FtypeXLSX:
try:
_RsXlsx_(ff, completeName)
except Exception:
errorCount += 1
print('Error in reading file, File Path: ', completeName)
if file_extension == FtypeTXT:
try:
_RsTxt_(ff, completeName)
except PermissionError:
errorCount += 1
print('Admin Permission Required File Path: ', completeName)
if file_extension == FtypeDOCX:
try:
_RsDocx_(ff, completeName)
except Exception:
errorCount += 1
print('Error in reading file, File Path: ', completeName)
'''
if file_extension == FtypePDF:
try:
_rsPDF_(ff, completeName)
except Exception:
errorCount += 1
print('Error in reading file, File Path: ', completeName)
'''
rss.append(completeName)
i += 1
else:
exit()
x.join()
logging.info("Master Control RSSF : Check " + CN + " For result data")
print('\nSystem scan completed\n><----------------------------------------------><\nSCAN RESULTS')
# print(rss)
print('Searched Text:', ff)
print('Total File Scanned :', len(rss))
print("Total file found: ", len(rvs))
print("Total No of unreachable files (ACTION REQUIRED)", errorCount)
if len(rvs) == 0:
input("Press enter to close program")
exit()
print("Below are the file paths \n", rvs)
'''
_Rs_ftp_COPY_()
_rS_FTP_file_transf(rvs) #ONLY FOR FTP '''
print('\n><----------------------------------------------><\ncalling backup')
_RsBackupWriter(rvs, CN) # LOCAL BACKUP CREATOR
_rSInternet_Check_()
if _rS_sign():
print("Other generic information\n><----------------------------------------------><\n1.Backup folders are "
"ENABLED\n2.FTP options are DISABLED ")
main()
else:
print("Author sign missing")
input("Press enter to close program")