如何转义 smb:// URL 密码字段中的 @ 符号
我正在尝试编写一个 bash 脚本,在其中连接到 samba 服务器,方法是获取用户名和密码,然后输入 $username:$password@SERVERNAME。
但是,如果密码中包含 @,则此操作将会失败。有没有办法在bash中转义密码中的@?
提前致谢
更新: 我正在设置此网络打印机
lpadmin -p PRINTER -v smb://$username:$password@SERVER -E
,它可以工作,除非 $password 中有 @ 符号; $username 和 $passwords 变量是通过读取 stdin 获得的
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.
However, this will fail if the password has an @ in it. Is there a way to escape the @ out of the password in 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
啊,不,这实际上不是引用 Bash 的问题,而是引用 Samba 的问题。你有这样的:
Bash 尽职尽责地扩展到
,然后 Samba 客户端库 认为密码以第一个 @ 符号结尾,并且它应该连接到名为
rd@server
的服务器,不要介意您实际上无法将该名称放入 DNS 中。lpadmin
来自 CUPS,而不是 Samba (这是它的手册页),并且稍微阅读一下这些文档,我认为您也许可以使用这种替代语法:我很惊讶将
@
转义为%40但
不起作用。在我看来,这像是 samba 客户端库中的一个错误。Ah, no, this isn't actually a matter of quoting for Bash, but quoting for Samba. You have this:
which Bash dutifully expands to
and then the Samba client library thinks 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.lpadmin
comes 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:I'm surprised escaping
@
as%40
doesn't work, though. Looks like a bug in the samba client library to me.我使用 http://localhost:631/ 处的 cups 管理来添加打印机。将 @ 编码为 %40 对我有用。
I use the cups admin at http://localhost:631/ to add printers. Encoding @ as %40 worked for me.