miketeo / pysmb

pysmb is an experimental SMB/CIFS library written in Python. It implements the client-side SMB/CIFS protocol (SMB1 and SMB2) which allows your Python application to access and transfer files to/from SMB/CIFS shared folders like your Windows file sharing and Samba folders.
Other
341 stars 94 forks source link

How do you create sub directories? #22

Open alexei opened 10 years ago

alexei commented 10 years ago

I am able to create directories with createDirectory, but there's no way to either create a subdirectory or change the cwd to another directory.

alexei commented 10 years ago

After a few more tests I realiezed I can simply create a directory "a", then another "b", and then rename "b" to "a/b". That seems to do the trick.

Is this the only way?

miketeo commented 10 years ago

@alexei : Have you tried createDirectory('myshare', 'a/b') ?

alexei commented 10 years ago

Yes. client.createDirectory('myshare', 'foo/bar') results in smb.smb_structs.OperationFailure: Failed to create directory foo\bar on myshare: Create failed

miketeo commented 10 years ago

What OS/distro are you using? If you can provide the packet capture, I can look into this in more details later on.

alexei commented 10 years ago

I'm on Ubuntu 14.04 x86-64, Python 2.7, pysmb 1.1.12

Here's the code:

client = SMBConnection(smb_username, smb_password, client_ip, smb_server_name, use_ntlm_v2=True)
client.connect(smb_server_ip, smb_port)
client.createDirectory(smb_folder, 'foo/bar')

And the output:

Traceback (most recent call last):
  File "test.py", line 58, in <module>
    client.createDirectory(smb_folder, 'foo/bar')
  File "[...]/local/lib/python2.7/site-packages/smb/SMBConnection.py", line 391, in createDirectory
    self._pollForNetBIOSPacket(timeout)
  File "[...]/local/lib/python2.7/site-packages/smb/SMBConnection.py", line 538, in _pollForNetBIOSPacket
    self.feedData(data)
  File "[...]/local/lib/python2.7/site-packages/nmb/base.py", line 54, in feedData
    self._processNMBSessionPacket(self.data_nmb)
  File "[...]/local/lib/python2.7/site-packages/nmb/base.py", line 75, in _processNMBSessionPacket
    self.onNMBSessionMessage(packet.flags, packet.data)
  File "[...]/local/lib/python2.7/site-packages/smb/base.py", line 143, in onNMBSessionMessage
    if self._updateState(self.smb_message):
  File "[...]/local/lib/python2.7/site-packages/smb/base.py", line 1513, in _updateState_SMB1
    req.callback(message, **req.kwargs)
  File "[...]/local/lib/python2.7/site-packages/smb/base.py", line 2234, in createCB
    errback(OperationFailure('Failed to create directory %s on %s: Create failed' % ( path, service_name ), messages_history))
  File "[...]/local/lib/python2.7/site-packages/smb/SMBConnection.py", line 385, in eb
    raise failure
smb.smb_structs.OperationFailure: Failed to create directory foo\bar on x: Create failed
==================== SMB Message 0 ====================
SMB Header:
-----------
Command: 0x75 (SMB_COM_TREE_CONNECT_ANDX) 
Status: NTSTATUS=0x00000000 
Flags: 0x18 
Flags2: 0xC807 
PID: 8949 
UID: 100 
MID: 4 
TID: 0 
Security: 0x0000000000000000 
Parameters: 8 bytes 
ff0000000c000100 
Data: 25 bytes 
005c005c005a004500550053005c00780000003f3f3f3f3f00 
SMB Data Packet (hex):
----------------------
ff534d4275000000001807c80000000000000000000000000000f5226400040004ff0000000c0001001900005c005c005a004500550053005c00780000003f3f3f3f3f00
==================== SMB Message 1 ====================
SMB Header:
-----------
Command: 0x75 (SMB_COM_TREE_CONNECT_ANDX) 
Status: NTSTATUS=0x00000000 
Flags: 0x88 
Flags2: 0xC803 
PID: 8949 
UID: 100 
MID: 4 
TID: 1 
Security: 0x0000000000000000 
Parameters: 14 bytes 
ff0000000100ff011f0000000000 
Data: 13 bytes 
413a004e005400460053000000 
SMB Data Packet (hex):
----------------------
ff534d4275000000008803c80000000000000000000000000100f5226400040007ff0000000100ff011f00000000000d00413a004e005400460053000000
==================== SMB Message 2 ====================
SMB Header:
-----------
Command: 0x00 (SMB_COM_CREATE_DIRECTORY) 
Status: NTSTATUS=0x00000000 
Flags: 0x18 
Flags2: 0xC807 
PID: 8949 
UID: 100 
MID: 5 
TID: 1 
Security: 0x0000000000000000 
Parameters: 0 bytes 

