omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Files App or SMB access to NAS and read filenames from it

    Pythonista
    4
    22
    14411
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • febalci
      febalci last edited by

      I have all my movie files on my NAS in an SMB shared folder. And i have this Python script which reads the movie filename and downloads the required subtitle from a local subtitle language website.

      What i am trying to do is to port this Python3 script to Pythonista. I tried direct SMB access to NAS from Pythonista which is failed. So, I used FE File Explorer to map the SMB share and now i can access to it from the Files app. When i try to get the filename of the movie file from Files app with 'dialogs.pick_document' it tries to create a local copy of the file which takes forever because of the movie file size. What i want is to just get the filename of the movie without creating a local copy, and after downloading the subtitle file, write the .txt file to the same folder with the movie on the Files app. Is there a way to do this in Pythonista?

      cvp 1 Reply Last reply Reply Quote 0
      • cvp
        cvp @febalci last edited by cvp

        @febalci See here a SMB Client

        This works for me, with your user, password and smb_name

        from SMB_client import SMB_client
        
        user = 'xxx'
        pwd = 'xxx'
        smb = SMB_client(username=user,password=pwd,smb_name='MYBOOKLIVE')
        smb.connect()
        tree_all = smb.getRemoteTree()
        print(tree_all)
        
        1 Reply Last reply Reply Quote 0
        • febalci
          febalci last edited by ccc

          @cvp ,
          That was the first thing i tried. Unfortunately i get these errors and it is not connecting:

          connecting...
          Error on line 64 NotReadyError SMB connection not authenticated
          Connect
          Error on line 88 NotReadyError SMB connection not authenticated
          

          I tried it on Nas, windows and linux shares.

          from SMB_client import SMB_client
          
          my_smb = SMB_client(username='XXXXX',password='XXXXXX',smb_name='SERVERNAME')
          print('connecting...')
          my_smb.connect()
          print('Connect')
          tree = my_smb.getRemoteTree()
          for elem in tree:
              print(elem)
                  
          #my_smb.download(path, filename)
          #my_smb.upload(path, filename)
          #my_smb.delete_remote_file(path, filename)
          #my_smb.createRemoteDir(path)
          #my_smb.removeRemoteDir(path)
          #my_smb.renameRemoteFileOrDir(path, new_path)
          
          my_smb.close()
          
          cvp 1 Reply Last reply Reply Quote 0
          • cvp
            cvp @febalci last edited by

            @febalci I got the same error on my WD NAS until I set user='' to use public share, give a try please

            I use a normal user to access in smb an USB key connected to my router and it works

            1 Reply Last reply Reply Quote 0
            • febalci
              febalci last edited by ccc

              @cvp,
              One step ahead... If i use no user or a user that does not exist, i can get the root share list. But if i try to go into a directory, even a public folder, it gives an error. Funny enough, if i use an existing username, it doesn’t even gives me the share list.

              from SMB_client import SMB_client
              
              user = ''
              pwd = ''
              smb = SMB_client(username=user,password=pwd,smb_name='FEBSTATION')
              smb.connect()
              smb.getRemoteDir('dl','*')
              #tree_all = smb.getRemoteTree('dl')
              #print(tree_all)
              
              ActiveBackupforBusiness
              Calibre_Library
              dl
              docker
              general
              homes
              music
              photo
              public
              surveillance
              Time Machine Folder
              usbshare1-1
              video
              IPC$
              web
              BROTHER
              Error on line 88 OperationFailure Failed to list dl on web: Unable to connect to shared device
              ==================== SMB Message 0 ====================
              SMB Header:
              -----------
              Command: 0x03 (SMB2_COM_TREE_CONNECT) 
              Status: 0x00000000 
              Flags: 0x00 
              PID: 221 
              MID: 9 
              TID: 0 
              Data: 40 bytes 
              b'09000000480020005c005c00460045004200530054004100540049004f004e005c00770065006200' 
              SMB Data Packet (hex):
              ----------------------
              b'fe534d4240000000000000000300000000000000000000000900000000000000dd00000000000000c897127c000000000000000000000000000000000000000009000000480020005c005c00460045004200530054004100540049004f004e005c00770065006200'
              ==================== SMB Message 1 ====================
              SMB Header:
              -----------
              Command: 0x03 (SMB2_COM_TREE_CONNECT) 
              Status: 0xC0000022 
              Flags: 0x01 
              PID: 221 
              MID: 9 
              TID: 0 
              Data: 9 bytes 
              b'090000000000000000' 
              SMB Data Packet (hex):
              ----------------------
              b'fe534d4240000000220000c00300010001000000000000000900000000000000dd00000000000000c897127c0000000000000000000000000000000000000000090000000000000000'
              
              
              cvp 2 Replies Last reply Reply Quote 0
              • cvp
                cvp @febalci last edited by

                @febalci I think the problem comes from the device, not from Pythonista smb because when I use it to access an USB key connected to my router, all is ok, even the user.
                But, of course, it is only an idea, not proved

                1 Reply Last reply Reply Quote 0
                • cvp
                  cvp @febalci last edited by

                  @febalci try

                  user = ''
                  pwd = ''
                  smb = SMB_client(username=user,password=pwd,smb_name='MYBOOKLIVE')
                  smb.connect()
                  smb.service_name = 'dl'
                  tree = smb.getRemoteTree()
                  print(tree)
                  
                  1 Reply Last reply Reply Quote 0
                  • febalci
                    febalci last edited by febalci

                    @cvp,

                    thanks for all the help. I can connect to Synology NAS now, but only through 'guest' account, that is no username or password. Also same for Windows or Linux SMB shares. For this i have to share the SMB open for all users. Still trying to find a way to connect thru an authenticated session. I keep on trying if i can make a progress i will post it here.

                    cvp 1 Reply Last reply Reply Quote 0
                    • cvp
                      cvp @febalci last edited by

                      @febalci I wait for your solution because, I have the same problem with my Western Digital NAS but not with an USB key on my router...

                      1 Reply Last reply Reply Quote 0
                      • cvp
                        cvp last edited by

                        Perhaps @brumm could help because he wrote the original version of the SMB client

                        1 Reply Last reply Reply Quote 0
                        • brumm
                          brumm last edited by

                          Maybe I can check it at the weekend. Stay tuned.

                          cvp 1 Reply Last reply Reply Quote 0
                          • cvp
                            cvp @brumm last edited by

                            @brumm Thanks. But don't sacrify your week-end 😀

                            1 Reply Last reply Reply Quote 0
                            • cvp
                              cvp last edited by

                              user = '' works
                              user = 'bad user' works<==================
                              user = 'correct user' does not work

                              1 Reply Last reply Reply Quote 0
                              • brumm
                                brumm last edited by

                                Mmmh, same here...
                                print(smb.has_authenticated) #true if empty

                                Authentication (with extended security) successful! #username+password empty
                                vs.
                                Authentication (with extended security) failed. Please check username and password. #username+password correct

                                So maybe you like to open an issue or switch to impacket???

                                cvp 1 Reply Last reply Reply Quote 0
                                • cvp
                                  cvp @brumm last edited by

                                  @brumm what is strange is that I use a smb connection to a usb key connected to my router and only a correct user is accepted, thus perhaps the code is correct but needs some parameters for the nas.
                                  In the case of the usb slot of my router, there is no free user defined.

                                  1 Reply Last reply Reply Quote 0
                                  • brumm
                                    brumm last edited by brumm

                                    The SMB dialect negotiation is always good, but the authentication fails in my case. Don't know if SMB signing deativated is okay... it is always deactivated with my smal travel router. So if you like you can check the base.py file and log the output.

                                    1 Reply Last reply Reply Quote 0
                                    • febalci
                                      febalci last edited by febalci

                                      Now i used SMBConnection from pysmb directly instead of @brumm 's SMB client. Everything is the same; can use it with guest account but could not with authenticated user. So, i tried this very same code on OSX and and on IPad Pro.

                                      Under OSX this works perfectly without any authentication problem. However, under IPad IOS; it simply doesnt work:

                                      import socket
                                      from smb.SMBConnection import SMBConnection
                                      import logging
                                      import platform
                                      
                                      username = 'admuser'
                                      password = 'admpass'
                                      server_ip = '192.168.1.20'
                                      system_name = 'SMBSERVER'
                                      share_name = 'general'
                                      
                                      logging.basicConfig(filename='example.log', level=logging.DEBUG)
                                      print (socket.gethostname())
                                      conn = SMBConnection(username=username, password=password, my_name=socket.gethostname(), remote_name=system_name, use_ntlm_v2=True)
                                      conn.connect(server_ip, 139)
                                      logging.info(conn.listPath(share_name,'/'))
                                      conn.close()
                                      
                                      

                                      OSX:

                                      INFO:SMB.SMBConnection:Authentication with remote machine "FEBSTATION" for user "febalci" will be using NTLM v2 authentication (with extended security)
                                      INFO:SMB.SMBConnection:Now switching over to SMB2 protocol communication
                                      DEBUG:SMB.SMBConnection:Received SMB2 message "SMB2_COM_NEGOTIATE" (command:0x0000 flags:0x0001)
                                      INFO:SMB.SMBConnection:SMB2 dialect negotiation successful
                                      DEBUG:SMB.SMBConnection:Received SMB2 message "SMB2_COM_SESSION_SETUP" (command:0x0001 flags:0x0001)
                                      INFO:SMB.SMBConnection:Performing NTLMv2 authentication (on SMB2) with server challenge "b'7c9f209be7a4da06'"
                                      INFO:SMB.SMBConnection:Performing NTLMv2 authentication (on SMB2) with server challenge "b'7c9f209be7a4da06'"
                                      DEBUG:SMB.SMBConnection:NT challenge response is "b'3ccca33bd0d432f9efdc8280caf3e4870101000000000000000000000000000010af8ad0e64fc2a70000000002001400460045004200530054004100540049004f004e0001001400460045004200530054004100540049004f004e0004000200000003001400660065006200730074006100740069006f006e0007000800506d040a3fe2d40100000000'" (138 bytes)
                                      DEBUG:SMB.SMBConnection:LM challenge response is "b'4582a0d54eb37d03d5f306266811552410af8ad0e64fc2a7'" (24 bytes)
                                      INFO:SMB.SMBConnection:Server supports SMB signing
                                      INFO:SMB.SMBConnection:SMB signing deactivated. SMB messages will NOT be signed.
                                      DEBUG:SMB.SMBConnection:Received SMB2 message "SMB2_COM_SESSION_SETUP" (command:0x0001 flags:0x0009)
                                      INFO:SMB.SMBConnection:Authentication (on SMB2) successful!
                                      DEBUG:SMB.SMBConnection:Received SMB2 message "SMB2_COM_TREE_CONNECT" (command:0x0003 flags:0x0001)
                                      DEBUG:SMB.SMBConnection:Received SMB2 message "SMB2_COM_CREATE" (command:0x0005 flags:0x0001)
                                      DEBUG:SMB.SMBConnection:Received SMB2 message "SMB2_COM_QUERY_DIRECTORY" (command:0x000E flags:0x0001)
                                      DEBUG:SMB.SMBConnection:Received SMB2 message "SMB2_COM_QUERY_DIRECTORY" (command:0x000E flags:0x0001)
                                      DEBUG:SMB.SMBConnection:Received SMB2 message "SMB2_COM_CLOSE" (command:0x0006 flags:0x0001)
                                      INFO:root:[<smb.base.SharedFile object at 0x10f898ac8>, <smb.base.SharedFile object at 0x10f898b70>, <smb.base.SharedFile object at 0x10f898ba8>, <smb.base.SharedFile object at 0x10f898be0>, <smb.base.SharedFile object at 0x10f898c50>, <smb.base.SharedFile object at 0x10f898c88>, <smb.base.SharedFile object at 0x10f898cc0>, <smb.base.SharedFile object at 0x10f898d30>, <smb.base.SharedFile object at 0x10f898da0>, <smb.base.SharedFile object at 0x10f898dd8>, <smb.base.SharedFile object at 0x10f898e10>, <smb.base.SharedFile object at 0x10f898e48>, <smb.base.SharedFile object at 0x10f898e80>, <smb.base.SharedFile object at 0x10f898eb8>]

                                      IOS:

                                      INFO:SMB.SMBConnection:Authentication with remote machine "FEBSTATION" for user "febalci" will be using NTLM v2 authentication (with extended security)
                                      INFO:SMB.SMBConnection:Now switching over to SMB2 protocol communication
                                      DEBUG:SMB.SMBConnection:Received SMB2 message "SMB2_COM_NEGOTIATE" (command:0x0000 flags:0x0001)
                                      INFO:SMB.SMBConnection:SMB2 dialect negotiation successful
                                      DEBUG:SMB.SMBConnection:Received SMB2 message "SMB2_COM_SESSION_SETUP" (command:0x0001 flags:0x0001)
                                      INFO:SMB.SMBConnection:Performing NTLMv2 authentication (on SMB2) with server challenge "b'f0202135e4a8887e'"
                                      INFO:SMB.SMBConnection:Performing NTLMv2 authentication (on SMB2) with server challenge "b'f0202135e4a8887e'"
                                      DEBUG:SMB.SMBConnection:NT challenge response is "b'5592e18ef198ca1331d9e4c86e035d8d010100000000000000000000000000001bddb210e8e728520000000002001400460045004200530054004100540049004f004e0001001400460045004200530054004100540049004f004e0004000200000003001400660065006200730074006100740069006f006e00070008000e278c4f40e2d40100000000'" (138 bytes)
                                      DEBUG:SMB.SMBConnection:LM challenge response is "b'f69c607446c5e5d044e3d694a49b3bab1bddb210e8e72852'" (24 bytes)
                                      INFO:SMB.SMBConnection:Server supports SMB signing
                                      INFO:SMB.SMBConnection:SMB signing deactivated. SMB messages will NOT be signed.
                                      DEBUG:SMB.SMBConnection:Received SMB2 message "SMB2_COM_SESSION_SETUP" (command:0x0001 flags:0x0001)
                                      INFO:SMB.SMBConnection:Authentication (on SMB2) failed. Please check username and password.

                                      This line on OSX has flags 0x0009 whereas in IOS it is 0x0001; Like if the password is wrong. Yeah this still gives nothing, i try to use wireshark on Ipad hoping i might catch something; but Apple changed UDID structure of newer devices, so i have to wait for the new version of XCode. At least i understand that it is directly a problem on pysmb side...

                                      cvp 2 Replies Last reply Reply Quote 0
                                      • cvp
                                        cvp @febalci last edited by

                                        @febalci I'm really sorry but I can't help, just 😢

                                        1 Reply Last reply Reply Quote 0
                                        • brumm
                                          brumm last edited by

                                          If there's no need for python3, again, I recommend the impacket library. Here you can find a working ui example. Just make sure that impacket and ntpath.py is installed.
                                          If you like to go further with pysmb, then you find more information in the Microsoft openspecs of SMB2 (Connecting to a Share by Using an SMB2 Negotiate, Message Syntax and SMB2 Packet Header - SYNC). Good luck!

                                          1 Reply Last reply Reply Quote 0
                                          • cvp
                                            cvp @febalci last edited by cvp

                                            @febalci connect with your little script to a flash drive in an usb slot of my router, log follows, hoping it could help you because I'm not enough skilled in this matter.

                                            Edit: no error even if invalid user or invalid password, thus that does not help at all, sorry

                                            INFO:SMB.SMBConnection:Authentication with remote machine "VOO-USB" for user "admin" will be using NTLM v2 authentication (with extended security)
                                            DEBUG:SMB.SMBConnection:Received SMB message "SMB_COM_NEGOTIATE" (command:0x72 flags:0x88 flags2:0xC001 TID:0 UID:0)
                                            INFO:SMB.SMBConnection:SMB dialect negotiation successful (ExtendedSecurity:False)
                                            INFO:SMB.SMBConnection:Performing NTLMv1 authentication (without extended security) with challenge "b'11a5b2121f990f0c'" and hashed password of "b'b167b87b195510e8f49cc5dc2cf310e87497d7cc991c4908'"
                                            DEBUG:SMB.SMBConnection:Received SMB message "SMB_COM_SESSION_SETUP_ANDX" (command:0x73 flags:0x88 flags2:0xC001 TID:0 UID:0)
                                            DEBUG:SMB.SMBConnection:SMB uid is now 0
                                            INFO:SMB.SMBConnection:Authentication (without extended security) successful!
                                            DEBUG:SMB.SMBConnection:Received SMB message "SMB_COM_TREE_CONNECT_ANDX" (command:0x75 flags:0x88 flags2:0xC001 TID:0 UID:0)
                                            INFO:SMB.SMBConnection:Authentication with remote machine "VOO-USB" for user "admin" will be using NTLM v2 authentication (with extended security)
                                            DEBUG:SMB.SMBConnection:Received SMB message "SMB_COM_NEGOTIATE" (command:0x72 flags:0x88 flags2:0xC001 TID:0 UID:0)
                                            INFO:SMB.SMBConnection:SMB dialect negotiation successful (ExtendedSecurity:False)
                                            INFO:SMB.SMBConnection:Performing NTLMv1 authentication (without extended security) with challenge "b'0a5f922ce062ad4c'" and hashed password of "b'55c03ccd143e02c6cdf2ae1bae2646c8cc9b240ad99f2e40'"
                                            DEBUG:SMB.SMBConnection:Received SMB message "SMB_COM_SESSION_SETUP_ANDX" (command:0x73 flags:0x88 flags2:0xC001 TID:0 UID:0)
                                            DEBUG:SMB.SMBConnection:SMB uid is now 0
                                            INFO:SMB.SMBConnection:Authentication (without extended security) successful!
                                            DEBUG:SMB.SMBConnection:Received SMB message "SMB_COM_TREE_CONNECT_ANDX" (command:0x75 flags:0x88 flags2:0xC001 TID:0 UID:0)
                                            
                                            1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post
                                            Powered by NodeBB Forums | Contributors