使用 bash 脚本在 Mac OS X 上安装驱动器(而不是使用 expect)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6555632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mount a drive on Mac OS X with bash script (and NOT use expect)
提问by lollercoaster
I'd like to mount a Samba drive in OS X using bash. This line does the trick:
我想使用 bash 在 OS X 中安装一个 Samba 驱动器。这一行可以解决问题:
mount -t smbfs //$SAMBAUSER@$ADRESS/$NAMEOFSHARE $MACOSXPATH
only one problem. I want it done without user input - which means no password can be manually put in. And I'm not going to ask my users to download fink just so they can install expect (as seen here).
只有一个问题。我希望它在没有用户输入的情况下完成 - 这意味着不能手动输入密码。而且我不会要求我的用户下载 fink 只是为了他们可以安装 expect(如这里所见)。
I tried applying the accepted solution to a similar StackOverflow problem shown hereby doing this:
我尝试通过执行以下操作将已接受的解决方案应用于此处显示的类似 StackOverflow 问题:
echo "mypassword" | mount -t smbfs //$SAMBAUSER@$ADRESS/$NAMEOFSHARE $MACOSXPATH --stdin
but no luck - that doesn't work and Mac OS X tells me I used mount command improperly:
但没有运气 - 这不起作用,Mac OS X 告诉我我错误地使用了 mount 命令:
usage: mount [-dfruvw] [-o options] [-t ufs | external_type] special node
mount [-adfruvw] [-t ufs | external_type]
mount [-dfruvw] special | node
Any suggestions? This would be easy with an expect script - but that would ruin the user experience to have that prerequisite in my mind.
有什么建议?使用预期脚本会很容易 - 但这会破坏用户体验,以便在我心中拥有该先决条件。
回答by Mark Williams
If mount(8)can't just call the mountsyscall on the filesystem, it looks for a program to help it. On FreeBSD and Mac OS X, those helper programs follow the naming convention mount_XXX, where XXXis the -targument's value.
如果mount(8)不能只调用mount文件系统上的系统调用,它会寻找一个程序来帮助它。在 FreeBSD 和 Mac OS X 上,这些帮助程序遵循命名约定mount_XXX,其中XXX是-t参数的值。
That means you want to check the mount_smbfs(8)man page, which tells us about -N:
这意味着您要查看mount_smbfs(8)手册页,它告诉我们-N:
-N Do not ask for a password. At run time, mount_smbfs reads the ~/Library/Preferences/nsmb.conf
file for additional configuration parameters and a password. If no password is found,
mount_smbfs prompts for it.
Unfortunately the man page trail ends with one for nsmb.confthat doesn't mention anything about storing passwords. On FreeBSD 8.0, at least, the solution is to put a passwordkey with a plain text(!) password value under a [SERVER:USER]heading. That would be type Caccording to the linked nsmb.confman page.
不幸的是,手册页跟踪以一个结尾,nsmb.conf因为它没有提到任何有关存储密码的内容。至少在 FreeBSD 8.0 上,解决方案是在标题下放置一个password带有纯文本(!)密码值的密钥[SERVER:USER]。这将C根据链接的nsmb.conf手册页进行输入。
So it seems that you'll want to dump a pre-configured nsmb.confinto your user's ~/Library/Preferences/directory and then call your mountcommand with -N. As far as I know you can't provide a hashed value, which is not especially awesome. I'll try to get access to a MacBook in a few hours to test this.
因此,您似乎希望将预先配置nsmb.conf的~/Library/Preferences/文件转储到用户目录中,然后mount使用-N. 据我所知,您无法提供散列值,这并不是特别棒。我将尝试在几个小时内访问 MacBook 以进行测试。
NB: This is nothow to do it with the GNU toolchain. If you're on Linux, you're probably going to be using something like mount.cifs(8). The correct solution in thatcase is the credentials=filenameoption (used after -o, of course), where filenameis a file of credentials in key=valueform, separated by newlines. See http://linux.die.net/man/8/mount.cifs
注意:这不是使用 GNU 工具链的方法。如果您使用的是 Linux,您可能会使用类似mount.cifs(8). 在这种情况下,正确的解决方案是credentials=filename选项(-o当然在 之后使用), wherefilename是key=value表单中的凭证文件,由换行符分隔。见http://linux.die.net/man/8/mount.cifs
回答by Stuart Berg
The answer in this apple support discussion threadworked for me:
这个苹果支持讨论线程中的答案对我有用:
osascript -e 'mount volume "smb://user:password@server/share"'
回答by Gordon Davisson
I'll give you 2 options. First, include the password on the command line:
我会给你2个选择。首先,在命令行中包含密码:
mount -t smbfs //$SAMBAUSER:$PASSWORD@$ADRESS/$NAMEOFSHARE $MACOSXPATH
This is not a great option because the command line (including password) is visible to anyone who happens to be logged in at the moment. Not great, but it is a possibility.
这不是一个很好的选择,因为命令行(包括密码)对当时碰巧登录的任何人都是可见的。不是很好,但这是一种可能性。
Second, use expect. The Mac OS X Hints article you linkeddates from 2002, when OS X v10.2 would've been current. While 10.2 apparently didn't include expect as a standard component, 10.6 does, and I'm pretty sure it's been included for several versions now.
其次,使用期望。您链接的Mac OS X 提示文章可以追溯到 2002 年,当时 OS X v10.2 是最新的。虽然 10.2 显然没有将 expect 作为标准组件包含在内,但 10.6 确实包含了,而且我很确定它现在已经包含在多个版本中。
#!/usr/bin/expect -f
spawn mount -t smbfs //[email protected]/myfiles /tmp/mountpoint
expect "Password:"
send "wibble\r"
wait
回答by paul
You not only can use a hashed value, you're expected to, at least in older releases. The crypt option for smbutil is not DOD-level security but as with most security, you're trying to keep the honest people honest: the bent ones will find a way. It seems to be broken in Mountain Lion but the nsmb.conf file in ~/Library/Preferences should be secure at the OS level. The /etc/nsmb.conf would override it if it exists. I'm sure there's a reason why plain text files are obsolete but didn't we go this with NetInfo? How long did it take for Apple to allow the old standbys (/etc/hosts, /etc/passwd) to be used?
您不仅可以使用散列值,至少在旧版本中可以使用。smbutil 的 crypt 选项不是 DOD 级别的安全性,但与大多数安全性一样,您试图让诚实的人保持诚实:弯曲的人会找到方法。它似乎在 Mountain Lion 中被破坏,但 ~/Library/Preferences 中的 nsmb.conf 文件在操作系统级别应该是安全的。如果 /etc/nsmb.conf 存在,它将覆盖它。我敢肯定纯文本文件已经过时是有原因的,但我们不是在 NetInfo 中采用这种方式吗?Apple 允许使用旧的备用数据库(/etc/hosts、/etc/passwd)需要多长时间?

