0

Below is my bash script:

#!/bin/bash
message=$1
hostname=$2
appname=$3
severity=$4

data='{"action":"EventsRouter","method":"add_event","data":[{"summary":"'$message'","device":"'$hostname'","message":"message","component":"'$appname'","severity":"'$severity'","evclasskey":"nxlog","evclass":"nxlog","monitor":"localhost"}],"type":"rpc","tid":2}'

echo "Total number of args : $#"

echo "message = $message"
echo "hostname = $hostname"
echo "appname = $appname"
echo "data = $data"

curl -u "Eve:Welcome666" -k "https://myurlcom/zport/dmd/evconsole_router" -d $data -H "Content-Type:application/json"

and my error is:

Total number of args : 4
message = mes
hostname = hos
appname = sev
data = {"action":"EventsRouter","method":"add_event","data":[{"summary":"mes","device":"hos","message":"message","component":"sev","severity":"ap","evclasskey":"nxlog","evclass":"nxlog","monitor":"localhost"}],"type":"rpc","tid":2}

{"uuid": "9b66e70d-44fc-463d-a347-786fe5502c13", "action": "EventsRouter", "result": {"msg": "Failed to create event: NoRouteException: Reply code: 312, Reply text: NO_ROUTE, Exchange:zenoss.zenevents.raw, Routing key: zenoss.zeneventnxlog", "type":"exception", "success": false}, "tid": 2, "type": "rpc", "method": "add_event"}

However, when i curl a data directly, it is working.

curl -u Eve:Welcome666 -k https://myurl.com/zport/dmd/evconsole_router -d '{"action":"EventsRouter", "method":"add_event","data":[{"summary":"test","device":"test","message":"msg","component":"testhost","severity":"5", "evclasskey":"nxlog", "evclass":"/nxlog/perf","monitor":"localhost"}],"type":"rpc","tid":2}' -H "Content-Type:application/json"

{"uuid": "1654584c-5f86-489e-a5e7-35d45e462066", "action": "EventsRouter", "result": {"msg": "Created event", "success": true},"tid": 2, "type": "rpc", "method": "add_event"}
Community
  • 1
  • 1
AlisonGrey
  • 497
  • 1
  • 7
  • 23

1 Answers1

0

I think you are missing quotes around $data in the script, the correct form is:

curl -u "Eve:Welcome666" -k "https://myurlcom/zport/dmd/evconsole_router" -d "'$data'" -H "Content-Type:application/json"
7af03dec0b
  • 11
  • 4
  • This will make the name $data as a string, printing $data as the value and not it's contents in the bash. Not the result I'm expecting, the json wont get accepted. – AlisonGrey May 14 '20 at 17:31
  • you can mix double and single quote: https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash aa='"as":"bb"' ; echo $aa ; echo "'$aa'" – 7af03dec0b May 14 '20 at 17:37
  • I did try "'$data'" which considered the whole string as $data and not the one inside – AlisonGrey May 14 '20 at 17:44
  • `"'$data'"` would include literal single quotes, which you probably don't want. – Benjamin W. May 14 '20 at 18:10
  • @SPishere `"'$data'"` would *not* expand to literally `$data`, but to `''`. If it expands to `$data` for you, there is some outer single quote you're not showing. – Benjamin W. May 14 '20 at 18:11
  • @Benjamin Okay I see your point. I was having another quote placed by mistake. However, if I get this, my output says that the request body is un jsonable. – AlisonGrey May 15 '20 at 03:59