4

I would like create a python script that mounts a Windows smb share on a Mac then copies data to it. I am falling at the first hurdle, having problems with mounting the share. It should be really easy but I can't get it to work. I have trawled the internet and have a few clues:

mount -t smbfs //user@server/sharename share

This doesn't work but I have seen another solution that uses the os module for the mount command:

import os
os.system("mount -t smbfs //user@server/sharename share")

But this doesn't work either.

I have also read about creating a local mount point for the remote share to mount to. Is this true? And how would I go about this?

Many thanks for any insights

All the best

John

1
  • That python program does exactly the same as the command line
    – mmmmmm
    Feb 6, 2014 at 11:03

4 Answers 4

3

I think, with the help of Tony Williams, I have figured it out. My mount a Windows smb share python script:

import os
directory = "/Users/user.name/foldername"
if not os.path.exists(directory): os.makedirs(directory)
os.system("mount_smbfs //user.name:password@server/servershare ~/foldername")

This will check whether a local folder called foldername exists and create it if doesn't. It will then mount the smb share into it using a defined username/password.

1

I would suggest you start setting up networking using System Preferences.

Go to System Preferences/Network/Wi-Fi (or Ethernet)/Advanced/WINS and set your Workgroup to match the Windows Workgroup

Then go to Finder/Go/Connect to Server

Try to connect using smb://servername

Once you can connect you can try to automate.

1
  • 1
    Thanks for your answer. I wanted the script to mount the share then unmount it, not for me to do it manually. Thanks again. Feb 6, 2014 at 11:56
1

This is a similar answer, and one I wound up using myself.

import os

os.system("osascript -e 'mount volume \"smb://Server/Share\"'")
#Do my important functions
os.system("umount /Volumes/Share")

The pros are:

  1. Uses the keychain credentials for the current user
  2. Will wake up a sleeping Mac to attach to the smb share
  3. Will automatically create/delete the attachment directory in /Volumes

The cons are:

  1. You probably have to attach manually to the share through Finder the first time. This will establish the credentials in keychain.
  2. This might not be common, but on one of my Macs, this and open will open the server and immediately close it again. Then it works fine on a second try.

Tony had good advice: check out the man pages and then try for yourself at a shell prompt first.

0

The first thing to do is to try and get it working at the command line.

What happens when you try it at the command line?

The first thing you might notice is that mount is likely to ask for the password for user "user", which it can't easily do in a python script. Next is that "share" has to exist ready to be a mount point and exist in the directory that python thinks is the current working directory since you didn't give a complete path to it.

I would use mount_smbfs rather than mount. Check the manual page for this command. You would have a command line something like this :

mount smbfs //user:[email protected]/sharename /Volumes/Share

assuming that /Volumes/Share exists as a directory.

1
  • Thanks for your help. Yes, I can run the script in terminal and it mounts the smb share in the local defined share (I used ~/SharedFolder rather than /Volumes/Share). As I am authenticated to the domain, I don't get the password prompt. The mount_smbfs worked as well as mount -t smbfs. Thanks again Feb 6, 2014 at 11:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .