Sanitizing read entry in Bash - macos

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!

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}"

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.

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

Related

Curl HTTPS returns empty string, how to set CURLOPT_SSL_VERIFYHOST in Linux shell / bash script

I have simple shell script that uses curl to get a response from a remote URL. The issue is that I migrated the said URL to HTTPS and now curl return an empty string. It seems that setting CURLOPT_SSL_VERIFYHOST option should fix the issue but how do I set this option in a shell script? Any other options / workarounds?
#! /bin/bash
URL=$(curl -u user:pass -A "Mozilla" https://example.com/ddns/update.php)
Today, I happened to have the same issue. neither -k and --insecure nor -L worked, but then for some reason I mixed them and it worked.
I can't say why that happened, but this worked for me:
curl -kL https://...
PS: it was a Jupyter notebook server. my guess is that it uses both ssl and redirection.

Bash script and escaping special characters in password

I have a shell script which is trying to login into a box with password having special character.
sshpass -p"\"$Passw\"" ssh -tt -o StrictHostKeyChecking=no root#"${Hostname}"
I am passing Hostname and Password parameter from Jenkins. Password conatins $ characater for example n$1sachin . But I am not able to login. Can anyone please help?
I tried different option but its not working for me.
I have encounter similar problem, finally using \ to escape special char in password is work for me, such as:
from xxx)xxx#?xxx to xxx\)xxx\#\?xxx
full command like this:
sshpass -p xxx\)xxx\#\?xxx rsync -avzh ...

Mount SMB Share on macOS from Terminal having a # in Password [duplicate]

I am trying to mount a windows shared folder on Mac OSX Mavericks. A simplistic user name and password worked fine
mount -t smbfs //user2:password2#server1.mydomain.com/myproject ~/localmap
On trying out the more valid user name and password I am getting errors that parsing URL failed.
The details are
Username: mydomain\user1
Password: A%b$c#d!e#f
The command tried is
mount -t smbfs //mydomain\user1:A%b\$c\#d\!e#f#server1.mydomain.com/myproject ~/localmap
Based on what I found, $ and ! needs to be escaped. Need help on how to escape the special characters. Incidentally, using only the username without the domain seems to work in the first case
Single quotes escape shell meta-characters, a semi-colon should separate the domain controller from the credentials, and can use %40 to represent an # in the password:
mount -t smbfs '//mydomain;user1:A%b$c%40d!e#f#server1.mydomain.com/myproject' ~/localmap
Just encode your special characters.
# -> %40
$ -> %24
! -> %21
Others characters can be found here:
http://www.degraeve.com/reference/urlencoding.php
e.g.
username="someone", password="passw#rd"
Then this should work for you:
mount -t smbfs //someone:passw%40rd#server/path /Volumes/path
Use \ to escape special symbols
if you want to convert some special symbols you can write additional string, where $1 - is parameter you provide for converting
user1=$(sed -e "s/+/%2B/g;s/#/%40/g;s/_/%5F/g" <<< "$1")
and then you can use " " and call your converted variable like this $user1
Might be handy to use nodejs to encode the url stuff:
$ node -e 'console.log(encodeURIComponent("A%b$c#d!e#f"))'
A%25b%24c%40d!e%23f
Decode to go the other way:
$ node -e 'console.log(decodeURIComponent("A%25b%24c%40d!e%23f"))'
A%b$c#d!e#f
Please see https://serverfault.com/questions/309429/mount-cifs-credentials-file-has-special-character, the first answer there worked for me. Basically, you create a file with the credentials and then specify that file instead of your username and password.

Using cURL to send JSON within a BASH script

Alright, here's what I'm trying to do. I'm attempting to write a quick build script in bash that will check out a private repository from GitHub on a remote server. To do this a "hands off" as possible, I want to generate a local RSA key set on the remote server and add the public key as a Deploy Key for that particular repository. I know how to do this using GitHub's API, but I'm having trouble building the JSON payload using Bash.
So far, I have this particular process included below:
#!/bin/bash
ssh-keygen -t rsa -N '' -f ~/.ssh/keyname -q
public_key=`cat ~/.ssh/keyname.pub`
curl -u 'username:password' -d '{"title":"Test Deploy Key", "key":"'$public_key'"}' -i https://api.github.com/repos/username/repository/keys
It's just not properly building the payload. I'm not an expert when it comes to string manipulation in Bash, so I could seriously use some assistance. Thanks!
It's not certain, but it may help to quote where you use public_key, i.e.
curl -u 'username:password' \
-d '{"title":"Test Deploy Key", "key":"'"$public_key"'"}' \
-i https://api.github.com/repos/username/repository/keys
Otherwise it will be much easier to debug if you use the shell's debugging options set -vx near the top of your bash script.
You'll see each line of code (or block (for, while, etc) as it is in your file. Then you see each line of code with the variables expanded to their values.
If you're still stuck, edit your post to show the expanded values of variables for the problem line in your script. What you have looks reasonable at first glance.

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
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.
I use the cups admin at http://localhost:631/ to add printers. Encoding # as %40 worked for me.

Resources