Search code examples
pythonsmb

"Unable to open file" using SMBConnection


There is an issue when uploading a file to a remote server. The error is

Unable to open file using SMBConnection

Any help is appreciated as I am unsure what to do. I have searched stackoverflow and github for this question being asked, but had no luck figuring out a solution.

smb.smb_structs.OperationFailure: Failed to store DEVELOPMENT\Microchiseling\timedata_MC-1.csv on sys2: Unable to open file
==================== SMB Message 0 ====================
SMB Header:
-----------
Command: 0x03 (SMB2_COM_TREE_CONNECT) 
Status: 0x00000000 
Flags: 0x00 
PID: 22459 
MID: 3 
TID: 0 
Data: 38 bytes 
b'0900000048001e005c005c004f0050005400530046005300300031005c007300790073003200' 
SMB Data Packet (hex):
----------------------
b'fe534d4240000000000000000300000000000000000000000300000000000000bb57000000000000810d004823740100000000000000000000000000000000000900000048001e005c005c004f0050005400530046005300300031005c007300790073003200'
==================== SMB Message 1 ====================
SMB Header:
-----------
Command: 0x03 (SMB2_COM_TREE_CONNECT) 
Status: 0x00000000 
Flags: 0x01 
PID: 22459 
MID: 3 
TID: 1 
Data: 16 bytes 
b'100001000000000000000000ff011f00' 
SMB Data Packet (hex):
----------------------
b'fe534d4240000000000000000300010001000000000000000300000000000000bb57000001000000810d00482374010000000000000000000000000000000000100001000000000000000000ff011f00'
==================== SMB Message 2 ====================
SMB Header:
-----------
Command: 0x05 (SMB2_COM_CREATE) 
Status: 0x00000000 
Flags: 0x00 
PID: 22459 
MID: 4 
TID: 1 
Data: 264 bytes 
b'3900000002000000000000000000000000000000000000009f0112002000000000000000050000004400000078005800d00000007800000044004500560045004c004f0050004d0045004e0054005c004d006900630072006f00630068006900730065006c0069006e0067005c00740069006d00650064006100740061005f004d0043002d0031002e006300730076002800000010000400000018001000000044486e51000000000000000000000000000000000000000020000000100004000000180008000000416c5369000000008562000000000000180000001000040000001800000000004d78416300000000000000001000040000001800000000005146696400000000' 
SMB Data Packet (hex):
----------------------
b'fe534d4240000000000000000500000000000000000000000400000000000000bb57000001000000810d004823740100000000000000000000000000000000003900000002000000000000000000000000000000000000009f0112002000000000000000050000004400000078005800d00000007800000044004500560045004c004f0050004d0045004e0054005c004d006900630072006f00630068006900730065006c0069006e0067005c00740069006d00650064006100740061005f004d0043002d0031002e006300730076002800000010000400000018001000000044486e51000000000000000000000000000000000000000020000000100004000000180008000000416c5369000000008562000000000000180000001000040000001800000000004d78416300000000000000001000040000001800000000005146696400000000'
==================== SMB Message 3 ====================
SMB Header:
-----------
Command: 0x05 (SMB2_COM_CREATE) 
Status: 0xC0000022 
Flags: 0x01 
PID: 22459 
MID: 4 
TID: 1 
Data: 9 bytes 
b'090000000000000000' 
SMB Data Packet (hex):
----------------------
b'fe534d4240000000220000c00500010001000000000000000400000000000000bb57000001000000810d00482374010000000000000000000000000000000000090000000000000000'

Trying to understand the code is difficult for my limited experience, however, here's the entire code posted below to give a better idea of the situation:

import filecmp
import datetime
import time
from shutil import copyfile
from socket import gethostname
from smb.SMBConnection import SMBConnection
from smb import smb_structs
import os


filename="timedata_MC-1.csv"
mc = "/home/pi/MC_SHARE/timedata.csv"
loc = "/home/pi/Documents/PythonScripts/timedata_MC-1.csv"

def upload(username, password, my_name, remote_name, remote_ip, path, filename, service_name):
    conn = connect(username, password, my_name, remote_name, remote_ip)
    '''
    uploads a file to an sbm server
    '''
    
    if conn:
        print ('Upload = ' + path + filename
)
        print ('Size = %.1f kB' % (os.path.getsize(filename) / 1024.0))
        print ('start upload')
        with open(filename, 'rb') as file_obj:
            filesize = conn.storeFile(service_name, path+filename, file_obj)
        print ('upload finished')
        conn.close()


def connect(username, password, my_name, remote_name, remote_ip):
    '''
    Creates an SMB connection object to a server
    '''
    smb_structs.SUPPORT_SMB2 = True
    conn = SMBConnection(username, password, my_name, remote_name, use_ntlm_v2 = True)

    try:
        conn.connect(remote_ip, 139)
        return conn
    
    except Exception as e:
        print(e)
        return


def get_password():
    passwordFile= '/home/pi/Documents/Backup/PythonScripts/Test/8265.txt'
    emailPassword = open(passwordFile)
    file_contents = list(emailPassword.readlines())
    return file_contents[0].strip()

username = 'OPTSservice'
password = get_password()
my_name = gethostname()
remote_name = 'OPTSFS01'
remote_ip = '???.???.?.???'
path = '/DEVELOPMENT/Microchiseling/'
service_name = 'sys2'

go = True

while go:
    try:
        print("CHECKING FILE: " + str(datetime.datetime.now()))
        
        if not filecmp.cmp (mc, loc):
            copyfile(mc, loc)
            print("UPDATED")
            upload(username, password, my_name, remote_name, remote_ip, path, filename, service_name)
        time.sleep(300)
    except KeyboardInterrupt:
        go = False
    #except:
        #continue

Solution

  • It was a permission issue that caused

    Failed to store DEVELOPMENT\Microchiseling\timedata_MC-1.csv on sys2: Unable to open file