Quantcast

python: listing samba shares

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

python: listing samba shares

Igor Katson-2
I've been googling that for a long time, but couldn't find an answer.
I also checked the python-samba Debian (ubuntu) package contents and
even compiled samba 4.0.0 alpha8, and checked its python libraries, but
couldn't find anything regarding viewing the shares contents there.

Is it possible to list samba shares and their contents of a particular host?
--
To unsubscribe from this list go to the following URL and read the
instructions:  https://lists.samba.org/mailman/options/samba
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: python: listing samba shares

Ricardo-59
Hi,

Here is some code to get you going. Try it out.

#!/usr/bin/env python

import sys

sys.path.append('/usr/local/samba/lib/python2.6/site-packages')
sys.path.append('/usr/local/samba/lib/python2.6/site-packages/samba')
sys.path.append('/usr/local/samba/lib/python2.6/site-packages/samba/dcerpc')

import param, shares

samba_lp = param.LoadParm()
samba_lp.load_default()

container = shares.SharesContainer(samba_lp)

for share in container:
        print "Path for " + share + ": " + samba_lp.get("path", share)





On Sun, Aug 16, 2009 at 3:37 PM, Igor Katson<[hidden email]> wrote:

> I've been googling that for a long time, but couldn't find an answer.
> I also checked the python-samba Debian (ubuntu) package contents and even
> compiled samba 4.0.0 alpha8, and checked its python libraries, but couldn't
> find anything regarding viewing the shares contents there.
>
> Is it possible to list samba shares and their contents of a particular host?
> --
> To unsubscribe from this list go to the following URL and read the
> instructions:  https://lists.samba.org/mailman/options/samba
>
--
To unsubscribe from this list go to the following URL and read the
instructions:  https://lists.samba.org/mailman/options/samba
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: python: listing samba shares

Igor Katson-2
Ricardo Jorge wrote:
> Hi,
>
> Here is some code to get you going. Try it out.
>
>  
Thanks for the quick answer. As far as I understand, this snippet of
code parses smb.conf and prints your shares (i don't have an smb.conf
for samba4 yet, and btw, found a segfault while trying to do samba_lp).

But what I need, is to list remote shares through the network, provided
a host, and, optionally, a port. Is it possible?

As for that segfault:
Python 2.6.2 (release26-maint, Apr 19 2009, 01:58:18)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> import samba.param
 >>> obj = samba.param.LoadParm()
 >>> obj.[Here I press tab-tab, for readline autocompletion]
Segmentation fault

--
To unsubscribe from this list go to the following URL and read the
instructions:  https://lists.samba.org/mailman/options/samba
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: python: listing samba shares

Helmut Hullen
In reply to this post by Igor Katson-2
Hallo, Igor,

Du meintest am 16.08.09:

> Is it possible to list samba shares and their contents of a
> particular host? --

Shell:

        smbclient -N -L $particular_host

shows first all shares of that particular host and then some other  
stuff. Perhaps you filter some output depending on the key word "Disk"  
(or "Printer").

Contents: "that depends".
You must have some rights.

Viele Gruesse!
Helmut
--
To unsubscribe from this list go to the following URL and read the
instructions:  https://lists.samba.org/mailman/options/samba
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: python: listing samba shares

Igor Katson-2
Helmut Hullen wrote:

> Hallo, Igor,
>
> Du meintest am 16.08.09:
>
>  
>> Is it possible to list samba shares and their contents of a
>> particular host? --
>>    
>
> Shell:
>
>         smbclient -N -L $particular_host
>
> shows first all shares of that particular host and then some other  
> stuff. Perhaps you filter some output depending on the key word "Disk"  
> (or "Printer").
>
> Contents: "that depends".
> You must have some rights.
>
> Viele Gruesse!
> Helmut
>  
Thanks.
Yes, I know of that shell command, but I would like to do that directly
from python for more flexibility, control and convenience.
--
To unsubscribe from this list go to the following URL and read the
instructions:  https://lists.samba.org/mailman/options/samba
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: python: listing samba shares

