Desinfectar la entrada de lectura en Bash

I have a script which prompts for user credentials in order to phrase a curl command. The issue I am having is if the password has a special character it can mess up the curl command.

código de ejemplo:

curl -k -o ~/Desktop/output.txt https://$name:$password@'server.example.com/serverpage.asp?action=MakeList&User='$enduser

example inputs

name:test

password:P@55w0rd!

salida:

curl: (6) Could not resolve host: 55w0rd!@server.example.com; nodename nor servname provided, or not known

I understand that the curl command is hitting the "@" in the password and trying to connect to 55w0rd!@server.example.com in stead of server.example.com.

How do I "sanitize" the input to escape special characters?

¡Gracias!

preguntado el 08 de noviembre de 11 a las 17:11

3 Respuestas

Try to use the "-u" parameter for curl. On the other hand try to use " for start and end the parameters and finally use ${varname} format to access to variables to prevent bash escaping problems.

curl -k -u "${name}:${password}" -o "~/Desktop/output.txt" "https://server.example.com/serverpage.asp?action=MakeList&User=${enduser}"

respondido 09 nov., 11:16

No? -> curl -k -u "${name}:${password}" -o "~/Desktop/output.txt" "server.example.com/serverpage.asp?action=MakeList&User=${enduser}" - Ferran Basora

Actually, yes, sorry. I missed the -u. I'd really like to upvote, but apparently you need to edit the answer before I can do that. Maybe add the content of your comment? - themel

unfortunately that didn't work for me. using -u causes curl to attempt two downloads in this case. I'm not sure why.... - Sonic84

You want to urlencode your password (Pp%340ssword!). AFAIK there is no simple way to do it in bash, this hilo anterior has some suggestions involving Perl etc. Testing or looking at the curl source might reveal that just replacing @ con %40 is sufficient, I haven't tried for the general case.

contestado el 23 de mayo de 17 a las 14:05

I verified that typing in "P%4055w0rd!" does work with my original syntax. looks like I'll have to awk/sed the password first. Thank you! - Sonic84

curl -k -o ~/Desktop/output.txt "https://${name}:${password}@server.example.com/serverpage.asp?action=MakeList&User=${enduser}"

respondido 09 nov., 11:14

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.