matrixratrix
Posts: 10
Joined: Sat Sep 15, 2018 10:39 am

os.path to network pc

Sat Sep 15, 2018 10:51 am

Hello,

I'm pretty new at this and I'm still trying to get into the basics, for now I came across a problem that is burning my head.

So, I have this Python script:

Code: Select all

import os
if os.path.exists ("\\\\192.168.1.3\\maq//"):
  print ("OKK")
else:
  print ("NOT OKK")
The Ip 192.168.1.3 is one pc on network that have a share folder.

This script return "true" on any pc on network (windons os), but the same script in raspberry always returns false,

I can only check paths inside raspberry (Like "/home/pi" return true), I can not check paths in another pc connected to the network that
have that has shared folders.


Any special reason? do I have to enable some output permission on raspian?

hippy
Posts: 4031
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: os.path to network pc

Sat Sep 15, 2018 5:52 pm

Beyond saying that Raspbian / Linux probably doesn't include Microsoft Windows network share access by default, and you might need to set up Samba or something to handle it, I can't give you an answer but can confirm what you are seeing with Raspbian Stretch, Python 2.7 -

Code: Select all

import os

path = "//192.168.0.207/Android"

if os.path.isdir(path)  : print "Okay isdir("+path+")"
else                    : print "FAIL isdir("+path+")"

if os.path.exists(path) : print "Okay exists("+path+")"
else                    : print "FAIL exists("+path+")"
Run on Windows XP with the "Android" share on a Windows 7 PC, absolutely fine "Okay" in both cases.

On Raspbian "FAIL" in both cases".

Unsurprisingly "Dir \\192.168.0.207\Android" works on XP, "ls //192.168.0.207/Android" doesn't.

Update: This worked for me ...

Code: Select all

[email protected]:~$ mkdir -p ./win
[email protected]:~$ sudo mount //192.168.0.207/Android ./win
Password for [email protected]//192.168.0.207/Android:
[email protected]:~$ cd ./win
[email protected]:~/win$ ls
Android Studio     Projects - App Inventor 2  <redacted>
[email protected]:~/win$                                         
I just pressed return for the password as there is none.

I'm guessing you could probably handle that programmatically within your Python code, check the return code to see if the mount succeeded or not.

matrixratrix
Posts: 10
Joined: Sat Sep 15, 2018 10:39 am

Re: os.path to network pc

Mon Sep 17, 2018 11:09 am

Hello,

Thank you for replay,

Your idea is good, however I'll need to move files to this location, so I'll find the same problem later.

Maybe the solution is to enable some permission?


Thanks

ghans
Posts: 7819
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: os.path to network pc

Mon Sep 17, 2018 12:05 pm

This simply doesn't work on Linux. Before trying to open, read, write and close files from Windows network shares inside a program, you need to explicitly configure them for the whole system.

After doing that once, they will be automatically mounted and dismounted on demand within milliseconds. This functionality is part of systemd and not the Linux kernel per se and therefore rather new in the context of Raspbian Linux.

https://unix.stackexchange.com/question ... th-systemd

So if you are doubting that you can access a Windows network share, you have to query systemd. Trying to open files and paths won't get you nowhere on Linux.

ghans
Last edited by ghans on Mon Sep 17, 2018 12:16 pm, edited 1 time in total.
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

matrixratrix
Posts: 10
Joined: Sat Sep 15, 2018 10:39 am

Re: os.path to network pc

Mon Sep 17, 2018 12:13 pm

Ok,

Excuse my ignorance in this, but:


On my network I have a pc using "Centos os", is not Windons so, is there any way to check and move files more easily to the specific path?


Thanks
Ricardo

ghans
Posts: 7819
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: os.path to network pc

Mon Sep 17, 2018 12:18 pm

The files you want to access are stored on a CentOS machine ?

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

matrixratrix
Posts: 10
Joined: Sat Sep 15, 2018 10:39 am

Re: os.path to network pc

Mon Sep 17, 2018 12:30 pm

Sim, is on CentOs machine .

hippy
Posts: 4031
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: os.path to network pc

Mon Sep 17, 2018 1:05 pm

matrixratrix wrote:
Mon Sep 17, 2018 11:09 am
Your idea is good, however I'll need to move files to this location, so I'll find the same problem later.
No; you are just creating a 'fake local directory' and linking that local Linux directory to the remote share. Then the files and sub-directories of the remote share can be seen and accessed as if in that local Linux directory.

