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

12,812

Solution 1

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.

Solution 2

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

Share:
12,812

Related videos on Youtube

codersarepeople
Author by

codersarepeople

Student @ Princeton

Updated on May 02, 2022

Comments

  • codersarepeople
    codersarepeople about 2 years

    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

    • Jonathan Leffler
      Jonathan Leffler over 13 years
      I think the problem is not in bash but in getting Samba to recognize which '@' is in the password and which '@' marks the end of the password. And I doubt there's a way to do it, but if there is, you will need to look at the Samba manuals for the information. 'Doctor, doctor, it hurts when I hit myself like this'? Or, in other words, do not use '@' in the password because it causes grief.
    • TOMKA
      TOMKA over 13 years
      Maybe try replacing all @ with %40... I don't think it'll work but maybe that's the go. Use $password=${password//@/%40} to replace them.