How to send a message from command line to an url

root's picture

There are several applications, including monitoring solutions, which are able to interpret a json or any other type of message if sent correctly and according to each application's rules.

The way to do this is limitless but I will present you below only 2 methods. They have as example a json message.

With curl

Curl is able to send any message you want and json is not exception. The only thing is some application require certains http headers and others must not get anything but the message.

An example of json message with defined http headers:

$ curl -H "Content-Type: application/json; charset=UTF-8" -X POST -d '{"value1":"message_log","id":"2","alert":"This server requires attention!!","status":"CRITICAL","machine_host":"gzlinux2","timestamp":"1231230000"}' http://192.168.69.96:8003

An example of json message sent via curl by stripping all headers:

Here you would have to first send a message with -v option, to see what you want to strip from the headers.

user@gzaix $ curl -v -H "Content-Type: application/json; charset=UTF-8" -X POST -d '{"value1":"message_log","id":"2","alert":"This server requires attention!!","status":"CRITICAL","machine_host":"gzlinux2","timestamp":"1231230000"}' http://192.168.69.96:8003
* About to connect() to 192.168.69.96 port 8003 (#0)
*   Trying 192.168.69.96... connected
* Connected to 192.168.69.96 (192.168.69.96) port 8003 (#0)
> POST / HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-debian-linux-gnu) libcurl/7.19.7 NSS/3.18 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: 192.168.69.96:8003
> Accept: */*
> Content-Type: application/json; charset=UTF-8
> Content-Length: 106
>

So we will have to get rid of User-Agent, Host, Accept, Content-Type and Content-Length. We will have to add each of these value as empty with -H option:
$ curl -H "Content-length:" -H "User-agent:" -H "Host:" -H "Accept:" -H "Content-Type:" -X POST -d '{"value1":"message_log","id":"2","alert":"This server requires attention!!","status":"CRITICAL","machine_host":"gzlinux2","timestamp":"1231230000"}' http://192.168.69.96:8003

Via /dev/tcp

Bash has a nice feature as it is capable of sending a message if you redirect it to /dev/tcp/host/port.
If you are curious about this, hit the tldp website: http://www.tldp.org/LDP/abs/html/devref1.html#DEVTCP
Many modern linuxes should have this feature already implemented.
Remember, the shell must be bash:

user@gzaix $ echo $SHELL
/bin/bash
Having said that, an example below of sending a json via /dev/tcp:

$ echo '{"value1":"message_log","id":"2","alert":"This server requires attention!!","status":"CRITICAL","machine_host":"gzlinux2","timestamp":"1231230000"}' > /dev/tcp/10.26.156.95/8003

And that concludes or article for today. Hope you've enjoyed it.

Thou shalt not steal!

If you want to use this information on your own website, please remember: by doing copy/paste entirely it is always stealing and you should be ashamed of yourself! Have at least the decency to create your own text and comments and run the commands on your own servers and provide your output, not what I did!

Or at least link back to this website.

Recent content

root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root