Ricardo-59
Hi,

I was investigating this and I believe there is actually a way with
Python but the problem is that the Python module with the binding is
not being compiled (sorry but I have no idea why).

There should be a module called samba.dcerpc.srvsvc.

If you look at librpc/gen_ndr/py_srvsvc.c you will find
NetShareEnumAll which binds to the same function that's called when -L
is used in smbclient.

Maybe it's something with the make file?



On Sun, Aug 16, 2009 at 4:36 PM, Igor Katson<[hidden email]> wrote:

> Helmut Hullen wrote:
>>
>> Hallo, Igor,
>>
>> Du meintest am 16.08.09:
>>
>>
>>>
>>> Is it possible to list samba shares and their contents of a
>>> particular host? --
>>>
>>
>> Shell:
>>
>>        smbclient -N -L $particular_host
>>
>> shows first all shares of that particular host and then some other  stuff.
>> Perhaps you filter some output depending on the key word "Disk"  (or
>> "Printer").
>>
>> Contents: "that depends".
>> You must have some rights.
>>
>> Viele Gruesse!
>> Helmut
>>
>
> Thanks.
> Yes, I know of that shell command, but I would like to do that directly from
> python for more flexibility, control and convenience.
> --
> To unsubscribe from this list go to the following URL and read the
> instructions:  https://lists.samba.org/mailman/options/samba
>
--
To unsubscribe from this list go to the following URL and read the
instructions:  https://lists.samba.org/mailman/options/samba
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: python: listing samba shares

Ricardo-59
Hi,

There was a line missing from source4/librpc/config.mk and it wasn't
building the Python module.

Here is a patch if you can apply it:

diff --git a/source4/librpc/config.mk b/source4/librpc/config.mk
index 2aeea6d..5305b34 100644
--- a/source4/librpc/config.mk
+++ b/source4/librpc/config.mk
@@ -672,6 +672,12 @@ PRIVATE_DEPENDENCIES = RPC_NDR_WKSSVC PYTALLOC
pyparam_util pycredentials python

 python_wkssvc_OBJ_FILES = ../librpc/gen_ndr/py_wkssvc.o

+[PYTHON::python_srvsvc]
+LIBRARY_REALNAME = samba/dcerpc/srvsvc.$(SHLIBEXT)
+PRIVATE_DEPENDENCIES = RPC_NDR_SRVSVC PYTALLOC pyparam_util
pycredentials python_dcerpc
+
+python_srvsvc_OBJ_FILES = ../librpc/gen_ndr/py_srvsvc.o
+
 [PYTHON::python_dfs]
 LIBRARY_REALNAME = samba/dcerpc/dfs.$(SHLIBEXT)
 PRIVATE_DEPENDENCIES = RPC_NDR_DFS PYTALLOC pyparam_util
pycredentials python_dcerpc

==================

If you can't patch just paste this:

[PYTHON::python_srvsvc]
LIBRARY_REALNAME = samba/dcerpc/srvsvc.$(SHLIBEXT)
PRIVATE_DEPENDENCIES = RPC_NDR_SRVSVC PYTALLOC pyparam_util
pycredentials python_dcerpc

python_srvsvc_OBJ_FILES = ../librpc/gen_ndr/py_srvsvc.o


in source4/librpc/config.mk. I think anywhere will be fine.

==================
After recompiling I could get the share names listed using this Python
code. Replace the credentials where necessary and server address.

#!/usr/bin/env python

import sys

sys.path.append('/usr/local/samba/lib/python2.6/site-packages')
sys.path.append('/usr/local/samba/lib/python2.6/site-packages/samba')
sys.path.append('/usr/local/samba/lib/python2.6/site-packages/samba/dcerpc')

from samba.dcerpc import samr, security, srvsvc
from samba import credentials

creds = credentials.Credentials()
creds.set_username("Administrator")
creds.set_password("x")

# you must leave this line or it will cause a segmentation fault
creds.set_workstation("")

try:
        conn = srvsvc.srvsvc('ncacn_np:localhost', credentials=creds)
