Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying files from SMB server to local drive(Linux/Windows) in python

Tags:

python

I am using smb module to connect to smb server using following snippet

import tempfile
from smb.SMBConnection import SMBConnection
from nmb.NetBIOS import NetBIOS

conn = SMBConnection('salead', 
                     'repo@2k12', 
                     '192.168.14.1', 
                     'SERVER', 
                     use_ntlm_v2=True)
assert conn.connect('192.168.1.41', 139)
if conn:
    print "successfull",conn
else:
    print "failed to connect"

I am unable to figure out what exactly I could do to copy files from smb to my local drive as I am using linux machine.

If anyone can help me out it would be a great help for me.

Thanks

like image 354
Anvesh Avatar asked Dec 06 '13 10:12

Anvesh


1 Answers

According to some documentation, SMBConnection.retrieveFile() is the function you are searching for.

Example:

# UNTESTED
conn = SMBConnection('salead',
                     'repo@2k12',
                     '192.168.14.1',
                     'SERVER',
                     use_ntlm_v2 = True)
assert conn.connect('192.168.1.41', 139)
with open('local_file', 'wb') as fp:
    conn.retrieveFile('share', '/path/to/remote_file', fp)

Documentation: http://pysmb.readthedocs.io/en/latest/api/smb_SMBConnection.html

Example (in Japanese): http://symfoware.blog68.fc2.com/blog-entry-999.html

like image 81
Robᵩ Avatar answered Sep 21 '22 12:09

Robᵩ