pysmb
pysmb is an experimental SMB/CIFS library written in Python. It implements the client-side SMB/CIFS protocol which allows your Python application to access and transfer files to/from SMB/CIFS shared folders like your Windows file sharing and Samba folders.
If you use pysmb in your applications, please drop me a comment at the bottom of this page to let me and the others know of what you have done using pysmb. Thanks.
Disclaimer
Use this software entirely at your own risk. pysmb has not been actively maintained for the past few years because of my work. The last major development was done during the Windows XP days. Hence, I cannot guarantee that it will work with shared folders on Windows Vista and the latest Samba service.
What It Can Do
It can run on Python 2.1 to 2.5. I have already tested them on my machines. Since the whole library is written in pure Python, it should be able to run on any Python-supported platforms. It can upload/download/copy/rename/remove/list files and directories that are shared in user mode.
From version 0.2.0, pysmb also supports encrypted authentication in DES. However, you can turn off encrypted authentication on these machines by using these registry files (Win95, Win98, Win2000, NT4) in the extra directory
From version 0.3.0, pysmb allows operations on services in share mode. Previous versions only support services in user mode.
From version 0.4.0, pysmb uses NT LM0.12 dialect to communicate with the SMB server. Note that this is only a partial implementation, so it might fail on some systems.
What It Can’t Do
Note that this is only a client library. It does not share files.
Quick Start
Simply put smb.py and nmb.py together with your Python source files and import them. Please use
import smb, nmb
and not
from smb import *
This is because smb.py defines a number of constants which might interfere with your project’s variables. For more information about the API, please consult the documentation in the docs directory, or look at the accompanying
utilities for ideas.
- Request the NetBIOS name of the remote machine from your user.
- Create a nmb.NetBIOS() object.
- Call nmb.NetBIOS.gethostbyname() method to resolve your remote NetBIOS name to an IP address.
- Create a smb.SMB() object, passing in the NetBIOS names and the resolved IP address.
- Call smb.SMB.is_login_required() to determine if the remote machine requires login to further access its services. If login is required, retrieve the user name and password from the user and call smb.SMB.login().
- Depending on your application, call the exported functions in smb.SMB object.
Utilities
pysmb also includes some simple utilities which I hope can help to demonstrate the use of pysmb. These are working applications which I feel will be very useful.
- nmbquery
This is very similar to Samba’s nmblookup but is quite very limited in its functionality - smbdu
This displays the disk usage of the files/directories recursively like the Unix’s du program. - smbcp
Performs files/directory copy to/from your local machine and the remote SMB machine. - smblistshare
List the available share devices on the remote SMB machine.
Downloads
If you have problems opening tar.gz files in Windows, please install 7-Zip. It supports 7z, ZIP, GZIP, BZIP2, TAR and many other archive formats.
Version 0.4.5, 2009-06-22 (Download)
- Prevents pysmb from failing when there are too many files/folders to
be returned in a single SMB TRANS2 call. pysmb will “resume” requesting
for more files/folders information in subsequent SMB TRANS2 requests.
Version 0.4.4, 2004-01-12 (Download)
- Add in support for AMK’s Python Cryptography Toolkit which will be used for DES password hashing. If AMK’s pycrypto is found, it will be used instead of mxCrypto.
Version 0.4.3, 2003-02-22 (Download)
- Fix a bug which fails to close the socket in nmb.py on socket exception
- Fix a bug which fails to close the NetBIOSSession in smb.py if the session has not been properly established yet
Version 0.4.2, 2002-08-03 (Download)
- Add new methods to SharedFile instances, get_mtime_epoch, get_atime_epoch and get_ctime_epoch. These methods will return the mtime, atime and ctime in epoch time rather than SMB time.
- Remove debugging printout in smb.py which has been released accidentally with the last release.
- Fix a bug in smbcp which causes to local to remote copy to fail
Version 0.4.1, 2002-06-22 (Download)
- Fix a bug in smb.py which does not return the correct file size for files with their archive bits turned off. This results in these files not being retrieved or sent properly.
- Fix some typo error in the documentations
Version 0.4.0, 2002-04-17 (Download)
- Use NT LM0.12 dialect.
- New smb.SMBMachine class
- Add SMB.get_server_domain(), smb.get_server_os(), SMB.get_server_lanman()
Version 0.3.1, 2001-11-12 (Download)
- Fix a problem with some Windows server where an UID is required when server is in share mode. Now, pysmb calls login() with empty authentication information when server is in share mode.
- Add TYPE_DOMAIN_MASTER constant and description to nmb.py.
Version 0.3.0, 2001-11-10 (Download)
- Add support for services in share mode. Minor changes to smb.SMB class API.
- Fix a bug in smb.py’s __raw_retr_file which has failed to initialize the max_buf_size prior to usage.
- Fix a bug in smblistshare which fails to print the correct NMB error message
- Modify smb.py not throw AttributeError in the destructor when there is an error while creating a session in the constructor
Version 0.2.0, 2001-10-04 (Download)
- Add support for encrypted authentication using DES
- Fix a bug(?) which treats all services and filenames as case-sensitive
Version 0.1.3, 2001-09-03 (Download)
- Fix a bug in smblistshare and smbdu which fails to catch the nmb.NetBIOSError raised when session setup fails.
- Fix a bug in smb.SMB that arises from the change in nmb.NetBIOSSession which sends the session port number as the remote host type.
- Fix a bug in nmb.NetBIOSSession which specifies a TYPE_WORKSTATION for remote host instead of TYPE_SERVER.
- Minor change to nmb.NetBIOSSession constructor API.
- Fix a bug in smbdu which raises OverflowError when printing long file size values.
- Fix a bug in smbcp which does not handle the destination path correctly when the source file is copied to a different filename.
- Change nmb’s NetBIOS and NetBIOSSession class such that they raise a NetBIOSError with a tuple of ( err_msg, err_class, err_code )
- Add a function strerror() in both smb and nmb to return human readable messages for error codes.
- Fix a bug in smbcp which fails to print an error message and terminate when the remote source path is not found.
- Add in another utility, smblistshare.
- First public release
