except Exception, msg:
        print 'boooooo'
        print str(msg)
else:
        ctr = srvsvc.NetShareInfoCtr()
        shares = conn.NetShareEnumAll(u'localhost', ctr, 0, 0)

        print "\n\n============"
        print "There are " + str(shares[0].ctr.count) + " shares"
        print "============\n"

        for i in range(0, shares[0].ctr.count):
                print str(i) + ": " + shares[0].ctr.array[i].name

        print "\n\n"

I don't know how it will work for you because of your server
configuration. My Samba4 server is just for coding so it's pretty much
all default and local.




On Sun, Aug 16, 2009 at 5:08 PM, Ricardo Jorge<[hidden email]> wrote:

> Hi,
>
> I was investigating this and I believe there is actually a way with
> Python but the problem is that the Python module with the binding is
> not being compiled (sorry but I have no idea why).
>
> There should be a module called samba.dcerpc.srvsvc.
>
> If you look at librpc/gen_ndr/py_srvsvc.c you will find
> NetShareEnumAll which binds to the same function that's called when -L
> is used in smbclient.
>
> Maybe it's something with the make file?
>
>
>
> On Sun, Aug 16, 2009 at 4:36 PM, Igor Katson<[hidden email]> wrote:
>> Helmut Hullen wrote:
>>>
>>> Hallo, Igor,
>>>
>>> Du meintest am 16.08.09:
>>>
>>>
>>>>
>>>> Is it possible to list samba shares and their contents of a
>>>> particular host? --
>>>>
>>>
>>> Shell:
>>>
>>>        smbclient -N -L $particular_host
>>>
>>> shows first all shares of that particular host and then some other  stuff.
>>> Perhaps you filter some output depending on the key word "Disk"  (or
>>> "Printer").
>>>
>>> Contents: "that depends".
>>> You must have some rights.
>>>
>>> Viele Gruesse!
>>> Helmut
>>>
>>
>> Thanks.
>> Yes, I know of that shell command, but I would like to do that directly from
>> python for more flexibility, control and convenience.
>> --
>> To unsubscribe from this list go to the following URL and read the
>> instructions:  https://lists.samba.org/mailman/options/samba
>>
>
--
To unsubscribe from this list go to the following URL and read the
instructions:  https://lists.samba.org/mailman/options/samba
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: python: listing samba shares

Igor Katson-2
Thanks a lot! And what about listing the share's contents with python?

Ricardo Jorge wrote:

> Hi,
>
> There was a line missing from source4/librpc/config.mk and it wasn't
> building the Python module.
>
> Here is a patch if you can apply it:
>
>
> I don't know how it will work for you because of your server
> configuration. My Samba4 server is just for coding so it's pretty much
> all default and local.
>
>
>
>
> On Sun, Aug 16, 2009 at 5:08 PM, Ricardo Jorge<[hidden email]> wrote:
>  
>> Hi,
>>
>> I was investigating this and I believe there is actually a way with
>> Python but the problem is that the Python module with the binding is
>> not being compiled (sorry but I have no idea why).
>>
>> There should be a module called samba.dcerpc.srvsvc.
>>
>> If you look at librpc/gen_ndr/py_srvsvc.c you will find
>> NetShareEnumAll which binds to the same function that's called when -L
>> is used in smbclient.
>>
>> Maybe it's something with the make file?
>>
>>
>>
>> On Sun, Aug 16, 2009 at 4:36 PM, Igor Katson<[hidden email]> wrote:
>>    
>>> Helmut Hullen wrote:
>>>      
>>>> Hallo, Igor,
>>>>
>>>> Du meintest am 16.08.09:
>>>>
>>>>
>>>>        
>>>>> Is it possible to list samba shares and their contents of a
>>>>> particular host? --
>>>>>
>>>>>          
>>>> Shell:
>>>>
>>>>        smbclient -N -L $particular_host
>>>>
>>>> shows first all shares of that particular host and then some other  stuff.
>>>> Perhaps you filter some output depending on the key word "Disk"  (or
>>>> "Printer").
>>>>
>>>> Contents: "that depends".
>>>> You must have some rights.
>>>>
>>>> Viele Gruesse!
>>>> Helmut
>>>>
>>>>        
>>> Thanks.
>>> Yes, I know of that shell command, but I would like to do that directly from
>>> python for more flexibility, control and convenience.
>>> --
>>> To unsubscribe from this list go to the following URL and read the
>>> instructions:  https://lists.samba.org/mailman/options/samba
>>>
>>>      

