Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 376
codersarepeople Avatar asked Sep 01 '10 01:09

codersarepeople


2 Answers

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

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

which Bash dutifully expands to

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

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:

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

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

like image 132
zwol Avatar answered Nov 04 '22 02:11

zwol


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

like image 23
David Durham Avatar answered Nov 04 '22 00:11

David Durham