Sharing files with SAMBA

In a LAN environment, you will often need to share the files between different types of machines, such as Windows and Linux machines. The protocol used for sharing the files and the printers among these machines is either the Server Message Block (SMB) protocol or its enhanced version called the Common Internet File System (CIFS) protocol. CIFS runs over TCP/IP and it is used by the SMB clients and servers. In Linux, you will find a package called Samba, which implements the SMB protocol.

If you are running a Linux virtual machine within a Windows box with the help of software, such as VirtualBox, then we can test file sharing among the Windows and the Linux machines. Let us create a folder at C:share on the Windows machine as you can see in the following screenshot:

Sharing files with SAMBA

Now, right-click on the folder and then go to the Sharing tab. There are two buttons: Share and Advanced sharing. You can click on the latter and it will open the advanced sharing dialog box. Now you can adjust the share permissions. If this share is active, then you will be able to see this share from your Linux virtual machine. If you run the following command on your Linux box, then you will see the previously defined file-share:

$smbclient -L 10.0.2.2 -U WINDOWS_USERNAME%PASSWPRD  -W WORKGROUP
Domain=[FARUQUESARKER] OS=[Windows 8 9200] Server=[Windows 8 6.2]

    Sharename       Type      Comment
    ---------       ----      -------
    ADMIN$          Disk      Remote Admin
    C$              Disk      Default share
    IPC$            IPC       Remote IPC
    Share           Disk

The following screenshot shows how you can share a folder under Windows 7 as discussed previously:

Sharing files with SAMBA

The preceding file share can be accessed from your Python script by using a third-party module called pysmb. You can use the pip command-line tool for installing pysmb:

$ pip install pysmb

This module provides an SMBConnection class, where you can pass the necessary parameters for accessing an SMB/CIFS share. For example, the following code will help you to access a file-share:

from smb.SMBConnection import SMBConnection
smb_connection = SMBConnection(username, password, client_machine_name, server_name, use_ntlm_v2 = True, domain='WORKGROUP', is_direct_tcp=True)

If the preceding works, then the following assertion will be true:

assert smb_connection.connect(server_ip, 445)

You can list the shared files by using the listShares() method:

shares =  smb_connection.listShares()
for share in shares:
    print share.name

If you can use the tmpfile module copying a file from your windows share. For example, if you create a file in the C:Share est.rtf path, then the additional code shown here will copy that file by using the SMB protocol:

import tempfile
files = smb_connection.listPath(share.name, '/')

for file in files:
    print file.filename

file_obj = tempfile.NamedTemporaryFile()
file_attributes, filesize = smb_connection.retrieveFile('Share', '/test.rtf', file_obj)
file_obj.close()

If we put the entire code into a single source file, then it will look like the following listing:

#!/usr/bin/env python
import tempfile
from smb.SMBConnection import SMBConnection

SAMBA_USER_ID = 'FaruqueSarker'
PASSWORD = 'PASSWORD'
CLIENT_MACHINE_NAME = 'debian6box'
SAMBA_SERVER_NAME = 'FARUQUESARKER'
SERVER_IP = '10.0.2.2'
SERVER_PORT = 445
SERVER_SHARE_NAME = 'Share'
SHARED_FILE_PATH = '/test.rtf'


if __name__ == '__main__':

    smb_connection = SMBConnection(SAMBA_USER_ID, PASSWORD, CLIENT_MACHINE_NAME, SAMBA_SERVER_NAME, use_ntlm_v2 = True, domain='WORKGROUP', is_direct_tcp=True)
    assert smb_connection.smb_connectionect(SERVER_IP, SERVER_PORT = 445)
    shares =  smb_connection.listShares()
    
    for share in shares:
        print share.name
        
    files = smb_connection.listPath(share.name, '/')
    for file in files:
        print file.filename
    
    file_obj = tempfile.NamedTemporaryFile()
    file_attributes, filesize = smb_connection.retrieveFile(SERVER_SHARE_NAME, SHARED_FILE_PATH, file_obj)
    
    # Retrieved file contents are inside file_obj
    file_obj.close()

Inspecting SAMBA packets

If we capture the SMABA packets on port 445, then we can see how the Windows Server communicates with the Linux SAMBA client over the CIFS protocol. In the following two screenshots, a detailed communication between the client and the server, has been presented. The connection setup has been shown in the following screenshot:

Inspecting SAMBA packets

The following screenshot shows how a file copy session is performed:

Inspecting SAMBA packets

A typical SAMBA packet format has been shown in the following screenshot. The important field of this packet is the NT_STATUS field. Typically, if the connection is successful, then it will show STATUS_SUCESS. Otherwise, it will print a different code. This is shown in the following screenshot:

Inspecting SAMBA packets
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.157.6