#!/bin/bash
currentTime=$(date +"%R")
currentdate=$(date +'%m/%d/%Y')
oldtime=$(date +%R -d "50 min ago")
echo "Current time : $currentTime"
echo "Current Date : $currentdate"
echo "Old time : $oldtime"
Response=$(curl -X GET -H "Authorization: Basic Token=" "http://targetserver.company.com:8080/v1/organizations/company/environments/environmentname/stats/apis?select=sum(is_error)&timeRange="$currentdate"%20"$currentTime"~"$currentdate"%20"$oldtime"")
echo -e "the response is: \n $Response"
Not getting any response? Please help how to use system date-time/current date-time in curl URL in shell-script.
it looks consistent, but not entirely sure what looks wrong. have you tried to echo the Response line to see what it looks like? how about trying it with wrapping the bash variable names in curly brace and removing the quotes in the string?
Response=$(curl -X GET -H "Authorization: Basic Token=" "http://targetserver.company.com:8080/v1/organizations/company/environments/environmentname/stats/apis?select=sum(is_error)&timeRange=${currentdate}%20${currentTime}~${currentdate}%20${oldtime}")
Related
I am trying to connection to Neptune DB and getting vertices details using CURL command. I have shell script for it. But somehow variable data is not going through it gremlin query. I have one Orgid.txt file where tenantid is present and my shell script reading the file and passing it to "name" variable
#!/bin/bash
i=1
rm VerticesCount1
while IFS= read -r line
do
name="$line"
#echo "Orgid name is "$i": $name"
curl -X POST https://<Neptune_endpoint>:<port>/gremlin -d '{"gremlin":"g.V().has(\"system.tenantId\",\"$name\").count()"}' >> VerticesCount1
#printf "\n"
echo >> VerticesCount1
((i=i+1))
done < Orgid.txt
As with your other question I tested with a simple data file and it works fine. However, note how I changed the type of quotes used by curl.
i=1
while IFS= read -r line
do
name="$line"
curl -X POST https://mydbcluster.cluster-xxxxxxxxxxxx.us-east-1.neptune.amazonaws.com:8182/gremlin -d \
"{\"gremlin\":\"g.V().has('code','$name').count()\"}"
((i=i+1))
done < values.txt
which produces
{"requestId":"4e3e80ed-efcb-40a7-b92b-366c6f391d4e","status":{"message":"","code":200,"attributes":{"#type":"g:Map","#value":[]}},"result":{"data":{"#type":"g:List","#value":[{"#type":"g:Int64","#value":1}]},"meta":{"#type":"g:Map","#value":[]}}}{"requestId":"6a269b5b-32f6-49d2-a31d-c51dd52eba29","status":{"message":"","code":200,"attributes":{"#type":"g:Map","#value":[]}},"result":{"data":{"#type":"g:List","#value":[{"#type":"g:Int64","#value":1}]},"meta":{"#type":"g:Map","#value":[]}}}
it is working fine with this code.
while IFS= read -r line
do
name="$line"
#echo "Orgid name is "$i": $name"
curl -X POST https://<Neptune_endpoint>:<port>/gremlin -d '{"gremlin":"g.V().has(\"system.tenantId\",\"'$name'\").count()"}' >> VerticesCount1
echo >> VerticesCount1
done < Orgid.txt
function creation() {
for file in *.yaml; do
name=$(echo "$file" | cut -f 1 -d '.')
echo "$name"
curl -X POST -H 'Content-Type: application/x-yaml' -u username:"$password" --data-binary "#$file" https://example.com/rules/"$name"
done
}
I have tried removing double quotes from variable names but it still doesn't work, only the name of the file which is in $name gets added in rules not the remaining content.
Can someone help me out to add the remaining content as well.
I have a hash file containing several md5 hashes.
I want to create a bash script to curl virustotal to check if the hashes are known.
#!/bin/bash
for line in "hash.txt";
do
echo $line; curl -s -X GET --url 'https://www.virustotal.com/vtapi/v2/file/report?apikey=a54237df7c5c38d58d2240xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxcc0a0d7&resource='$line'';
done
but not working.
Could you help me please?
Better use a while loop. Your for loop would only run once, because bash interpret it as a value, not a file. Try this:
while read -r line; do
echo "$line"
curl -s -X GET --url "https://www.virustotal.com/vtapi/v2/file/report?apikey=a54237df7c5c38d58d2240xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxcc0a0d7&resource=$line"
done <"/path/to/hash.txt"
Depending on certain conditions I want to use JWT else I want to provide path to certs. Thus in my shell script this is the code:
if /* some condition */
authorization='-H "'Authorization': 'Bearer' ${JWT}"'
else
authorization="--cert "${ADMIN_CERT_PATH}" --key "${ADMIN_KEY_PATH}""
Now the curl request should be:
curl -H "Authorization: Bearer 348129" for if condition
curl --cert /Users/.../admin_cert --key /Users/../admin_key .. for else path
In order to get that output I need to use the following format in my shell script for if condition
response_code="$(curl -s -o /dev/null -w "%{http_code}" "$authorization" "$status_url")"
and following format for else code:
response_code="$(curl -s -o /dev/null -w "%{http_code}" $authorization "$status_url")"
Note:
I need $authorization variable quoted in first case and unquoted in the else case.
I do not want to write 2 different curl commands instead reuse the authorization variable.
Thus, i need to modify the way I have declared my authorization variable such that I can write any one of the curl commands only once which works for both if and else cases.
curl supports a way to pass command line parameters in a file that I have used before when I have complex parameters. The idea is to place the complex command-line parameters into a simple text file and instruct curl to read parameters from it using --config parameter.
In this case the shell script would look something like the following.
#!/bin/sh
## "safely" create a temporary configuration file
curlctl=$(mktemp -q -t $(basename "$0"))
if test $? -ne 0
then
echo "$0: failed to create temporary file, exiting."
exit 75 # EX_TEMPFAIL
fi
trap 'rm "$curlctl"' 0
## write parameters used in all cases
cat>>"$curlctl" <<EOF
output = /dev/null
silent
write-out = %{http_code}
EOF
## append conditional parameters
if test "$some" = 'condition'
then
printf 'header = "Authorization: Bearer %s"\n' "$JWT" >> "$curlctl"
else
echo "cert = $ADMIN_CERT_PATH" >> "$curlctl"
echo "key = $ADMIN_KEY_PATH" >> "$curlctl"
fi
# uncomment to see what the config file looks like
# cat "$curlctl" | sed 's/^/curl config: /'
response_code=$(curl --config "$curlctl" http://httpbin.org/get)
echo "response code: $response_code"
The first few lines set up a temporary file that is deleted when the shell script exits. If you are already using trap then your cleanup will probably be more complex.
When you are using a shell that supports arrays, you can avoid the need for a temporary configuration file.
curl_opts=(-s -o /dev/null -w "%{http_code}")
if /* some condition */
curl_opts+=(-H "Authorization: Bearer $JWT")
else
curl_opts+=(--cert "$ADMIN_CERT_PATH" --key "$ADMIN_KEY_PATH")
fi
...
response_code="$(curl "${curl_opts[#]}" "$status_url")"
I have a bash script that calls "curl" to post parameters. Some of these parameters have spaces in their values, so we need to surround it with quotes when passing it to curl. Otherwise, only the first part before the space is passed as a parameter.
I tried storing the command in a variable, using escaped quotes, but I cannot get it to work in command substitution except by using eval. I can also get it working by calling command substitution directly on the string (and not storing the command in a variable).
Is it possible to store the command in a variable and use command substitution on that variable? This is what I am trying - and failing - to do in attempt 1 in the code below. I don't understand why it doesn't work:
#!/bin/bash
yesterday=$(date +"%Y-%m-%d %H:%M:%S" -d "1 day ago") #2013-09-04 01:15:51
now=$(date +"%Y-%m-%d %H:%M:%S" ) #2013-09-05 01:15:51
echo -e "INPUTS: Today: $now, Yesterday: $yesterday"
#Attempt 1: Does not work
cmd="curl -s -u scott:tiger -d url=http://localhost:8080 -d \"start=$yesterday\" -d \"end=$now\" -d \"async=y\" http://192.168.1.46:8080/cmd/doSendMinimalToServer"
output=$($cmd)
echo "Output from executing cmd variable: $output"
#Result: The quotes get passed into the HTTP POST request. This is not good.
#Attempt 2: Using eval on the variable. Works.
output_eval=$(eval $cmd)
echo "Output from eval'ing variable: $output_eval"
#Result: This works, but I would prefer not to use eval
#Attempt 3: Using Command substitution directly on the string. Works.
output_direct=$(curl -s -u scott:tiger -d url=http://localhost:8080 -d "start=$yesterday" -d "end=$now" -d "async=y" http://192.168.1.46:8080/cmd/doSendMinimalToServer)
echo "Output from executing string: $output_direct"
#Result: This works. The HTTP POST parameters correctly have spaces in their values and no quotes.
I have also tried passing in the parameters as an array, unsuccessfully.
Store your command arguments in an array and run it like this:
#!/bin/bash
yesterday=$(date +"%Y-%m-%d %H:%M:%S" -d "1 day ago") # 2013-09-04 01:15:51
now=$(date +"%Y-%m-%d %H:%M:%S" ) #2013-09-05 01:15:51
echo -e "INPUTS: Today: $now, Yesterday: $yesterday"
cmd=(curl -s -u scott:tiger -d url=http://localhost:8080 -d "start=$yesterday" -d "end=$now" -d "async=y" "http://192.168.1.46:8080/cmd/doSendMinimalToServer")
output=$("${cmd[#]}")
echo "Output from executing cmd variable: $output"
Also I think you could use date '+%F %T' for simplicity.