--
To unsubscribe from this list go to the following URL and read the
instructions:  https://lists.samba.org/mailman/options/samba
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: python: listing samba shares

Ricardo-59
Just try using the regular python os.listdir()

Use this code:

.....

        ctr = srvsvc.NetShareInfoCtr()
        shares = conn.NetShareEnumAll(u'localhost', ctr, 0, 0)

        print "\n\n============"
        print "There are " + str(shares[0].ctr.count) + " shares"
        print "============\n"

        for i in range(0, shares[0].ctr.count):
                share_info = info = conn.NetShareGetInfo(u'localhost',
shares[0].ctr.array[i].name, 2)
                print share_info.name + "\t" + share_info.path + "\t "
+ share_info.comment

                if len(share_info.path) > 0:
                        try:
                                print os.listdir(share_info.path)
                        except:
                                pass

        print "\n\n"

.....

Just note that share_info.path is returning the path name "windows
style" (c:\blabla) so the dir listing will (most likely) fail. You'll
have to convert this if required.



On Sun, Aug 16, 2009 at 6:25 PM, Igor Katson<[hidden email]> wrote:

> Thanks a lot! And what about listing the share's contents with python?
>
> Ricardo Jorge wrote:
>>
>> Hi,
>>
>> There was a line missing from source4/librpc/config.mk and it wasn't
>> building the Python module.
>>
>> Here is a patch if you can apply it:
>>
>>
>> I don't know how it will work for you because of your server
>> configuration. My Samba4 server is just for coding so it's pretty much
>> all default and local.
>>
>>
>>
>>
>> On Sun, Aug 16, 2009 at 5:08 PM, Ricardo Jorge<[hidden email]> wrote:
>>
>>>
>>> Hi,
>>>
>>> I was investigating this and I believe there is actually a way with
>>> Python but the problem is that the Python module with the binding is
>>> not being compiled (sorry but I have no idea why).
>>>
>>> There should be a module called samba.dcerpc.srvsvc.
>>>
>>> If you look at librpc/gen_ndr/py_srvsvc.c you will find
>>> NetShareEnumAll which binds to the same function that's called when -L
>>> is used in smbclient.
>>>
>>> Maybe it's something with the make file?
>>>
>>>
>>>
>>> On Sun, Aug 16, 2009 at 4:36 PM, Igor Katson<[hidden email]> wrote:
>>>
>>>>
>>>> Helmut Hullen wrote:
>>>>
>>>>>
>>>>> Hallo, Igor,
>>>>>
>>>>> Du meintest am 16.08.09:
>>>>>
>>>>>
>>>>>
>>>>>>
>>>>>> Is it possible to list samba shares and their contents of a
>>>>>> particular host? --
>>>>>>
>>>>>>
>>>>>
>>>>> Shell:
>>>>>
>>>>>       smbclient -N -L $particular_host
>>>>>
>>>>> shows first all shares of that particular host and then some other
>>>>>  stuff.
>>>>> Perhaps you filter some output depending on the key word "Disk"  (or
>>>>> "Printer").
>>>>>
>>>>> Contents: "that depends".
>>>>> You must have some rights.
>>>>>
>>>>> Viele Gruesse!
>>>>> Helmut
>>>>>
>>>>>
>>>>
>>>> Thanks.
>>>> Yes, I know of that shell command, but I would like to do that directly
>>>> from
>>>> python for more flexibility, control and convenience.
>>>> --
>>>> To unsubscribe from this list go to the following URL and read the
>>>> instructions:  https://lists.samba.org/mailman/options/samba
>>>>
>>>>
>
>
--
To unsubscribe from this list go to the following URL and read the
instructions:  https://lists.samba.org/mailman/options/samba
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: python: listing samba shares

