Curl and Variables inside http - bash

I try to do combination of variable source and destination IP.
then I would like to send to our software to tell me if traffic is allow or not.
I am using curl but I dont know how to add variables there
I need someting like this
curl -ks -X GET 'https://tufin.com/api/path?$line_source$line_destination$line_port
but this show me is not variable
line_source="$(cat $HOME/src)"
line_destination="$(cat $HOME/dst)"
line_port="$(cat $HOME/port)"
prepare_curl=$(echo -e "'https://tufin.com/api/path?src=$line_source&dst=$line_destination&service=$line_port'
result=$(curl -k -s -X GET $prepare_curl )
echo -e " source $line_source destination $line_destination port $line_port traffic is $result"
curl: option -m: expected a proper numerical parameter
curl: try 'curl --help' or 'curl --manual' for more information

Related

How can I pass all arguments to another command, some of which are quoted and contain spaces?

I want to pass multiple arguments through to curl. Some of these arguments are quoted and contain spaces.
I have tried like this:
ARGS="http://example.org -H 'My-Header: Foo'"
curl -vvv $ARGS
But my header is not set and I get an error at the end curl: (6) Could not resolve host: Foo'.
I have also tried quoting ARGS like this:
ARGS="http://example.org -H 'My-Header: Foo'"
curl -vvv "$ARGS"
But I get curl: (3) URL using bad/illegal format or missing URL.
If I just run curl with the arguments directly, then it works fine:
curl -vvv http://example.org -H 'My-Header: Foo'
How can I pass these arguments through to curl correctly?
There is a command called eval which evaluates all the arguments into one string and then runs it as a one big command.
Try eval curl $ARGS
I recommend you to checkout eval's man page ;)

trying to use variable in bash-curl, getting curl: (3) <url> malformed

we get error on trying to use variable in curl url block, how i can fix it?
variable is
NEXUS_UPLOAD='http://mynexus.com/nexus/content/repositories/releases/ru/123/project/1.0/1.0.289/'
#!/bin/bash -v
for i in $(cat upload.list); do
content=$(curl -v -u "$NEXUS_USER":"$NEXUS_USER_PASSWORD" --upload-file "$i" "${NEXUS_UPLOAD}")
echo $content
done
error is:
* <url> malformed
* Closing connection -1
curl: (3) <url> malformed
if we have direct link in this script, curl worked correctly
#!/bin/bash -v
for i in $(cat upload.list); do
content=$(curl -v -u "$NEXUS_USER":"$NEXUS_USER_PASSWORD" --upload-file "$i" http://mynexus.com/nexus/content/repositories/releases/ru/123/project/1.0/1.0.289/)
echo $content
done
using just $NEXUS_UPLOAD without quotes getting - curl: no URL specified!
also I try to use "$NEXUS_UPLOAD" in script, but still get malformed error
Remove the curly brackets around NEXUS_UPLOAD and use -x to enable the trace option, and don't forget to "export" the variables.
You can see a list of exported variables with declare -p
#!/bin/bash -x
for i in $(cat upload.list); do
content=$(curl -v -u "$NEXUS_USER":"$NEXUS_USER_PASSWORD" --upload-file "$i" "$NEXUS_UPLOAD")
echo $content
done
Here's a link explaining when they are useful.
For example:
i=image.jpg
convert $i ${i%jpg}png

How i can safe the curl result in a bash variable

When i run the command
curl -d "param1=value1&param2=value2" -X POST https://xxx.xxxx.de/xx/xx.php 2>/dev/null on the normal command line i get the requested result {"success":false,"cause":"Token needed"}.
I need this result on a bash script but when i try to run it
curl = "$(curl -d "param1=value1&param2=value2" -X POST https://xxx.xxxx.de/xx/xx.php 2>/dev/null)"
echo $curl
I don't recieve the requested result i recieve this
[1/2]: "success":false --> <stdout>
--_curl_--"success":false
curl: (3) URL using bad/illegal format or missing URL
[2/2]: "cause":"Token needed" --> <stdout>
--_curl_--"cause":"Token needed"
curl: (3) URL using bad/illegal format or missing URL
How i can use the correct result in my bash script ?
Your command is not a variable assignment, it tries to executes curl with arguments = and the output of the command substitution. Remove the space characters before and after = and you may omit the quotes around the command substitution (this is one of the few occasions where quotes are not needed).
curl=$(curl -d "param1=value1&param2=value2" -X POST https://xxx.xxxx.de/xx/xx.php 2>/dev/null)
echo "$curl"

How to retrieve error code from cURL on shell

I know a similar question was posted, but I can't get it to work on my machine.
I tried the 1st answer from the mentioned question, i.e. response=$(curl --write-out %{http_code} --silent --output /dev/null servername) and when I echo $response I got 000 [Not sure if that is the desired output].
However, when trying to do so with my cURL command, I get no output.
This is my command:
curl -k --silent --ftp-pasv --ftp-ssl --user C:is_for_cookies --cert localcert_cert.pem --key certs/localcert_pkey.pem ftps://10.10.10.10:21/my_file.txt
and I use it with
x=$(curl -k --silent --ftp-pasv --ftp-ssl --user C:is_for_cookies --cert localcert_cert.pem --key certs/localcert_pkey.pem ftps://10.10.10.10:21/my_file.txt)
but when I try to echo $x all I get is a newline...
I know the cURL is failing, because when I run the same command, without --silent, I get curl: (7) Couldn't connect to server
This Q is tagged with both sh, bash because I've tried it on both with same results
I found this option which kind of helps (but I still don't know how to assign it to a variable, which should be easier than this...):
--stderr <file>
Redirect all writes to stderr to the specified file instead. If the file name is a plain '-', it is instead written to stdout.
If this option is used several times, the last one will be used.
When I use it like this:
curl -k --silent -S --stderr my_err_file --ftp-pasv --ftp-ssl --user C:is_for_cookies --cert localcert_cert.pem --key certs/localcert_pkey.pem ftps://10.10.10.10:21/my_file.txt
I can see the errors (i.e. curl: (7) Couldn't connect to server) inside that file.
I used --silent to suppress all output, and -S to un-suppress the errors, and the --stderr <file> to redirect them

curl return HTTP code 503 when called from a script

I'm try to call some REST API and get the resulting HTTP code using curl. If in a terminal I type :
curl -s -o /dev/null -I -w '%{http_code}' -X POST 'http://localhost/gitlab/api/v3/projects?private_token=my_private_token&name=blabla' -H 'Content-Length: 0'
It works and return me the HTTP code 201 ("created"). Now I try to use this command in a bash script replacing a part of the url with variable:
echo "Enter base URL :"
read gitlab_url # Here I type 'http://localhost/gitlab', to generate the same URL as in first code snippet
code_status=$(curl -s -o /dev/null -I -w '%{http_code}' -X POST '$gitlab_url/api/v3/projects?private_token=my_private_token&name=blabla' -H 'Content-Length: 0')
echo "$code_status"
And then it returns me the HTTP code 503 ("Service Unavailable"). To see if there is any differences between the "hard coded" URL and the generated one, I do :
echo "curl -s -o /dev/null -I -w '%{http_code}' -X POST '$gitlab_url/api/v3/projects?private_token=my_private_token&name=blabla' -H 'Content-Length: 0'"
# Output :
curl -s -o /dev/null -I -w '%{http_code}' -X POST 'http://localhost/gitlab/api/v3/projects?private_token=my_private_token&name=blabla' 'Content-Length: 0'
And if I execute this in a terminal directly, it works and return me 201. So: why do this command fails if I use it in a script ? Is there anything I missed ?
It was a proxy problem. If I use curl -v .... I can see the following output:
When curl is typed directly in terminal I have :
* About to connect() to localhost port 80 (#0)
* Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 80 (#0)
And when I used it into a bash script I get :
* About to connect() to proxy proxy.my.company port xxx (#0)
* Trying xx.xx.xx.xx... connected
* Connected to proxy.my.company (xx.xx.xx.xx) port xxx (#0)
So to fix it I added this in the top of my script :
export no_proxy=localhost,127.0.0.1
export http_proxy=""
I am very surprised to have to do this, because I already have an environment var no_proxy who already reference localhost and 127.0.0.1
Try to run it as bash -x script.sh.

Resources