forked from humberry/smb-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smb-test.py
174 lines (153 loc) · 5.67 KB
/
smb-test.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
# -*- coding: utf-8 -*-
# Attention: Please use pysmb 1.1.14
from cStringIO import StringIO
from smb.SMBConnection import SMBConnection
from smb import smb_structs
from nmb.NetBIOS import NetBIOS
import os
from socket import gethostname
def getBIOSName(remote_smb_ip, timeout=5):
try:
bios = NetBIOS()
srv_name = bios.queryIPForName(remote_smb_ip, timeout=timeout)
except:
print 'getBIOSName: timeout too short?'
finally:
bios.close()
#print 'bios name = ' + srv_name[0]
return srv_name[0]
def getServiceName(username, password, my_name, remote_name, remote_ip):
conn = connect(username, password, my_name, remote_name, remote_ip)
if conn:
shares = conn.listShares()
for s in shares:
if s.type == 0: # 0 = DISK_TREE
return s.name
conn.close()
else:
return ''
def getRemoteDir(username, password, my_name, remote_name, remote_ip, path, pattern, service_name):
print('getRemoteDir() starts...')
conn = connect(username, password, my_name, remote_name, remote_ip)
if conn:
try:
files = conn.listPath(service_name, path, pattern=pattern)
#files = conn.listPath(service_name, path)
#print('returning: {}'.format(files))
return files
except Exception, e:
fmt = 'conn.listPath({}, {}, {}) threw {}: {}'
print(fmt.format(service_name, path, pattern, type(e), e))
#print(type(e))
finally:
conn.close()
else:
print('connect() failed!')
return None
def connect(username, password, my_name, remote_name, remote_ip):
smb_structs.SUPPORT_SMB2 = True
conn = SMBConnection(username, password, my_name, remote_name, use_ntlm_v2 = True)
try:
conn.connect(remote_ip, 139) #139=NetBIOS / 445=TCP
except Exception, e:
print e
return conn
def download(username, password, my_name, remote_name, remote_ip, path, filename, service_name):
conn = connect(username, password, my_name, remote_name, remote_ip)
if conn:
print 'Download = ' + path + filename
attr = conn.getAttributes(service_name, path+filename)
print 'Size = %.1f kB' % (attr.file_size / 1024.0)
print 'start download'
file_obj = StringIO()
file_attributes, filesize = conn.retrieveFile(service_name, path+filename, file_obj)
fw = open(filename, 'w')
file_obj.seek(0)
for line in file_obj:
fw.write(line)
fw.close()
print 'download finished'
conn.close()
def upload(username, password, my_name, remote_name, remote_ip, path, filename, service_name):
conn = connect(username, password, my_name, remote_name, remote_ip)
if conn:
print 'Upload = ' + path + filename
print 'Size = %.1f kB' % (os.path.getsize(filename) / 1024.0)
print 'start upload'
with open(filename, 'r') as file_obj:
filesize = conn.storeFile(service_name, path+filename, file_obj)
print 'upload finished'
conn.close()
def delete_remote_file(username, password, my_name, remote_name, remote_ip, path, filename, service_name):
conn = connect(username, password, my_name, remote_name, remote_ip)
if conn:
conn.deleteFiles(service_name, path+filename)
print 'Remotefile ' + path + filename + ' deleted'
conn.close()
def createRemoteDir(username, password, my_name, remote_name, remote_ip, path, service_name):
conn = connect(username, password, my_name, remote_name, remote_ip)
if conn:
try:
conn.createDirectory(service_name, path)
except Exception, e:
fmt = 'conn.listPath({}, {}, {}) threw {}: {}'
print(fmt.format(service_name, path, pattern, type(e), e))
finally:
conn.close()
else:
print('connect() failed!')
return None
def removeRemoteDir(username, password, my_name, remote_name, remote_ip, path, service_name):
conn = connect(username, password, my_name, remote_name, remote_ip)
if conn:
try:
conn.deleteDirectory(service_name, path)
except Exception, e:
fmt = 'conn.listPath({}, {}, {}) threw {}: {}'
print(fmt.format(service_name, path, pattern, type(e), e))
finally:
conn.close()
else:
print('connect() failed!')
return None
def removeRemoteDir(username, password, my_name, remote_name, remote_ip, old_path, new_path, service_name):
conn = connect(username, password, my_name, remote_name, remote_ip)
if conn:
try:
conn.rename(service_name, old_path, new_path)
except Exception, e:
fmt = 'conn.listPath({}, {}, {}) threw {}: {}'
print(fmt.format(service_name, path, pattern, type(e), e))
finally:
conn.close()
else:
print('connect() failed!')
return None
username = 'admin'
password = 'raspi4715'
#my_name = 'iPad'
my_name = gethostname()
remote_ip = '10.10.10.254'
#remote_name = getBIOSName(remote_ip)
#remote_name = 'tm02'
remote_name = '10.10.10.254'
filename = 'raspi-guide.pdf'
#filename = 'smb-example.py'
#new_filename = 'smb-example2.py'
path = '/'
#path = '/share/'
service_name = getServiceName(username, password, my_name, remote_name, remote_ip)
#service_name = 'IPC$'
pattern = '*'
#removeRemoteDir(username, password, my_name, remote_name, remote_ip, path + filename, path + new_filename, service_name)
#removeRemoteDir(username, password, my_name, remote_name, remote_ip, path, service_name)
#createRemoteDir(username, password, my_name, remote_name, remote_ip, path, service_name)
#download(username, password, my_name, remote_name, remote_ip, path, filename, service_name)
#upload(username, password, my_name, remote_name, remote_ip, path, filename, service_name)
#delete_remote_file(username, password, my_name, remote_name, remote_ip, path, filename, service_name)
'''
files = getRemoteDir(username, password, my_name, remote_name, remote_ip, path, pattern, service_name)
if files:
for file in files:
print file.filename
'''