Trying to send a weekly overview of the backup size from a bash script which does a curl to send a message.
echo $(curl -s \
-X POST \
--user "aasdfasdfbc:4adgadfgsdfg" \
https://api.mailjet.com/v3/send \
-H "Content-Type: application/json" \
-d '{
"FromEmail":"noreply#bashdrsh.li",
"FromName":"Backup Notification",
"Recipients": [
{
"Email":"back.upper#bashdrsh.li"
}
],
"Subject":"['"$host"'] Backup overview",
"Text-part":"Backup on '"$host"' weekly overview\n\n '"$(find /tmp/backup/2017-07-12/ -maxdepth 1 -type f -exec ls -hls {} \; | awk '{ printf "%-40s %30s\n", $10, $6 }')"'\n"
}')
But the find command always returns with this response
find: paths must precede expression: globals-only.7z
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec|time] [path...] [expression]
{
"FromEmail":"noreply#bashdrsh.li",
"FromName":"Backup Notification",
"Recipients": [
{
"Email":"back.upper#bashdrsh.li"
}
],
"Subject":"[MYLOCAL] Backup overview",
"Text-part":"Backup on MYLOCAL weekly overview\n\n \n"
}
Running the find command on command line it returns everything as expected
# find /tmp/backup/2017-07-12/ -name '*.7z' -exec ls -hls {} \; | awk '{ printf "%-40s %30s\n", $10, $6 }'
/tmp/backup/2017-07-12/globals-only.7z 7.3K
/tmp/backup/2017-07-12/auth.7z 759
How can I include a nice formatted table into the message?
You're not using the same find expression in curl invocation and later on command line. Also, no need for ls & awk.
Try replacing your complete command substitution with:
find /tmp/backup/2017-07-12/ -name '*.7z' -printf "%-40P %30s\n"
For readability, you can store that into a variable, and use like:
output=$(find /tmp/backup/2017-07-12/ -name '*.7z' -printf "%-40P %30s\n")
echo $(curl -s \
-X POST \
--user "aasdfasdfbc:4adgadfgsdfg" \
https://api.mailjet.com/v3/send \
-H "Content-Type: application/json" \
-d '{
"FromEmail":"noreply#bashdrsh.li",
"FromName":"Backup Notification",
"Recipients": [
{
"Email":"back.upper#bashdrsh.li"
}
],
"Subject":"['"$host"'] Backup overview",
"Text-part":"Backup on '"$host"' weekly overview\n\n '"$output"'\n"
}')
Related
I'm doing a bash script, when working out such an error ... I can't figure out what the problem is. I'm not good at scripting yet.
#!/bin/bash
data=LOGIN
password=123PASSWD
note_link=$(curl -s 'https://cryptgeon.nicco.io' \
-H 'X-Requested-With: XMLHttpRequest' \
--data-urlencode "data=$data" \
--data "has_manual_pass=false&duration_hours=0&dont_ask=false&data_type=T¬ify_email=¬ify_ref=" \
| jq -r --arg arg $password '.note_link + "#" + $arg')
echo "note URL is $note_link"
curl's -s option is silencing the errors as well, but you want to see the errors in this case to be able to understand what is going wrong, so use -sS instead.
Also, jq can only parse json. If the input is not json, it will fail with the error you get. You should first try to parse the output with jq, and if it fails, display it.
#!/bin/bash
data=LOGIN
password=123PASSWD
curl_output=$(curl -sS 'https://cryptgeon.nicco.io' \
-H 'X-Requested-With: XMLHttpRequest' \
--data-urlencode "data=$data" \
--data "has_manual_pass=false&duration_hours=0&dont_ask=false&data_type=T¬ify_email=¬ify_ref=")
if note_link=$(jq -r --arg pass "$password" '.note_link + "#" + $pass' <<<"$curl_output" 2>/dev/null); then
echo "note URL is $note_link"
else
printf >&2 %s\\n "Could not parse the curl output:" "$curl_output"
fi
I've been spending hours trying to format my json post body so that my script is happy but I haven't had any luck. In my code down below, I would like to use the $utc_format variable as the value for datetime_gt key.
#!/bin/bash
utc_format=$(date -v-3d -v-5M -u +"%Y-%m-%dT%H:%M:%SZ")
curl \
-X POST \
-H "Content-Type: application/json" \
-H "X-Auth-Email: email#email.com" \
-H "X-Auth-key: $API_KEY" \
--data '{"query": "query { viewer { zones ( filter: { zoneTag_in: [ \"abcde12345\" ] } ) { firewallEventsAdaptive ( filter: { source: \"waf\" datetime_gt: \"$utc_format\" ruleId: \"123456\" action: \"simulate\" } orderBy: [ datetime_DESC ] limit: 100 ) { clientIP edgeResponseStatus metadata { key value } } } } }"}' \
https://api.api.com/ \
| python -m json.tool >> curl_results
I even tried doing something like:
generate_post_data()
{
cat <<EOF
{
<json data>
}
EOF
}
but I got an error saying
"message": "failed to recognize JSON request: 'invalid character '\\n' in string literal'"
:'(
Thank you in advance!
The message literally means you have line break in your json body. Try removing line break to see if that helps.
curl \
-X POST \
-H "Content-Type: application/json" \
-H "X-Auth-Email: email#email.com" \
-H "X-Auth-key: $API_KEY" \
--data "$(generate_post_data | tr -d '\n')" \
https://api.api.com/ \
| python -m json.tool >> curl_results
I have a JSON file and I would like to pass each object to a curl -d command :
[
{
"number":"+336770002979",
"message":"La plupart\ntest",
"sender":"BEcompany",
"date": 1539286620000
},
{
"number":"+336600000780",
"message":"La plupart\ntest",
"sender":"BEcompany",
"date": 1539286620000
},
...
]
For now I tried this
curl -X POST \
-H "X-Primotexto-ApiKey: 784155eed9d0a4d1ffdb67466" \
-H "Content-Type: application/json" \
-d #json.json \
https://api.primotexto.com/v2/notification/messages/send;
but it only reads the first object.
Edit
I fixed the [...] in the JSON and the curl command based on the comments.
You can use jq -c .[] to split the file to one array element per line, and take it from there.
jq -c .[] json.json |
while IFS= read -r fragment; do
curl -options -etc -d "$fragment" "http://url"
done
I tried to send this by email. I tried a lot of ways and always I obteined the same thing: an error.
http://goto-21.net/campaign/htmlversion?mkt_hm=0&AdministratorID=47507&CampaignID=58&StatisticID=62&MemberID=733807&s=994508d6292a660150ccc60c3f0310d4&isDemo=0
I tried with this:
curl -s --user 'api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0' \
Xhttps://api.mailgun.net/v2/samples.mailgun.org/messages \
-F from='Excited User ' \
-F to='foo#example.com' \
-F cc='bar#example.com' \
-F bcc='baz#example.com' \
-F subject='Hello' \
-F text='Testing some Mailgun awesomness!' \
-F html=' CODE HERE ' \
And this:
curl -s --user 'api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0' \
Xhttps://api.mailgun.net/v2/samples.mailgun.org/messages \
-F from='Excited User ' \
-F to='foo#example.com' \
-F cc='bar#example.com' \
-F bcc='baz#example.com' \
-F subject='Hello' \
-F text='Testing some Mailgun awesomness!' \
--form-string html=' CODE HERE ' \
But it doesnt work...Always ''syntax error''
Anyone can help me?
Thank you!
Use the following cURL command to send HTML emails using the command line.
Replace the uppercase parameter with your own and you're ready to send!
curl -s --user 'api:YOUR-API-KEY' https://api.mailgun.net/v2/YOURDOMAIN/messages -F from='YOU#YOURPROVIDER.COM' -F to=RECEIVER#PROVIDER.com -F subject='Hello World with HTML' -F html='<html><head><title>Hello</title></head><body>Hello <strong>World</strong></body></html>' -F text='Hello world'
What this does is send an email from an address attached to your domain to a receiver sending html and plain text version as a fall back!
Happy mailgunning
Best,
the origin request is looks like:
curl -s --user 'api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0' \
https://api.mailgun.net/v3/samples.mailgun.org/messages \
-F from='Excited User <excited#samples.mailgun.org>' \
-F to='devs#mailgun.net' \
-F subject='Hello' \
-F text='Testing some Mailgun awesomeness!'
but you have a big X before https.
curl -s --user 'api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0' \
-------> X <----- https://api.mailgun.net/v2/samples.mailgun.org/messages \ -F from='Excited User ' \ -F to='foo#example.com' \ -F cc='bar#example.com' \ -F bcc='baz#example.com' \ -F subject='Hello' \ -F text='Testing some Mailgun awesomness!' \ --form-string html=' CODE HERE ' \
maybe this cause an error
Let's take the following example:
curl -i -X POST \
-H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "method": "Player.Open", "params":{"item":false}}' \
http://example.com/jsonrpc
Now I want to have the boolean value of "item" be set in a shell script variable such as:
PRIVATE=false
read -p "Is this a private? (y/[n]) " -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]; then
PRIVATE=true
fi
And I want to pass in the value of PRIVATE to item. I have tried every which way but no luck. Can anyone shed some light?
You can do it this way:
curl -i -X POST \
-H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "method": "Player.Open", "params":{"item":'"$PRIVATE"'}}' \
http://example.com/jsonrpc
Instead of your existing -d ... line above, you could try the following:
-d "{\"jsonrpc\": \"2.0\", \"method\": \"Player.Open\", \"params\":{\"item\":$PRIVATE}}" \
That is: when using double quote speechmarks ("), bash substitutes values for variables referenced $LIKE_THIS (not the case for single quotes you were using). The downside is that you then need to escape any double-quotes in the string itself (using the backslash, as above).
This abomination works too.
$ npm run script -- madrid
# script
json='{"city":"'"$1"'"}'
curl -X POST -d $json http://localhost:5678/api/v1/weather