Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I regularly use samba network file shares on a corporate network from my Mac running Leopard. How can I automount these shares on-demand when I access a representative path via a shell or the Finder?

Ideally I want it to be as easy as accessing network shares from Windows with UNC paths (aka. \\[host][share]) via a path like /Volumes/smb/[host]/[share].

I attempted to use autofs 'executable map' support as written about at these links:

Here's the auto.smb script I tweaked a bit but it failed to work and I'm stumped:

#!/bin/bash

# $Id: auto.smb,v 1.3 2005/04/05 13:02:09 raven Exp $

# This file must be executable to work! chmod 755!

# IC patch
# The problem with the original file was that it was executing smbclient -gL $key
# But $key contains the full path e.g.: server/share/subdir
# This was causing auto.smb to go to an endless loop
# This modified file corrects this problem by passing just the host to smbclient
# and in addition it provides a simple method to locate the credentials file
# The changed parts are marked as "IC patch"
# Please read the creddir variable description to see how to provide credentials files
# Note: When you search for an inexistent share, or you authentication fails the script behaves the wrong way
#       This is because smbclient does protest, it just falls back like calling it
#       as smbclient -gL host
#       A check at the smbclient output should be done to avoid this, but I haven't figured it out yet
# Ioannis Chronakis <i.chronakis at gmail dot com>

key="$1"
mountopts="-fstype=smbfs"
smbopts=""

# IC patch
# Look for credentials files under the creddir
# If $key is just the host name, then $credfile=$creddir/$host
# If $key is in the form of host/share,
# first look for a file $creddir/$host.$share
# and it does not exist, look for $creddir/$host
creddir="/etc/auto.smb.credentials"

# IC patch
# Providing a common credfile for everything, will override the above lookup method
credfile=

if [ -z "$key" ]; then
    echo host1.example.com
    echo host2.example.com
    echo host3.example.com
    echo host4.example.com
        exit
fi

# IC patch
# The problem is that the key ($1) is the full path with the host
# So there should be one key for each directory
# To solve the problem:
# Easy: Get the first part of the $1 (/host/share/...)
# Comprehensive: Look the first two (host and share)
# If key does not exist, look just for the host
host=${key%%/*}
if [[ "$key" =~ '/' ]]; then
        path=${key#*/}
fi

if [ -n "$path" ]; then
    if [[ "$path" =~ '/' ]]; then
        share=${path%%/*}
    else
        share=$path
    fi
fi

if [ -z "$credfile" ]; then
    # Search for credentials file

    # First look for $creddir/$host.$share then for $creddir/$host
    if [ -n "$share" ]; then
        credfile="$creddir/$host.$share"
        if [ ! -e $credfile ]; then
            credfile="$creddir/$host"
        fi
    else
        credfile="$creddir/$host"
        if [ ! -e $credfile ]; then
            credfile="$creddir/global"
        fi
    fi

fi

for P in /bin /sbin /usr/bin /usr/sbin
do
    if [ -x $P/smbclient ]
    then
        SMBCLIENT=$P/smbclient
        break
    fi
done

[ -x $SMBCLIENT ] || exit 1

if [ -e $credfile ]; then
    mountopts="$mountopts,credentials=$credfile"
    smbopts="-A $credfile"
else
    smbopts="-N"
fi


if [ -z "$share" ]; then
    echo $mountopts $host:/$share
    exit
fi


# IC patch
# Do not browse for $key, just the host part
$SMBCLIENT $smbopts -gL $host 2>/dev/null| awk -v key="$host" -v opts="$mountopts" -F'|' -- '
    BEGIN   { ORS=""; first=1 }
    /Disk/  { if (first) { print opts; first=0 }; sub(/ /, "\\ ", $2); sub(/\$/, "\\$", $2); print " \\\n\t " key ":/" $2 }
    END     { if (!first) print "\n"; else exit 1 }
    '
share|improve this question

migrated from serverfault.com May 6 '11 at 8:40

This question came from our site for professional system and network administrators.

1 Answer 1

You don't say much about what you tried and how exactly it failed, so it's difficult to help.

  • Have you actually created the creddir "/etc/auto.smb.credentials"?

  • Have you populated it with $host files containing your credentials? Show us
    ls -Al /etc/auto.smb.credentials

  • Is your auto.smb script actually executable, and referenced from /etc/auto_master? Let's see cat /etc/auto_master and ls -Al /etc/auto.smb

  • Is there anything interesting in your Mac's logs? I'm not sure how the Mac logs this, but maybe
    egrep 'auto(mount|fs)' /var/log/* would print something useful?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.