如何从 Mac 按需访问 SMB 网络共享?

如何从 Mac 按需访问 SMB 网络共享?

我经常在运行 Leopard 的 Mac 上使用公司网络上的 samba 网络文件共享。当我通过 shell 或 Finder 访问代表性路径时,如何按需自动挂载这些共享?

理想情况下,我希望它像通过 /Volumes/smb/[host]/[share] 之类的路径从具有 UNC 路径(又名 \\[host][share])的 Windows 访问网络共享一样简单。

我尝试使用 autofs‘可执行映射’支持,如以下链接所述:

下面是我稍微调整过的 auto.smb 脚本,但是它不起作用,我很困惑:

#!/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 }
    '

答案1

您没有过多讲述您尝试了什么以及具体为何失败,因此很难提供帮助。

  • 您是否实际创建了 creddir“/etc/auto.smb.credentials”?

  • 您是否已使用包含您的凭据的 $host 文件填充了它?请向我们展示
    ls -Al /etc/auto.smb.credentials

  • 您的 auto.smb 脚本是否可执行,并从 /etc/auto_master 引用?让我们看看 cat /etc/auto_master 和ls -Al /etc/auto.smb

  • 您的 Mac 日志中有什么有趣的东西吗?我不确定 Mac 如何记录这些内容,但也许
    egrep 'auto(mount|fs)' /var/log/*会打印一些有用的信息?

相关内容