Bash Linux Curl send headers from variable - linux

I have some script in bash.
I want to send a request to server with some headers (cloudflare block :).
I wrote this:
headers="\
-H 'Host: somesite.com' \
-H 'Accept-Language: pl,en-US;q=0.7,en;q=0.3' \
-H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' \
-H 'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0' \
-H 'Accept-Encoding: gzip, deflate' \
-H 'DNT: 1' \
-H 'Connection: keep-alive'";
curl $headers somesite.com
But in output I'm have this..
curl: (6) Couldn't resolve host 'Accept-Language'
curl: (6) Couldn't resolve host 'pl,en-US;q=0.7,en;q=0.3'
curl: (6) Couldn't resolve host 'Accept'
curl: (6) Couldn't resolve host 'text'
curl: (6) Couldn't resolve host 'User-Agent'
curl: (6) Couldn't resolve host 'Mozilla'
curl: (6) Couldn't resolve host '(Windows'
curl: (6) Couldn't resolve host 'NT'
curl: (6) Couldn't resolve host '6.1;'
How can I put headers from variable into command ?

Don't use a string for this. Trying to get quoting correct in a string like this is essentially impossible, use an array.
See http://mywiki.wooledge.org/BashFAQ/050 for more.

Related

How to send POST request from commandline?

I'm trying to send an "on" signal to a wemo smart plug. I want to send the signal from the linux command line. I know that the request is supposed to look like what I've included below, but I'm not sure what syntax to use. I tried using cURL, but couldn't quite seem to figure it out. Any help would be super appreciated!
POST /upnp/control/basicevent1
SOAPACTION: "urn:Belkin:service:basicevent:1#SetBinaryState"
Content-Type: text/xml; charset="utf-8"
Accept: */*
User-Agent: PostmanRuntime/7.15.2
Cache-Control: no-cache
Host: 192.168.1.116:49153
Accept-Encoding: gzip, deflate
Content-Length: 306
Connection: keep-alive
<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:SetBinaryState xmlns:u="urn:Belkin:service:basicevent:1">
<BinaryState>0</BinaryState>
</u:SetBinaryState>
</s:Body>
</s:Envelope>
I tried putting each of the headers in quotes after a "-H" but then I wasn't sure what to do with the headers that are already quoted. Also, it appeared to be still sending to port 80 even though I included the host header with a different port?
As far as I know, the plug has an IP address but no web address.
Post method have two part (header & body).
You should run cURL command like that:
Send Header:
curl -X POST -H 'SOAPACTION: "urn:Belkin:service:basicevent:1#SetBinaryState"' -H 'Content-Type: text/xml; charset="utf-8"' http://192.168.1.116:49153/upnp/control/basicevent1
With this method, you can head multi header POST.
Send body POST:
And also with that command you can send POST body:
curl -X POST -F '<?xml version="1.0" encoding="utf-8"?>...' http://192.168.1.116:49153/upnp/control/basicevent1
Also, they're available at the following links:
How to send body POST ,
How to send header POST
Command you want at single line:
curl -X POST -H 'SOAPACTION: "urn:Belkin:service:basicevent:1#SetBinaryState"' -H 'Content-Type: text/xml; charset="utf-8"' -H 'Accept: */*' -H 'User-Agent: PostmanRuntime/7.15.2' -H 'Cache-Control: no-cache' -H 'Host: 192.168.1.116:49153' -H 'Accept-Encoding: gzip, deflate' -H 'Content-Length: 306' -H 'Connection: keep-alive' -F '<?xml version="1.0" encoding="utf-8"?>\n<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">\n <s:Body>\n <u:SetBinaryState xmlns:u="urn:Belkin:service:basicevent:1">\n <BinaryState>0</BinaryState>\n </u:SetBinaryState>\n </s:Body>\n</s:Envelope>' http://192.168.1.116:49153/upnp/control/basicevent1
alias
If you think this is a long command, you can set as alias.
For e.g:
alias myPersonalCommandLS='ls -lthra1d'

Curl: Send request just like a browser

Searching theatres on Google returns future events in an instant panel. See the Showtimes panel below for example:
I am trying to pull Google instance information using curl. However, it seems Google somehow figures that this is a bot not a real browser request. Response received by curl has the instant panel excluded. Here is the curl call I use:
curl -H "DNT: 1" -H "UPGRADE-INSECURE-REQUESTS: 1" \
-H "Accept-Language: en-US,en;q=0.9,fa;q=0.8" \
-H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8" \
-H "REFERER: https://www.google.com/" \
-H "X-ACCEL-INTERNAL: /internal-nginx-static-location" \
-H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36" \
-v "https://www.google.com/search?q=rio+theatre&oq=rio+theatre&aqs=chrome..69i57j0l5.2382j1j7&sourceid=chrome&ie=UTF-8"
How can I modify curl to retrieve the same information as the browser does?

How to send same cookies to every curl command?

I am testing curl, and very new to this language.
let me explain what I m doing.
"http://somewebsite.com/click?param1=10&param2=523" this is the url which I am hitting in the browser and using Inspect Element and I got the following curl bash value --
curl 'http://somewebsite.com/click?param1=10&param2=523' -H 'Connection: keep-alive' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Mobile Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: en-US,en;q=0.9' -H 'Cookie: pdval=9bc5d1fa982ff4c1e1f3d224' --compressed
Now here every time I hit that url in the browser, the value of parameter "pdval" is changing.
Is there any option to read the -H values in a bash script in Linux using curl.
Any help will be great. Thank you.
Since pdval is in cookie, you can make use of -b and -c options for cookie related task.
-c, --cookie-jar < filename >
(HTTP) Specify to which file you want curl to write all cookies after
a completed operation. Curl writes all cookies from its in-memory
cookie storage to the given file at the end of operations. If no
cookies are known, no data will be written. The file will be written
using the Netscape cookie file format. If you set the file name to a
single dash, "-", the cookies will be written to stdout.
-b, --cookie < name=data >
(HTTP) Pass the data to the HTTP server in the Cookie header. It is
supposedly the data previously received from the server in a
"Set-Cookie:" line. The data should be in the format "NAME1=VALUE1;
NAME2=VALUE2".
If no '=' symbol is used in the argument, it is instead treated as a
filename to read previously stored cookie from.
So if you set the -c option, then curl will automatically stores the cookie in a file. And you have to use -b to tell curl that take cookies from that file.
So you command should be as follows:
curl 'http://somewebsite.com/click?param1=10&param2=523' -H 'Connection: keep-alive' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Mobile Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: en-US,en;q=0.9' -b /tmp/somewebsite.cookie -c /tmp/somewebsite.cookie --compressed

bash - add a variable to binary inside string

So I have a curl command in a bash script:
#!/bin/bash
curl -s 'http://eamorr.com/?' -H 'Host: eamorr.com' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:36.0) Gecko/20100101 Firefox/36.0' --data-binary $'-----------------------------1024109981087242139340277850\r\nContent-Disposition: form-data; name="target"\r\n\r\n90210\r\n-----------------------------102410998108724213934027785--\r\n'
This works fine for me.
Now, instead of 90210, I would like to try 90211
#!/bin/bash
postcode=90211
curl -s 'http://eamorr.com/?' -H 'Host: eamorr.com' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:36.0) Gecko/20100101 Firefox/36.0' --data-binary $'-----------------------------1024109981087242139340277850\r\nContent-Disposition: form-data; name="target"\r\n\r\n'$postcode'\r\n-----------------------------102410998108724213934027785--\r\n'
This won't work...
I think it's something to do with the $ in front of the --data-binary string? I tried dropping the $, but it wouldn't work.
I tried $(postcode), etc. to no avail
The problem is you now wrongly quote what follows the postcode. \r\n is not interpreted in single quotes, you need $'\r\n':
...'$postcode$'\r\n...
#!/bin/bash
postcode="90211"
curl -s 'http://eamorr.com/?' -H 'Host: eamorr.com' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:36.0) Gecko/20100101 Firefox/36.0' --data-binary $"-----------------------------1024109981087242139340277850\r\nContent-Disposition: form-data; name=\"target\"\r\n\r\n$postcode\r\n-----------------------------102410998108724213934027785--\r\n"
using quotation is a good practice, and "'$postcode'" is actually '90211' not 90211

Curl Ubuntu Post Request Doesn't receive file

So I'm trying to download ventrilo from their website
http://www.ventrilo.com/dlprod.php?id=102
As far as I'm aware what I'm showing is identical to the request that the page makes when you click accept, does anyone know why this isn't working?
curl -o ventrilo.tar.gz "http://dlx2.ventrilo.com/dl.php?server_linux_i386&8882453279444" -H
"Origin: http://www.ventrilo.com"
-H "Accept-Encoding: gzip,deflate,sdch"
-H "Accept-Language: en-US,en;q=0.8"
-H "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36"
-H "Content-Type: application/x-www-form-urlencoded"
-H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
-H "Cache-Control: max-age=0" -H "Referer: http://www.ventrilo.com/dlprod.php?id=102"
-H "Connection: keep-alive" --data "Download=I+Agree" --compressed
Notice this bold number from your POST url server_linux_i386&8882453279444. Using the number they are tracking the session.
There are two things can happen:
1) May be you have collected the url from the page's source and tried after a long time when the session is expired.
2) May be you collected the url from your pc and tried to execute at your server(or vice-versa).

Resources