-
Notifications
You must be signed in to change notification settings - Fork 1
/
get.py
69 lines (62 loc) · 1.71 KB
/
get.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
import paramiko
import sys
import os
import time
from glob import iglob
import shutil
target = {}
target["ip"] = sys.argv[1]
target["port"] = sys.argv[2]
target["user"] = sys.argv[3]
target["passwd"] = sys.argv[4]
target["remotepath"] = sys.argv[5]
target["offset"] = sys.argv[6]
target["size"] = sys.argv[7]
target["output"] = sys.argv[8]
target["end"] = sys.argv[9]
target["localpath"] = sys.argv[10]
temp = target["remotepath"].split('/')
target["filename"] = temp[len(temp)-1]
print target["localpath"]+target["filename"]
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(target["ip"], int(target["port"]), target["user"], target["passwd"])
ftp = ssh.open_sftp()
fr = ftp.file(target["remotepath"], 'rb')
fr.seek(long(target["offset"]))
file_size = long(target["size"])
fr.prefetch()
path = target["localpath"] + target["output"]
""" main sftp routine """
try:
fl = file(path, 'wb')
try:
size = 0
while True:
if (file_size - size > 32768):
data = fr.read(32768)
else:
data = fr.read(file_size - size)
if len(data) == 0:
# print "stop"
break
fl.write(data)
size += len(data)
finally:
fl.close()
finally:
fr.close()
s = os.stat(path)
if s.st_size != size:
raise IOError('size mismatch in get! %d != %d' % (s.st_size, size))
ftp.close()
""" if we are the last slave (last chunk) wait and concatenate all chunks to the end file """
if (target["end"] == "last"):
time.sleep(10)
destination = open(target["localpath"] + target["filename"], 'wb')
for filename in iglob(os.path.join(target["localpath"], 'chunk*')):
f = open(filename, 'rb')
shutil.copyfileobj(f, destination)
f.close()
os.remove(filename)
destination.close()