Igor Katson-2
Ricardo Jorge wrote:
> Just try using the regular python os.listdir()
>
> Use this code:
>
>  
Thanks for quick and detailed help, Ricardo. I'll try that.
--
To unsubscribe from this list go to the following URL and read the
instructions:  https://lists.samba.org/mailman/options/samba
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: python: listing samba shares

Igor Katson-2
In reply to this post by Ricardo-59
When just launching your code (with provided credentials), I have a
segfault. With the credentials code snippet removed, everything works
fine, and I can see the shares listing! But when changing "localhost" to
another ip address (a windows machine), it fails:

descent@descent:/usr/local/samba/lib/python2.6/site-packages$ smbclient
-N -L 192.168.37.37
Domain=[SARDINKA] OS=[Windows 5.1] Server=[Windows 2000 LAN Manager]

    Sharename       Type      Comment
    ---------       ----      -------
    IPC$            IPC       Remote IPC
    ExampleShare        Disk    

Domain=[test] OS=[Windows 5.1] Server=[Windows 2000 LAN Manager]

    Server               Comment
    ---------            -------

    Workgroup            Master
    ---------            -------
descent@descent:/usr/local/samba/lib/python2.6/site-packages$ python
/tmp/shares2.py
boooooo
(-1073741811, 'Unexpected information received')

Ricardo Jorge wrote:

> Hi,
>
> There was a line missing from source4/librpc/config.mk and it wasn't
> building the Python module.
>
> Here is a patch if you can apply it:
>
>
>
> ==================
> After recompiling I could get the share names listed using this Python
> code. Replace the credentials where necessary and server address.
>
> #!/usr/bin/env python
>
> import sys
>
> sys.path.append('/usr/local/samba/lib/python2.6/site-packages')
> sys.path.append('/usr/local/samba/lib/python2.6/site-packages/samba')
> sys.path.append('/usr/local/samba/lib/python2.6/site-packages/samba/dcerpc')
>
> from samba.dcerpc import samr, security, srvsvc
> from samba import credentials
>
> creds = credentials.Credentials()
> creds.set_username("Administrator")
> creds.set_password("x")
>
> # you must leave this line or it will cause a segmentation fault
> creds.set_workstation("")
>
> try:
>         conn = srvsvc.srvsvc('ncacn_np:localhost', credentials=creds)
> except Exception, msg:
>         print 'boooooo'
>         print str(msg)
> else:
>         ctr = srvsvc.NetShareInfoCtr()
>         shares = conn.NetShareEnumAll(u'localhost', ctr, 0, 0)
>
>         print "\n\n============"
>         print "There are " + str(shares[0].ctr.count) + " shares"
>         print "============\n"
>
>         for i in range(0, shares[0].ctr.count):
>                 print str(i) + ": " + shares[0].ctr.array[i].name
>
>         print "\n\n"
>
> I don't know how it will work for you because of your server
> configuration. My Samba4 server is just for coding so it's pretty much
> all default and local.
>
>
>
>
> On Sun, Aug 16, 2009 at 5:08 PM, Ricardo Jorge<[hidden email]> wrote:
>  
>> Hi,
>>
>> I was investigating this and I believe there is actually a way with
>> Python but the problem is that the Python module with the binding is
>> not being compiled (sorry but I have no idea why).
>>
>> There should be a module called samba.dcerpc.srvsvc.
>>
>> If you look at librpc/gen_ndr/py_srvsvc.c you will find
>> NetShareEnumAll which binds to the same function that's called when -L
>> is used in smbclient.
>>
>> Maybe it's something with the make file?
>>
>>
>>
>> On Sun, Aug 16, 2009 at 4:36 PM, Igor Katson<[hidden email]> wrote:
>>    
>>> Helmut Hullen wrote:
>>>      
>>>> Hallo, Igor,
>>>>
>>>> Du meintest am 16.08.09:
>>>>
>>>>
>>>>        
>>>>> Is it possible to list samba shares and their contents of a
>>>>> particular host? --
>>>>>
>>>>>          
>>>> Shell:
>>>>
>>>>        smbclient -N -L $particular_host
>>>>
>>>> shows first all shares of that particular host and then some other  stuff.
>>>> Perhaps you filter some output depending on the key word "Disk"  (or
>>>> "Printer").
>>>>
>>>> Contents: "that depends".
>>>> You must have some rights.
>>>>
>>>> Viele Gruesse!
>>>> Helmut
>>>>
>>>>        
>>> Thanks.
>>> Yes, I know of that shell command, but I would like to do that directly from
>>> python for more flexibility, control and convenience.
>>> --
>>> To unsubscribe from this list go to the following URL and read the
>>> instructions:  https://lists.samba.org/mailman/options/samba
>>>
>>>      

