I run this command from the CLI and it works fine...
curl -H Content-Type:text/plain -vLk https://10.42.0.197/exec/show%20ver --user chartley:<pw omitted>
Now when I put it into a bash script I get the following...
* About to connect() to 10.42.0.197 port 443 (#0)
* Trying 10.42.0.197... connected
* Connected to 10.42.0.197 (10.42.0.197) port 443 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
* warning: ignoring value of ssl.verifyhost
* skipping SSL peer certificate verification
* SSL connection using TLS_RSA_WITH_RC4_128_SHA
* Server certificate:
* subject: CN=ASA Temporary Self Signed Certificate
* start date: Jul 18 20:53:46 2013 GMT
* expire date: Jul 16 20:53:46 2023 GMT
* common name: ASA Temporary Self Signed Certificate
* issuer: CN=ASA Temporary Self Signed Certificate
* Server auth using Basic with user 'chartley'
> GET /exec/show%20version HTTP/1.1
> Authorization: Basic
> User-Agent: Firefox
> Host: 10.42.0.197
> Accept: */*
> Content-Type:text/plain
> < HTTP/1.1 401 Unauthorized < Date: Tue, 04 Apr 2017 22:06:53 UTC < Connection: close < Content-Type: text/html < Expires: Thu, 16 eb
1989 00:00:00 GMT
* Authentication problem. Ignoring this. < WWW-Authenticate: Basic realm="Authentication" < <HEAD><TITLE>Authorization
Required</TITLE></HEAD><BODY><H1>Authorization Required</H1>Browser
not authentication-capable or authentication failed.</BODY>
* Closing connection #0
I had the curl command echoed out with variable expansion performed and it's character for character with the command that works on the CLI.
What am I missing?
Here is the script
#!/usr/bin/bash
IFS=$'\n'
echo "Gimme yo password foo!!"
read -s pass
pass=$(echo $pass | sed 's/[(\!|\@|\#|\$|\%|\^|\&|\*|\(|\))&]/\\&/g')
if [[ "$2" =~ [:space:] ]];
then
CMD=`echo $2 | sed 's/ /\%20/g'`
#echo "space matched"
#echo "$2"
fi
if [[ "$CMD" =~ */* ]];
then
CMD=`echo $2 | 's/[\/]/\%2f/g'`
#echo "Slash matched"
#echo "$2"
fi
curl -H Content-Type:text/plain -vLk https://$1/exec/$CMD --user "$USER:$pass"
... and it is run as such... ASA_do 10.42.0.197 "show ver"
Here is the output having added "set -x" in the bash script...
[chartley@s324phx-syslog ~]$ ASA_do 10.42.0.197 "show version"
+ echo 'Gimme yo password foo!!'
Gimme yo password foo!!
+ read -s pass
++ echo '<omitted>'
++ sed 's/[(\!|\@|\#|\$|\%|\^|\&|\*|\(|\))&]/\\&/g'
+ pass='<pw omitted>'
+ [[ show version =~ [:space:] ]]
++ echo 'show version'
++ sed 's/ /\%20/g'
+ CMD=show%20version
+ [[ show%20version =~ */* ]]
+ curl -H Content-Type:text/plain -vLk https://10.42.0.197/exec/show%20version --user 'chartley:<pw omitted>'
* About to connect() to 10.42.0.197 port 443 (#0)
* Trying 10.42.0.197... connected
* Connected to 10.42.0.197 (10.42.0.197) port 443 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
* warning: ignoring value of ssl.verifyhost
* skipping SSL peer certificate verification
* SSL connection using TLS_RSA_WITH_RC4_128_SHA
* Server certificate:
* subject: CN=ASA Temporary Self Signed Certificate
* start date: Jul 18 20:53:46 2013 GMT
* expire date: Jul 16 20:53:46 2023 GMT
* common name: ASA Temporary Self Signed Certificate
* issuer: CN=ASA Temporary Self Signed Certificate
* Server auth using Basic with user 'chartley'
> GET /exec/show%20version HTTP/1.1
> Authorization: Basic <omitted>
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.18 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: 10.42.0.197
> Accept: */*
> Content-Type:text/plain
>
< HTTP/1.1 401 Unauthorized
< Date: Thu, 06 Apr 2017 20:39:38 UTC
< Connection: close
< Content-Type: text/html
< Expires: Thu, 16 Feb 1989 00:00:00 GMT
* Authentication problem. Ignoring this.
< WWW-Authenticate: Basic realm="Authentication"
<
<HEAD><TITLE>Authorization Required</TITLE></HEAD><BODY><H1>Authorization Required</H1>Browser not authentication-capable or authentication failed.</BODY>
* Closing connection #0
This is the script with it working using eval...
#!/usr/bin/bash
set -x
echo "Gimme yo password foo!!"
IFS=$'\n' read -r -s -p 'Password:' pass
pass=$(echo $pass | sed 's/[(\!|\@|\#|\$|\%|\^|\&|\*|\(|\))&]/\\&/g' | sed "s/'//g")
if [[ "$2" =~ [:space:] ]];
then
CMD=`echo $2 | sed 's/ /\%20/g'`
#echo "space matched"
#echo "$2"
fi
if [[ "$CMD" =~ */* ]];
then
CMD=`echo $2 | 's/[\/]/\%2f/g'`
#echo "Slash matched"
#echo "$2"
fi
eval curl -H Content-Type:text/plain -vLk https://$1/exec/$CMD --user "$USER:$pass"
type curl, andwhich curl.set -xcommand on second line of your script and show us the output.username:passwordand nothing more. You will see, the command will work as in plain command-line.