The files don't have to be moved anywhere, just have to be in the remote share directory, with the right permissions to be accessed, read or written.

If the files aren't already in that remote share I am not sure what you are trying to achieve by accessing that remote share.

ghans
Posts: 7819
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: os.path to network pc

Mon Sep 17, 2018 1:18 pm

If your server is running CentOS instead of Windows, you still need to configure network share acess on the Raspberry Pi once.
Only then you can check if the connection is working.

Furthermore, you need to install file server sofware on CentOS. For Windows File Sharing the software is called "Samba". For SSHFS it is called "OpenSSH". For NFS it is the NFS kernel server (?).

The simplest solution is probably to install the OpenSSH server software.

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

hippy
Posts: 4031
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: os.path to network pc

Mon Sep 17, 2018 2:17 pm

ghans wrote:
Mon Sep 17, 2018 1:18 pm
If your server is running CentOS instead of Windows, you still need to configure network share acess on the Raspberry Pi once.
Perhaps - That's beyond my skill level and experience - unless you mean mounting on the Pi.

The OP did say "This script return "true" on any pc on network (windons os), but the same script in raspberry always returns false" so I would have guessed that if they can access their CentOS share from Windows they would equally be able to access it from a Pi when treating that CentOS PC as if another Windows PC. I might be wrong on that though.

matrixratrix
Posts: 10
Joined: Sat Sep 15, 2018 10:39 am

Re: os.path to network pc

Thu Sep 20, 2018 4:25 pm

I have already seen that the only solution is to use the mount command.


I can do put the code in boot but, if for some reason the network is not accessible (on boot), this unit will not be mounted, and this can be a problem for me.

My ignorant solution is to build my Python script with a command to mount the drive.

Then it would be:

if the drive is not mount:
mount unit


And this drive has to be mounted with root privileges, otherwise I can not move files to the shared folder

I've looked, but I can not find any python commands to mount the drive.

Any idea

hippy
Posts: 4031
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: os.path to network pc

Thu Sep 20, 2018 4:44 pm

matrixratrix wrote:
Thu Sep 20, 2018 4:25 pm
I've looked, but I can not find any python commands to mount the drive.
This seems to have worked for me -

Code: Select all

import os
import time

def Execute(cmd):
  print( 'Executing ' + cmd + ' ...' )
  print( os.popen(cmd).read().strip() )

Execute( 'mkdir -p ./win' )
Execute( 'sudo mount -o user="",password="" //192.168.0.207/Android ./win' )
Execute( 'ls -al ./win' )
time.sleep(2)
Execute( 'sudo umount ./win' )
Execute( 'rmdir ./win' )
Note there needs to be a delay before the 'umount' and 'rmdir' or you'll get 'device is busy' errors or similar.

Code: Select all

[email protected]:~$ python pymount.py
Executing mkdir -p ./win ...

Executing sudo mount -o user="",password="" //192.168.0.207/Android ./win ...

Executing ls -al ./win ...
total 8
drwxr-xr-x  2 root root 4096 Aug 22 23:54 .
drwxr-xr-x 41 pi   pi   4096 Sep 20 17:36 ..
drwxr-xr-x  2 root root    0 Aug 22 23:09 Android Studio
drwxr-xr-x  2 root root    0 Sep 16 21:31 Projects - App Inventor 2
<redacted>
Executing sudo umount ./win ...

Executing rmdir ./win ...

[email protected]:~$

matrixratrix
Posts: 10
Joined: Sat Sep 15, 2018 10:39 am

Re: os.path to network pc

Fri Sep 21, 2018 7:26 am

Thank you so much, it was so simple that I even felt bad. :roll: :oops:

hippy
Posts: 4031
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: os.path to network pc

Fri Sep 21, 2018 1:29 pm

matrixratrix wrote:
Fri Sep 21, 2018 7:26 am
Thank you so much, it was so simple that I even felt bad. :roll: :oops:
I wouldn't. It's one thing saying it should be possible; it's quite another doing it. And doing it has taught me a thing or two, beyond the actual issue raised. Pi purpose served, again.

I have some code on my PC doing much the same as you and I hadn't even thought about that becoming an issue when I moved that code to a Pi so I am very grateful you mentioned it.

Return to “Python”