0

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.

example code:

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!

output:

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?

Thank you!

Sonic84
  • 931
  • 1
  • 10
  • 16

3 Answers3

2

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}"
Ferran Basora
  • 3,097
  • 3
  • 19
  • 13
  • No? -> curl -k -u "${name}:${password}" -o "~/Desktop/output.txt" "https://server.example.com/serverpage.asp?action=MakeList&User=${enduser}" – Ferran Basora Nov 08 '11 at 17:38
  • 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 Nov 09 '11 at 07:10
  • unfortunately that didn't work for me. using -u causes curl to attempt two downloads in this case. I'm not sure why.... – Sonic84 Nov 21 '11 at 20:40
1

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

Community
  • 1
  • 1
themel
  • 8,825
  • 2
  • 32
  • 31
  • 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 Nov 21 '11 at 20:38
0
curl -k -o ~/Desktop/output.txt "https://${name}:${password}@server.example.com/serverpage.asp?action=MakeList&User=${enduser}"
chemila
  • 4,231
  • 4
  • 22
  • 18