Forum Archive

Python SMB Connection

DavinE

Hey Guys,

I have a Problem and i don't find my solution...
Here's my test Code:

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

netbios = NetBIOS()

share_name          = "share"
user_name           = "user"
password            = "passwd"
server_IP           = "xxx.xxx.xxx.251"
local_machine_name  = socket.gethostname()
server_machine_name = netbios.queryIPForName(server_IP)

print(server_machine_name[0])

# create and establish connection
conn = SMBConnection(user_name, password, local_machine_name, server_machine_name[0], domain="WORKGROUP", use_ntlm_v2=True)
# 139
# 445
print(conn.connect(server_IP, 445))

i get every Time this Message:

ELEKTROTECHNIK
False

but the Function conn.connect return on Success True... not False....
i Have no idea what i'm doing wrong...

im trying to connect to my Synology NAS 718

i hope you guys know what im doing wrong....

Thanks!!

cvp

@DavinE I always use it with port=139 on my Western Digital NAS.

cvp

@DavinE said:

local_machine_name = socket.gethostname()

Try

local_machine_name  = socket.gethostname()[0]
DavinE

@cvp i tried both :( same result...

cvp

@DavinE said:

server_machine_name = netbios.queryIPForName(server_IP)

Sorry, error😢 , try

server_machine_name = netbios.queryIPForName(server_IP)[0]
DavinE

@cvp said:

@DavinE said:

server_machine_name = netbios.queryIPForName(server_IP)

Sorry, error😢 , try
server_machine_name = netbios.queryIPForName(server_IP)[0]

@cvp, no :(
i want to cry ^^

cvp

@DavinE said:

i want to cry ^^

Sorry, I don't understand, that will say that this does not work?

DavinE

@cvp
yes, sorry :(

I used both ports

cvp

@DavinE strange, I use this successfully

            ip = bios.queryName(self.smb_name)
            return ip[0]
DavinE

@cvp said:

@DavinE strange, I use this successfully
ip = bios.queryName(self.smb_name) return ip[0]

in your case whats the self.smb_name? Server Name ?

cvp

@DavinE sorry, a mistake more,

            bios = NetBIOS()
            srv_name = bios.queryIPForName(remote_smb_ip, timeout=timeout)
            return srv_name[0]
DavinE

@cvp
hmm...
I get the same output as i get with my code ELEKTROTECHNIK

cvp

@DavinE said:

in your case whats the self.smb_name? Server Name

Yes

cvp

@DavinE said:

local_machine_name = socket.gethostname()
server_machine_name = netbios.queryIPForName(server_IP)

Did you try with

local_machine_name  = socket.gethostname()[0]
server_machine_name = netbios.queryIPForName(server_IP)[0]
DavinE

@cvp said:

@DavinE said:

in your case whats the self.smb_name? Server Name

Yes

When i use your code above

ip = bios.queryName(self.smb_name)
            return ip[0]

server_machine_name = netbios.queryIPForName(server_IP)[0]

print(server_machine_name) ELEKTROTECHNIK
print(local_machine_name) Davin
print(netbios.queryName(server_machine_name)) and here None

why none ?

DavinE

@cvp said:

@DavinE said:

local_machine_name = socket.gethostname()
server_machine_name = netbios.queryIPForName(server_IP)

Did you try with
local_machine_name = socket.gethostname()[0] server_machine_name = netbios.queryIPForName(server_IP)[0]

This did not work here i get only the first letter

local_machine_name = socket.gethostname()[0]

in my case D

cvp

@DavinE could you try with my module
SMB_client.py

and

from SMB_client import SMB_client

my_smb = SMB_client(username='admin',password='admin',smb_name='share')
my_smb.connect()

tree = my_smb.getRemoteTree()
for elem in tree:
    print(elem)
DavinE

@cvp said:

@DavinE could you try with my module
SMB_client.py

and
```
from SMB_client import SMB_client

my_smb = SMB_client(username='admin',password='admin',smb_name='share')
my_smb.connect()

tree = my_smb.getRemoteTree()
for elem in tree:
print(elem)
```

yeah i tried it but i can retry it...

i do not have SMB_client installed
should i install it ? and try

cvp

@DavinE did you see the link in my post?

cvp

@DavinE said:

should i install it ? and try

Sure

DavinE

@cvp said:

@DavinE did you see the link in my post?

yeah sry, that's my mistake....

i get this output:

Error on line 47 TypeError 'NoneType' object is not subscriptable
Error on line 62 TypeError str, bytes or bytearray expected, not NoneType
Error on line 87 NotReadyError SMB connection not authenticated
cvp

@DavinE strange, I'll check, my smb connection works

DavinE

@cvp said:

@DavinE strange, I'll check, my smb connection works

Thanks for your Work @cvp

cvp

@DavinE could you try, with your own user, pwd, server

from SMB_client import SMB_client
import keychain

server_ftp = 'VOO-USB'
user = 'voo'
# get password from keychain
pwd = keychain.get_password(server_ftp,user)
my_smb = SMB_client(username=user,password=pwd, smb_name='VOO-USB', print_errors=False)
my_smb.connect()
print(my_smb.getRemoteDir('', '*'))
cvp
smb_files = my_smb.getRemoteDir('', '*')
for smb_file in smb_files:
    smb_filename = smb_file.filename
    print(smb_file.filename)
DavinE

@cvp said:

@DavinE could you try, with your own user, pwd, server
```
from SMB_client import SMB_client
import keychain

server_ftp = 'VOO-USB'
user = 'voo'

get password from keychain

pwd = keychain.get_password(server_ftp,user)
my_smb = SMB_client(username=user,password=pwd, smb_name='VOO-USB', print_errors=False)
my_smb.connect()
print(my_smb.getRemoteDir('', '*'))
```

i get None

but is it correct that i set my pass in server_ftp

cvp

@DavinE said:

but is it correct that i set my pass in server_ftp

Forget my keychain process. In all my scripts, I store my passwords in keychain.
Use your username='xxx',password='xxx',smb_name='xxx'

cvp

@DavinE and put your ip as server (my server has an ip but also a name)

DavinE

@cvp
sry.... i'm confused....

smb_name = "" # That is my Folder share name or ??
and where i need to put my Server ip ?

i have this:

my_smb = SMB_client(username=user,password=pwd, smb_name='home', print_errors=False)
cvp

@DavinE this should be ok if you know the ip but not the smb name

ip = ....
my_smb = SMB_client()
smb_name = my_smb.getBIOSName(ip)
print(smb_name)

my_smb = SMB_client(username=user,password=pwd, smb_name=smb_name, print_errors=False)
my_smb.connect()

smb_files = my_smb.getRemoteDir('', '*')
for smb_file in smb_files:
    smb_filename = smb_file.filename
    print(smb_file.filename)
cvp

@DavinE said:

sry.... i'm confused..

I'm too, forget all except my just previous post

DavinE

@cvp said:

@DavinE this should be ok if you know the ip but not the smb name
print(smb_name)

This is: ELEKTROTECHNIK
i think that's correct!

smb_files = my_smb.getRemoteDir('', '*') print(smb_files)

Here i get None

cvp

@DavinE said:

Here i get None

I you used my last post, that would say the ip is not correct.
The first my_smb is used to get the smb_name from the ip and the second uses the smb_name.

I print the smb_name, do you see it?

cvp

Now, this should work if you know the ip

from SMB_client import SMB_client

ip = 'xxx.xxx.xxx.xxx'
user = 'xxxxxx'
pwd = 'xxxxx'
my_smb = SMB_client()
smb_name = my_smb.getBIOSName(ip)
print('smb_name=',smb_name)
my_smb = SMB_client(username=user,password=pwd, smb_name=smb_name, print_errors=False)
my_smb.connect()
smb_files = my_smb.getRemoteDir('', '*')
for smb_file in smb_files:
    smb_filename = smb_file.filename
    print(smb_file.filename)
DavinE

@cvp said:

Now, this should work if you know the ip

```
from SMB_client import SMB_client

ip = 'xxx.xxx.xxx.xxx'
user = 'xxxxxx'
pwd = 'xxxxx'
my_smb = SMB_client()
smb_name = my_smb.getBIOSName(ip)
print('smb_name=',smb_name)
my_smb = SMB_client(username=user,password=pwd, smb_name=smb_name, print_errors=False)
my_smb.connect()
smb_files = my_smb.getRemoteDir('', '*')
for smb_file in smb_files:
smb_filename = smb_file.filename
print(smb_file.filename)
```

so i tried it with my Raspberry Pi2....
here is my output:

pi@RaspberryPi2:~/TEST/smb $ python smb_de.py 
('smb_name=', 'ELEKTROTECHNIK')
.
..
Drive

on my iPad pro 12.9" i get only:

smb_name=, ELEKTROTECHNIK

with the same user and pwd....
this is a iPad problem or ?

cvp

@cvp said:

my_smb = SMB_client()
smb_name = my_smb.getBIOSName(ip)
print('smb_name=',smb_name)

These lines do not use user nor pwd, it is only to get the smb_name that you need to use in tHe second. Note that if you know this smb_name, the ip is not necessary

my_smb = SMB_client(username=user,password=pwd, smb_name=smb_name, print_errors=False)
my_smb.connect()
smb_files = my_smb.getRemoteDir('', '*')
for smb_file in smb_files:
    smb_filename = smb_file.filename
    print(smb_file.filename)
cvp

@DavinE said:

pi@RaspberryPi2:~/TEST/smb $ python smb_de.py

This line is printed by the smb client, and it is not the same on iPad, it does not print anything, it is normal.

But is the connection ok?

DavinE

@cvp ,

this is my test code for my connection:

import smb, socket
from smb.SMBConnection import SMBConnection
from nmb.NetBIOS import NetBIOS

netbios = NetBIOS()

user = 'xxx'
pwd = 'xxx'

share_name          = "ELEKTROTECHNIK"
user_name           = user
password            = pwd
server_IP           = "xxx.xxx.178.251"
local_machine_name  = socket.gethostname()
server_machine_name = netbios.queryIPForName(server_IP)

print(server_machine_name[0])

# create and establish connection
conn = SMBConnection(user_name, password, local_machine_name, server_machine_name[0], domain="WORKGROUP", use_ntlm_v2=True)
# 139
# 445
print(conn.connect(server_IP, 139))

on my iPad i get here False

on my Raspi2 i get True.....

or need i to "translate" my Pwd on my iPad in Base64 ?

cvp

@DavinE said:

or need i to "translate" my Pwd on my iPad in Base24 ?

No.

Did you retry with the other port? 445

DavinE

@cvp
yes, its the same False

cvp

@DavinE you cold also remove ,use_ntlm_v2 = True) in the source code of SmB_client.py

Just to check

DavinE

@cvp
Its the same...

DavinE

@cvp

Do you know what protocol Pythonista use ?
SMB1; SMB2; SMB3 ?

cvp

@DavinE no idea.
Your code works on my NAS

DavinE

@cvp
Which on do you have ? WD or ?

and do you mean on your iPad ?

cvp

@DavinE my NAS is a WD
I work on an iPad. WitH your code.
It also works with a flash drive connected in usb slot of my router, drive that I always reach in smb.

cvp

Obviously, the smb module in Pythonista is old and is not identical as on your Radpberry.

DavinE

@cvp said:

@DavinE my NAS is a WD
I work on an iPad. WitH your code.
It also works with a flash drive connected in usb slot of my router, drive that I always reach in smb.

When i use my Raspi with the data i get this:

RASPBERRYPI4
Error on line 87 OperationFailure Failed to list  on Ebner-Elektrotechnik: Unable to connect to shared device
==================== SMB Message 0 ====================
SMB Header:
-----------
Command: 0x03 (SMB2_COM_TREE_CONNECT) 
Status: 0x00000000 
Flags: 0x00 
PID: 1563 
MID: 9 
TID: 0 
Data: 78 bytes 
b'09000000480046005c005c005200410053005000420045005200520059005000490034005c00450062006e00650072002d0045006c0065006b00740072006f0074006500630068006e0069006b00' 
SMB Data Packet (hex):
----------------------
b'fe534d42400000000000000003000000000000000000000009000000000000001b06000000000000979d8799000000000000000000000000000000000000000009000000480046005c005c005200410053005000420045005200520059005000490034005c00450062006e00650072002d0045006c0065006b00740072006f0074006500630068006e0069006b00'
==================== SMB Message 1 ====================
SMB Header:
-----------
Command: 0x03 (SMB2_COM_TREE_CONNECT) 
Status: 0xC0000022 
Flags: 0x01 
PID: 1563 
MID: 9 
TID: 0 
Data: 9 bytes 
b'090000000000000000' 
SMB Data Packet (hex):
----------------------
b'fe534d4240000000220000c003000100010000000000000009000000000000001b06000000000000979d87990000000000000000000000000000000000000000090000000000000000'

[]

this is a shared folder on my Pi Ebner-Elektrotechnik

DavinE

@cvp said:

Obviously, the smb module in Pythonista is old and is not identical as on your Radpberry.

it looks so but it sucks.... :(

cvp

@DavinE smb is not a standard module of Pythonista, in site-packages, thus you could try to install there the same as on your Raspberry.

DavinE

@cvp
smb is in the module of pysmb or ?
on my iPad an my Pi the version is the same.... 1.2.6
and i installed both with pip install pysmb

cvp

@DavinE said:

smb is in the module of pysmb

Yes

cvp

I have the same problem on another WD NAS....

DavinE

@cvp said:

@DavinE said:

smb is in the module of pysmb

Yes

okay... then i have no idea what and how i solve my what ever ^^....

DavinE

@cvp said:

I have the same problem on another WD NAS....

oh okay for test now ?

cvp

@DavinE said:

okay for test now ?

Test what?

DavinE

@cvp said:

@DavinE said:

okay for test now ?

Test what?

The Connection or do you have this problem longer ?

cvp

@DavinE I normally access this NAS via FTP but I just tested it with your script and I meet the same connection error.

DavinE

@cvp

i found this:
ftp

is Pythonista Able to use ftp/sftp to upload files ?

DavinE

@cvp said:

@DavinE I normally access this NAS via FTP but I just tested it with your script and I meet the same connection error.

hehe okay and my idea is now FTP ^^

works this with Pythonista ?

cvp

@DavinE FTP is ok and SFTP (SSH) also

cvp

But my old WD NAS does not support SFTP...

cvp
from ftplib import FTP
import paramiko

ftplib for FTP
paramiko for SFTP

DavinE

@cvp said:

from ftplib import FTP import paramiko
ftplib for FTP
paramiko for SFTP

I know I'm annoying you a lot, but would you have a script like with SMB?

DavinE

@cvp said:

@DavinE FTP is ok and SFTP (SSH) also

SFTP i don't know it works because i use Private Key's for my SSH connection...

cvp

@DavinE upload or download?

DavinE

@cvp said:

@DavinE upload or download?

upload ;)

cvp

@DavinE try first this

from ftplib import FTP
ip = '192.168.0.47'
user = 'admin'
pwd = 'xxxxxxx''
ftp = FTP(ip) #connect
ftp.encoding = 'utf-8'
ftp.login(user,pwd)
ftp.retrlines('NLST *')
ftp.quit()
cvp

Upload

ftp = FTP(ip) #connect
ftp.encoding = 'utf-8'
ftp.login(user,pwd)
local_file = open(path,'rb')
ftp.storbinary('STOR '+server_file, local_file, blocksize=32768)
local_file.close()
ftp.quit()
cvp

@DavinE said:

I'm annoying you a lot,

Not at all, but less available tonight

DavinE

@cvp said:

from ftplib import FTP import paramiko
ftplib for FTP
paramiko for SFTP

There is also FTPS is that better for security?
how can i use this with Ports ?

from ftplib import FTP_TLS

ftp = FTP_TLS()
    ftp.ssl_version = ssl.PROTOCOL_SSLv23
    ftp.ssl_version = ssl.PROTOCOL_TLSv1_2
    ftp.debugging = 2
    ftp.connect('localhost', 2121)
    ftp.login('developer', 'password')
    return ftp

but which version TLS or SSL ?

DavinE

@cvp said:

@DavinE try first this
from ftplib import FTP ip = '192.168.0.47' user = 'admin' pwd = 'xxxxxxx'' ftp = FTP(ip) #connect ftp.encoding = 'utf-8' ftp.login(user,pwd) ftp.retrlines('NLST *') ftp.quit()

This works :D ^^
i see my Shared Folders ;)

DavinE

@cvp said:

Upload
ftp = FTP(ip) #connect ftp.encoding = 'utf-8' ftp.login(user,pwd) local_file = open(path,'rb') ftp.storbinary('STOR '+server_file, local_file, blocksize=32768) local_file.close() ftp.quit()

server_file
is this this the storage Path ?

cvp

@DavinE yes sir, but it may begin at first folder where you are authorized. try first without folder, only a file name to check after upload where it has been stored

cvp

@DavinE said:

There is also FTPS is that better for security?
how can i use this with Ports ?

from ftplib import FTP_TLS

ftp = FTP_TLS()
ftp.ssl_version = ssl.PROTOCOL_SSLv23
ftp.ssl_version = ssl.PROTOCOL_TLSv1_2
ftp.debugging = 2
ftp.connect('localhost', 2121)
ftp.login('developer', 'password')
return ftp
but which version TLS or SSL ?

Never used. If needed, I use SFTP

DavinE

@cvp said:

@DavinE yes sir, but it may begin at first folder where you are authorized. try first without folder, only a file name to check after upload where it has been stored

@cvp really thanks a lot for your help!! and for your Time to help me out ;) Thanks!!