Data: 17 bytes 
0466006f006f005c006200610072000000 
SMB Data Packet (hex):
----------------------
ff534d4200000000001807c80000000000000000000000000100f522640005000011000466006f006f005c006200610072000000
==================== SMB Message 3 ====================
SMB Header:
-----------
Command: 0x00 (SMB_COM_CREATE_DIRECTORY) 
Status: NTSTATUS=0xC000003A 
Flags: 0x88 
Flags2: 0xC803 
PID: 8949 
UID: 100 
MID: 5 
TID: 1 
Security: 0x0000000000000000 
Parameters: 0 bytes 

Data: 0 bytes 

SMB Data Packet (hex):
----------------------
ff534d42003a0000c08803c80000000000000000000000000100f52264000500000000
miketeo commented 10 years ago

@alexei : Weird, I'm not able to reproduce the error. Are you using Ubuntu 14.04 as your remote server? I will need the packet capture from you to see what's wrong with the communication on your end.

from smb.SMBConnection import SMBConnection

c = SMBConnection('mike', 'xyzpass', 'MYTEST', 'TESTSERVER')
c.connect('127.0.0.1')
c.createDirectory('smbtest','foo')
c.createDirectory('smbtest','foo/bar')
alexei commented 10 years ago

Hello,

The file server is a Samba 3.5.8, and runs on Ubuntu 11.04 x86_64 (Linux 2.6.38-16).

HTH

sevenjay commented 8 years ago

@miketeo : I have the same issue. Samba Server: 4.1.6-Ubuntu on Ubuntu 14.04 x86_64 Client: both python 3.4.3 and 3.5.1, pysmb-1.1.17 on Ubuntu 14.04 x86_64 thx

keeely commented 8 years ago

I tried this with python 2.7.8 (win, 32-bit), pysmb 1.1.18, samba 4.1, no issues. The full Samba config is the same as in my post here

jingjing0506 commented 5 years ago

I have the same issue. Can you discribe your solution? thx so much.

miketeo commented 5 years ago

Note that if you are trying to create 'foo/bar' folder, 'foo' folder must exist on the remote server.

jingjing0506 commented 5 years ago

Note that if you are trying to create 'foo/bar' folder, 'foo' folder must exist on the remote server.

It works when I add Samba config create mask = 0666 directory mask = 0777

FengZhiheng commented 3 years ago

I'm on Ubuntu 14.04 x86-64, Python 2.7, pysmb 1.1.12

Here's the code:

client = SMBConnection(smb_username, smb_password, client_ip, smb_server_name, use_ntlm_v2=True)
client.connect(smb_server_ip, smb_port)
client.createDirectory(smb_folder, 'foo/bar')

And the output:

Traceback (most recent call last):
  File "test.py", line 58, in <module>
    client.createDirectory(smb_folder, 'foo/bar')
  File "[...]/local/lib/python2.7/site-packages/smb/SMBConnection.py", line 391, in createDirectory
    self._pollForNetBIOSPacket(timeout)
  File "[...]/local/lib/python2.7/site-packages/smb/SMBConnection.py", line 538, in _pollForNetBIOSPacket
    self.feedData(data)
  File "[...]/local/lib/python2.7/site-packages/nmb/base.py", line 54, in feedData
    self._processNMBSessionPacket(self.data_nmb)
  File "[...]/local/lib/python2.7/site-packages/nmb/base.py", line 75, in _processNMBSessionPacket
    self.onNMBSessionMessage(packet.flags, packet.data)
  File "[...]/local/lib/python2.7/site-packages/smb/base.py", line 143, in onNMBSessionMessage
    if self._updateState(self.smb_message):
  File "[...]/local/lib/python2.7/site-packages/smb/base.py", line 1513, in _updateState_SMB1
    req.callback(message, **req.kwargs)
  File "[...]/local/lib/python2.7/site-packages/smb/base.py", line 2234, in createCB
    errback(OperationFailure('Failed to create directory %s on %s: Create failed' % ( path, service_name ), messages_history))
  File "[...]/local/lib/python2.7/site-packages/smb/SMBConnection.py", line 385, in eb
    raise failure
smb.smb_structs.OperationFailure: Failed to create directory foo\bar on x: Create failed
==================== SMB Message 0 ====================
SMB Header:
-----------
Command: 0x75 (SMB_COM_TREE_CONNECT_ANDX) 
Status: NTSTATUS=0x00000000 
Flags: 0x18 
Flags2: 0xC807 
PID: 8949 
UID: 100 
MID: 4 
TID: 0 
Security: 0x0000000000000000 
Parameters: 8 bytes 
ff0000000c000100 
Data: 25 bytes 
005c005c005a004500550053005c00780000003f3f3f3f3f00 
SMB Data Packet (hex):
----------------------
ff534d4275000000001807c80000000000000000000000000000f5226400040004ff0000000c0001001900005c005c005a004500550053005c00780000003f3f3f3f3f00
==================== SMB Message 1 ====================
SMB Header:
-----------
Command: 0x75 (SMB_COM_TREE_CONNECT_ANDX) 
Status: NTSTATUS=0x00000000 
Flags: 0x88 
Flags2: 0xC803 
PID: 8949 
UID: 100 
MID: 4 
TID: 1 
Security: 0x0000000000000000 
Parameters: 14 bytes 
ff0000000100ff011f0000000000 
Data: 13 bytes 
413a004e005400460053000000 
SMB Data Packet (hex):
----------------------
ff534d4275000000008803c80000000000000000000000000100f5226400040007ff0000000100ff011f00000000000d00413a004e005400460053000000
==================== SMB Message 2 ====================
SMB Header:
-----------
Command: 0x00 (SMB_COM_CREATE_DIRECTORY) 
Status: NTSTATUS=0x00000000 
Flags: 0x18 
Flags2: 0xC807 
PID: 8949 
UID: 100 
MID: 5 
TID: 1 
Security: 0x0000000000000000 
Parameters: 0 bytes 

Data: 17 bytes 
0466006f006f005c006200610072000000 
SMB Data Packet (hex):
----------------------
ff534d4200000000001807c80000000000000000000000000100f522640005000011000466006f006f005c006200610072000000
==================== SMB Message 3 ====================
SMB Header:
-----------
Command: 0x00 (SMB_COM_CREATE_DIRECTORY) 
Status: NTSTATUS=0xC000003A 
Flags: 0x88 
Flags2: 0xC803 
PID: 8949 
UID: 100 
MID: 5 
TID: 1 
Security: 0x0000000000000000 
Parameters: 0 bytes 

Data: 0 bytes 

SMB Data Packet (hex):
----------------------
ff534d42003a0000c08803c80000000000000000000000000100f52264000500000000

I had the same problem, so I came up with a solution:

relativePath = r'\\a\\b\\c\\d'
tt = relativePath.split("\\")
for i in range(len(tt)+1):
    tempDir = []
    for j in range(i):
        tempDir.append(tt[j])
        tempDir.append("/")
    tempDir2 = ''.join(tempDir)
    try:
        self.samba.createDirectory(self.service_name, self.smb_dir + tempDir2)  # 先创建文件夹
    except:
        pass