--
To unsubscribe from this list go to the following URL and read the
instructions:  https://lists.samba.org/mailman/options/samba
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: python: listing samba shares

Igor Katson-2
In reply to this post by Ricardo-59
Ricardo Jorge wrote:

> Just try using the regular python os.listdir()
>
> Use this code:
>
> .....
>
>         ctr = srvsvc.NetShareInfoCtr()
>         shares = conn.NetShareEnumAll(u'localhost', ctr, 0, 0)
>
>         print "\n\n============"
>         print "There are " + str(shares[0].ctr.count) + " shares"
>         print "============\n"
>
>         for i in range(0, shares[0].ctr.count):
>                 share_info = info = conn.NetShareGetInfo(u'localhost',
> shares[0].ctr.array[i].name, 2)
>                 print share_info.name + "\t" + share_info.path + "\t "
> + share_info.comment
>
>                 if len(share_info.path) > 0:
>                         try:
>                                 print os.listdir(share_info.path)
>                         except:
>                                 pass
>
>         print "\n\n"
>
> .....
>
> Just note that share_info.path is returning the path name "windows
> style" (c:\blabla) so the dir listing will (most likely) fail. You'll
> have to convert this if required.
>  
I would like to list remote samba shares contents (most of which are
windows machines), not the local ones. So os.listdir() cannot help here.
--
To unsubscribe from this list go to the following URL and read the
instructions:  https://lists.samba.org/mailman/options/samba
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: python: listing samba shares

Ricardo-59
> When just launching your code (with provided credentials), I have a segfault. With the credentials code snippet removed, everything > works fine, and I can see the shares listing! But when changing "localhost" to another ip address (a windows machine), it fails:

About the segfault, I don't know why it happens. I found that it
happened (and already reported it) when creds.set_workstation was
omitted. I don't know what it could be in your situation.



Try this code instead. I changed the transport type to ncacn_ip_tcp.
It seems that you didn't need the credentials to do the smbclient
listing so you can also try removing them from there.

try:
        conn = srvsvc.srvsvc('ncacn_ip_tcp:192.168.1.68', credentials=creds)
except Exception, msg:
        print 'boooooo'
        print str(msg)
else:
        ctr = srvsvc.NetShareInfoCtr()
        shares = conn.NetShareEnumAll(u'192.168.11.68', ctr, 0, 0)

        print "\n\n============"
        print "There are " + str(shares[0].ctr.count) + " shares"
        print "============\n"

        for i in range(0, shares[0].ctr.count):
                share_info = info =
conn.NetShareGetInfo(u'192.168.1.68', shares[0].ctr.array[i].name, 2)
                print share_info.name + "\t" + share_info.path + "\t "
+ share_info.comment

        print "\n\n"

>
> I would like to list remote samba shares contents (most of which are windows
> machines), not the local ones. So os.listdir() cannot help here.
>

You're correct, I forgot about that detail :) I couldn't find a
binding for this in the Python library.
--
To unsubscribe from this list go to the following URL and read the
instructions:  https://lists.samba.org/mailman/options/samba
Loading...