bash 如何在 smb:// URL 的密码字段中转义 @ 符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3614444/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 22:33:30  来源:igfitidea点击:

How to escape an @ sign in the password field of an smb:// URL

bashsambaescaping

提问by codersarepeople

I'm trying to write a bash script in which I connect to a samba server, by getting the username and password, then saying $username:$password@SERVERNAME.

我正在尝试编写一个 bash 脚本,在其中连接到 samba 服务器,获取用户名和密码,然后说 $username:$password@SERVERNAME。

However, this will fail if the password has an @ in it. Is there a way to escape the @ out of the password in bash?

但是,如果密码中有@,这将失败。有没有办法在bash中将@从密码中转义出来?

Thanks in advance

提前致谢

Update: I'm setting up this network printer

更新:我正在设置这台网络打印机

lpadmin -p PRINTER -v smb://$username:$password@SERVER -E

and it works except in the case that $password has an @ sign in it; the $username and $passwords variables are gotten from reading stdin

它可以工作,除非 $password 中有 @ 符号;$username 和 $passwords 变量是通过读取标准输入获得的

采纳答案by zwol

Ah, no, this isn't actually a matter of quoting for Bash, but quoting for Samba. You have this:

啊,不,这实际上不是为 Bash 引用的问题,而是为 Samba 引用的问题。你有这个:

lpadmin -p PRINTER -v smb://$username:$password@SERVER -E

which Bash dutifully expands to

Bash 尽职尽责地扩展到

lpadmin -p PRINTER -v smb://alice:passw@rd@SERVER -E

and then the Samba client librarythinks the password ends at the first @ sign and it's supposed to connect to a server named rd@server, never mind that you can't actually put that name in the DNS.

然后Samba 客户端库认为密码以第一个 @ 符号结尾,并且它应该连接到名为 的服务器rd@server,别介意您实际上不能将该名称放入 DNS 中。

lpadmincomes from CUPS, not from Samba (here is its manpage) and, reading through those docs a bit, I think you may be able to use this alternate syntax:

lpadmin来自 CUPS,而不是来自 Samba(这是它的手册页),并且稍微通读一下这些文档,我认为您可以使用以下替代语法:

lpadmin -p PRINTER -U "${username}%${password}" -v smb://SERVER -E

I'm surprised escaping @as %40doesn't work, though. Looks like a bug in the samba client library to me.

不过,我很惊讶逃避@%40行不通的。对我来说看起来像是 samba 客户端库中的一个错误。

回答by David Durham

I use the cups admin at http://localhost:631/to add printers. Encoding @ as %40 worked for me.

我使用http://localhost:631/上的 cups admin添加打印机。将@ 编码为 %40 对我有用。