[...] more information, please visit: http://miketeo.net/wp/index.php/projects/pysmb Bookmark This Rattle… Posted in: Python, Software Development ADD [...]
Hi Mike,
great piece of code and seems to work pretty flawlessly. Just one minor thing i would love to do is reuse an existing connection to a smb server. So on windows once you are logged in on a server with say explorer it wouldnt re-need authentication and would reuse the windows connection. Is that possible and how would i go about it ?
Kind Regards and thanks in advance,
Thorsten
i’ve been searching for a python netbios name resolution module, and came across pysmb. just what i needed- native python and lightweight, thanks.
also, wanted to let you know that i found a small error while testing NetBIOS.gethostbyname(). if a wins server returns multiple nodes in response to a query, each node is 6 bytes long: 2 bytes for flags followed by 4 bytes for the ip address. however, the code only increments by 4 bytes if multiple modes are found.
for i in range(0, num_records):
# In Python2, we can use socket.inet_ntoa(data[58 + i * 4:62 + i * 4]) to convert
addrs.append(NBHostEntry(string.rstrip(qn_name[:-1]) + qn_scope, ord(qn_name[-1]), ‘%d.%d.%d.%d’ % unpack(‘4B’, (data[offset:offset + 4]))))
offset = offset + 4
changing the offset increment to “offset = offset + 6″ solves the problem. no idea how accurate this is across all versions and types of query responses, especially since the response record contains the data length (in this case 12 when 2 unique h-records are returned).
anyways thanks..
As I had to make an automatic backup script uploading to a SMB share, I chose your tool as the base for my work, but I had some troubles making it work since the terminology in the documentation is somewhat obscure and no working example is provided.
Here is a simple python script example :
#!/bin/env python
# Author : Stephen LARROQUE
# DESCRIPTION : simple python script to demonstrate the use of the pysmb library to connect to a Samba share and send a file. This could be useful for other devs, as there are nearly no documentation on the web, and all other solutions aren’t as versatile and interoperable as this one (it should work on any plateform)
import smb, nmb, getpass, os;
fileshare = smb.SMB(‘YOURSMBHOSTNAME’, ‘YOURSMBHOSTIP’, my_name = “THEMATRIX”, host_type = nmb.TYPE_SERVER, sess_port = nmb.NETBIOS_SESSION_PORT);
fileshare.login(‘yourlogin’, ‘password’)
#fileshare.stor_file(‘Call’, remote_path+filename, sz.read)
serveros = fileshare.get_server_os();
print serveros;
if (fileshare.is_login_required()) :
print “Login is required on this server.”;
print fileshare.list_shared();
if (fileshare.list_path(“SMBSHAREMOUNT”, “PATH\*”)) :
print “Dir exists !”;
src_path = ‘D:/test.txt’;
dest_service = “SMBSHAREMOUNT”;
dest_path = “PATH/test11.txt”;
fh = open(src_path, ‘rb’);
fileshare.stor_file(dest_service, dest_path, fh.read);
fh.close();
hi, found another minor error, when querying a node for its status. for instance, if you want to verify the IP returned by a name query sent to a WINS server is not a stale or tombstoned reference. -query the returned IP for its nbname and check that against the original name query, (if they match all is well). in this case setting the recursion bit against a client will cause status request to be ignored.
so, within __querynodestatus()
…
if destaddr:
req = pack(‘>HHHHHH’, trn_id, 0×0100, 0×01, 0×00, 0×00, 0×00) + qn_label + pack(‘>HH’, 0×21, 0×01)
else:
req = pack(‘>HHHHHH’, trn_id, 0×0110, 0×01, 0×00, 0×00, 0×00) + qn_label + pack(‘>HH’, 0×21, 0×01)
change the flags word from 0×0100 to 0×000 when destaddr is set and from 0×0110 to 0×0010 when it is not set.
Does this module handle SMB Chaining as well? Even though I didn’t go through all of the modules functions/classes I just wanted to know before I start working on it.
- Sujit
Add A Comment