1

how to write text files in a restricted file-share accesible by username & password in python. I was trying with the UNC of the fileshare, yet was not able to. Can Anyone help with code snippets as to how authorize as specific user in FS and write files to FS as specific user. Could be a C-user too. I thought of creating a mapping drive using python as follows:- # Drive letter: M # Shared drive path: \shared\folder # Username: user123 # Password: password import subprocess # Disconnect anything on M subprocess.call(r'net use m: /del', shell=True)

    # Connect to shared drive, use drive letter M
   subprocess.call(r'net use m: \\shared\folder /user:user123 password', shell=True)

How to do the same for Linux, I.e map a linux file system to a fileshare using python

sayan sen
  • 31
  • 5
  • Can you show us your attempts? – Lord Elrond Sep 10 '19 at 06:05
  • Sure. While I tried from my local , I created a mapping directory in my system & then wrote succesfully in that because me as a user have access to that file share. Now a scenario where I need to be running the same code in a stand alone server, i tried to do the mapping in my code such as below:- – sayan sen Sep 10 '19 at 08:32
  • though I have been trying to create another mapping directory from the same local system, i deleted the previous directory still getting an error like :- "Multiple connections to a server or shared resource by the same user, using more than one user name, are not allowed. Disconnect all previous connections to the server or shared resource and try again." – sayan sen Sep 10 '19 at 08:45

1 Answers1

1

I would suggest using the following commands if you have a Windows environment (Windows 10 or later, if not install Powershell version 5 or later, you can search for the "Win7AndW2K8R2-KB3191566-x64.zip" package from Microsoft):
In Powershell: ( You can call each line of the Powershell commands as you did with your example in Python.)

    $pass = "MyPlainTextNonSecureSecretPassword"
    $secpasswd = ConvertTo-SecureString $pass -AsPlainText -Force
    $mycreds = New-Object System.Management.Automation.PSCredential("172.1.1.111\myUserName", $secpasswd)
    New-PSDrive -Name "M" -PSProvider FileSystem -Root "\\172.1.1.111\MyFolder\MySubFolder" -Persist -Credential $mycreds  

Afterwards it is recommended to remove the mapped drive:

    Get-PSDrive M | Remove-PSDrive  

EDIT
For Linux:
This system command could be used to mount a directory.

mkdir /m
mount -t ext4 /dev/sdg1 /m -o rw

You could do it something like this:

    subprocess.Popen('mkdir /m;mount -t ext4 /dev/sdg1 /m -o rw', shell=True)  

where
- ext4 is your device file system.
- rw is your options and could be default

See this website for more options and explanations https://www.tecmint.com/mount-filesystem-in-linux/.

Roan
  • 892
  • 15
  • 27
  • I got it for windows..What if I have to map a filesystem in linux to a fileshare & access it using a C-user, how can I do it in python..I have been able to do it for windows , I.e mapping a drive to a restricted fileshare using python.. Please help – sayan sen Sep 10 '19 at 11:21
  • Added to my solution the Linux system command that can be used to mount a directory. See the link to the website that gives more information as to how to map directories in Linux. – Roan Sep 10 '19 at 11:36
  • Thanks a lot...However I am looking for a python code for the same. Also that the fileshare is restricted , hence need to pass the user credentials in the code – sayan sen Sep 10 '19 at 14:50
  • 1
    @sayansen to execute this code as a python script you can use `subprocess.popen`. See [this](https://stackoverflow.com/questions/12605498/how-to-use-subprocess-popen-python) question for details. – Lord Elrond Sep 10 '19 at 18:16
  • How about passing credentials. Not getting the exact syntax – sayan sen Sep 11 '19 at 03:36
  • Here is a link where the users discuss and gives examples of how to mount a directory with specific user rights: https://superuser.com/questions/320415/linux-mount-device-with-specific-user-rights Also here is some more information on this. https://en.wikipedia.org/wiki/Setuid#setuid_and_setgid_on_directories – Roan Sep 11 '19 at 04:47