-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSFTPmoverV8.py
163 lines (152 loc) · 6.47 KB
/
SFTPmoverV8.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
#!/usr/bin/python
#This script looks in the pi sample folder and SFTP's files to the destination folder when it is available.
#It requires FreeSSH or similar to be running on the target machine if it is Windows to be able to make a connection.
"""
This script is intended to loop through all the files in the
current directory and move them to the destination folder
"""
import datetime
import shutil
import os
import time
#import ftplib
#from ftplib import FTP
import sys
import paramiko
currentlocation = os.getcwd()
source = '/home/pi/sample/folder'
destination = 'Users/Username/Sample/Folder'
host = '192.168.0.1'
port = 22
user = 'user'
password = 'password'
def printLog(s):
print s
logfile = open('/home/pi/deluge/testftp/fileslog.txt', "a")
logfile.write("\n" + s)
logfile.close()
def findfiles(source):
files = os.listdir(source)
print('Operating in: ' + source + "\nThese are the files here: \n" + str(files))
tfiles = []
for f in files:
if not f == 'fileslog.txt' and not f[-3:] == '.py' and not f[-3:] == '.sh':
tfiles.append(f)
print('This will be moved: %s ,found: %s' % ((f), datetime.datetime.now()))
else:
print('This won\'t be moved :' + f)
return tfiles
def SFTPtransfer(f, source, destination, sftp, transport, level):
sourcefile = source + '/' + f
destfile = destination + '/' + f
level += 1
breakvar = False
printLog('Starting a new recursion with: ' + sourcefile)
printLog('Destination: ' + destfile)
if os.path.isfile(sourcefile):
try:
printLog('Putting File: ' + sourcefile)
sftp.put(sourcefile, destfile)
printLog('File Put')
return True
except:
printLog('Level: ' + str(level) + ' We got an error while trying to put the file: %s' % (str(sys.exc_info())))
return False
else:
try:
printLog('Directory detected, Starting to traverse: ' + sourcefile)
for root, dirs, files in os.walk(sourcefile):
printLog('Starting For loop with root = %s, dirs = %s, and files = %s.' % (str(root),str(dirs),str(files)))
printLog('Checking if directory already exists: ' + str(f in sftp.listdir(destination)))
if (f in sftp.listdir(destination)):
printLog('Directory already exists, just gonna put the files there')
else:
printLog('Making Directory: ' + str(destfile))
sftp.mkdir(destfile)
printLog('Made')
#printLog('Starting dirs: ' + str(dirs))
for newfile in dirs:
if not newfile == []:
if not SFTPtransfer(newfile, sourcefile, destfile, sftp, transport, level):
printLog('Level: ' + str(level) + ' Error hit on a deep folder recursion, breaking lowest for loop')
breakvar = True
break
else:
printLog('Transfering low level folder %s seemed to go well, now removing it' % newfile)
shutil.rmtree(source + '/' + newfile)
else:
print('No more dirs in ' + sourcefile)
if breakvar:
printLog('Breaking outer for loop now')
break
print('Done with dirs in ' + sourcefile)
#printLog('Starting files: ' + str(files))
for newfile in files:
if not newfile == []:
if not SFTPtransfer(newfile, sourcefile, destfile, sftp, transport, level):
printLog('Level: ' + str(level) + ' Error hit on a deep file recursion, breaking lowest for loop')
breakvar = True
break
else:
printLog('Transfering low level file %s seemed to go well, now removing it' % newfile)
shutil.rmtree(source + '/' + newfile)
else:
print('No more files in ' + sourcefile)
if breakvar:
printLog('Breaking outer for loop now')
break
print('Done with files in ' + sourcefile)
if breakvar:
return False
else:
return True
#printLog('Removing folder now: ' + sourcefile)
#os.remove(sourcefile)
except:
printLog('Level: ' + str(level) + ' We got an error while trying to put the "%s" folder: %s' % (sourcefile,str(sys.exc_info())))
#sftp.close()
#transport.close()
printLog('Returning false at end of top level, this should go back to SFTPlist now')
return False
def SFTPlist(files, source, host, port, user, password, destination):
try:
transport = paramiko.Transport((host, port))
printLog("Transport Object Created")
except:
#printLog('We got an error while trying to connect to %s: %s' % (host, str(sys.exc_info())))
printLog('HockenmaierA3 isn\'t on')
return
printLog('Starting an SFTP job with files %s from %s to %s %s' % (files, source, host, destination))
transport.connect(username = user, password = password)
printLog('Connected via SFTP')
printLog('These files will be moved: ' + str(files))
sftp = paramiko.SFTPClient.from_transport(transport)
printLog('Created SFTP Object')
for f in files:
if SFTPtransfer(f, source, destination, sftp, transport, 0):
printLog('Transfering top level %s seemed to go well, now removing it' % f)
shutil.rmtree(source + '/' + f)
else:
printLog('Something when wrong, I will try this item again later: ' % f)
sftp.close()
transport.close()
printLog('Closed SFTP')
#Check for running process:
pid = str(os.getpid())
print pid
pidfile = "/tmp/mydaemon.pid"
if os.path.isfile(pidfile):
print "%s already exists, exiting" % pidfile
sys.exit()
else:
printLog('Writing a new PID file')
printLog('Process is: ' + pid)
file(pidfile, 'w').write(pid)
#Now starting the work:
printLog(str(datetime.datetime.now()))
tfiles = findfiles(source)
if tfiles:
SFTPlist(tfiles, source, host, port, user, password, destination)
time.sleep(5)
if os.path.isfile(pidfile):
os.remove(pidfile)