i played now a while and it works like i will create folders upload files etc. great!!

beautiful evening @cvp xD

cvp

@DavinE 👍

DavinE

@cvp
I Have a Problem...

I used this code:

self.subDirCustomer_Prüfungen = 'Prüfungen'
self.cur.encoding = 'utf-8'
self.cur.mkd(self.subDirCustomer_Prüfungen) 

But i get this Output folder Prüfungen
How can i tell mkd to use the utf-8....

Do you know That ?

cvp

@DavinE sincerely, I don't know. I got the same kind of problems with accents like é, è, à on some, not all, machines I connect in smb. To avoid this, I don't use anymore accents in my folder/file names

Edit : even 10 years ago, this kind problem did already exist, see here

DavinE

@cvp aahhhh ;)
Okay shit but thanks for your answer

When i find something i tell you

ccc

Wild guess but... self.subDirCustomer_Prüfungen = 'Prüfungen'.encode('utf-8')

DavinE

@ccc
Now i get b'Pr\xc3\xbcfungen' as folder Name

cvp

@DavinE said:

How can i tell mkd to use the utf-8....

Sorry, my answer guess that you still speak about smb. I just see now that your post uses mkd, thus ftp, sorry.
I remember an old post here where the problem has been solved with

ftp.encoding = "utf-8"

Then, I can't help more.

DavinE

@cvp
No Problem

I find my Solution.
I removed the encoding = ‚utf-8‘ and it works

cvp

@DavinE 😂