How to SSH a curl command - linux

env = GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
Situation:
SystemA=No internet. SystemB=Yes internet.
SystemA has a log file. SystemA wants SystemB to send a curl command for him.
SystemA$
ssh SystemB curl -X POST -H "Content-type: application/json" -d "$data" $hook
= fail
SystemB$
curl -X POST -H "Content-type: application/json" -d "$data" $hook = success
How do I achieve this without SystemA 'scp'ing the log file to SystemB?
It's heavily schedule related so I want SystemA let SystemB work.
EDIT:
I narrowed down the problem :
On SystemB:
curl -X POST -H "Content-type: application/json" -d '{$data}' $hookurl = success
curl -X POST -H "Content-type: application/json" -d {$data} $hookurl = fail
So when I type in SystemA
ssh SystemB curl -X POST -H "Content-type: application/json" -d "{$data}" $hookurl
It actually runs with -d {$data} on SystemB. How can I fix this?
Update:
ssh SystemB curl -X POST -H "Content-type: application/json" -d "'{$data}'" $hookurl
did work and actually sent data to url,
but curl: (6) Could not resolve host: application; Unknown error occurred again.

You can use this command :
ssh SystemB /bin/bash <<< "$(declare -p data hook);"'curl -X POST -H "Content-type: application/json" -d "$data" "$hook"'
"$(declare -p data hook);" takes variable definitions from SystemA and passes them to SystemB

Use sshfs perhaps
on systemB
sshfs -p'ssh port' -o password_stdin user#systemA:/home/user/dir /home/user/dir <<< 'passwd'

Related

How to chain curl such that the output of one become another's input?

Could someone tell me how to pass the output of one CURL GET to another CURL POST? I mean something like this:
curl --header "Content-Type: application/json" --request POST --data "{\"specification\": curl --header "Content-Type: application/json" --request GET "https://user:pass#anotherhost"}" "https://user:pass#localhost"
Use jq to craft JSON in the shell.
response="$(curl --header "Content-Type: application/json" --request GET "https://user:pass#anotherhost")"
data="$(jq "{specification: .}" <<< "$response")"
curl --header "Content-Type: application/json" --request POST --data "$data" "https://user:pass#localhost"
Keep in mind, that passwords in command line arguments are public to the host.
It looks like you need to use command substitution. The result would be something like this.
curl --header "Content-Type: application/json" --request POST --data "{\"specification\": $(curl --header "Content-Type: application/json" --request GET "https://user:pass#anotherhost")}" "https://user:pass#localhost"
$() is used for command substitution and it invokes a subshell. The command in the parentheses (a.k.a. round brackets) of $() is executed in a subshell and the output is then placed in the original command.
So the curl GET will be executed in a subshell and the result will be passed to the curl POST
You can read more about it at the link below:
https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html

I am very new to API. Please send me what -H mean?

curl -X POST https://api.commerce.coinbase.com/charges/ \
-H "Content-Type: application/json" \
-H "X-CC-Api-Key: YOUR_API_KEY" \
-H "X-CC-Version: 2018-03-22" \
-d "#data.json"
how do i make this api work?
-H attaches a header to your request. See manpage of curl.
The -H is adding a Header to the request so everything between the speech marks after the -H is a header.
Your CURL request has 3 headers.
See https://developer.mozilla.org/en-US/docs/Glossary/Request_header

How to escape double quotes and exclamation mark in password?

I have the following code:
curl -s --insecure -H "Content-Type: application/json" -X POST -d "{\"username\":\"$1\",\"password\":\"$2\"}" http://apiurl
In the above curl command I want to escape the " and ! in the password. Right now the password is just given as $2
I have modified the curl command as below but it doesn't seem to work
curl -s --insecure -H "Content-Type: application/json" -X POST -d "{\"username\":\"$1\",\"password\":\"$2\"}" http://apiurl
Do not try to generate JSON manually; use a program like jq, which knows how to escape things properly, to generate it for you.
json=$(jq -n --arg u "$1" --arg p "$2" '{username: $u, password: $p}')
curl -s --insecure -H "Content-Type: application/json" -X POST -d "$json" http://apiurl

Variable not expanding inside curl command in linux

I have a curl command which I want to execute from inside a shell script.
My script is as below:
iddn="`jq -r '.APPID_DN' /scr/resp.json`"
appid = `echo "$iddn" | awk -F',' '{print $1}'`
id =`echo $appid | awk '{print substr($0,4)}'`
apppwd = `jq -r '.APPID_PWD' /scr/resp.json`
tnname = `jq '.tnName' /scr/resp.json`
curl -i --user "$id":"$apppwd" -H "Content-Type: application/xml" -H "Accept: application/xml" -H "X-USER-IDENTITY-DOMAIN-NAME: tn11" --request GET "http://hostname.XX.XXXX.com:port/XXX/services/rest/version/auth/administrator/Clients"
But I am getting below error
HTTP/1.1 401 Unauthorized
Not authorized. Provide Authorization Header.
I tried by echoing the command to see how the curl command is forming,I am getting it as(userid and passwd not passed at all),
curl -i --user -H "Content-Type: application/xml" -H "Accept: application/xml" -H "X-USER-IDENTITY-DOMAIN-NAME: tn11" --request GET "http://hostname.XX.XXXX.com:port/XXX/services/rest/version/auth/administrator/Clients"
But if i am running the same command directly from command line,its running fine.Please help on this.
Regards,
Shilpi
id =`echo $appid | awk '{print substr($0,4)}'`
apppwd = `jq -r '.APPID_PWD' /scr/resp.json`
You must not put spaces around = in a shell variable assignment.

Using a variable in a script calling curl

I'm trying to replace 1.2.3.4 with the contents of variable $wanip in the following script.
wanip="4.3.2.1"
echo $wanip
content=$(curl --insecure -H "X-DNSimple-Token: foo:bar" -H "Accept: application/json" -H "Content-Type: application/json" -X PUT -d "{\"record\": {\"name\": \"foo\",\"content\": \"1.2.3.4\"}}" https://acme.com/records/123)
echo $content
If I literally replace 1.2.3.4 with $wanip*, when I run the script I'm getting a message saying: "message":"Problems parsing JSON".
Try adding a layer of abstraction:
#!/bin/bash
wnip="4.3.2.1"
echo $wanip
command="curl --insecure -H 'X-DNSimple-Token: foo:bar' -H 'Accept: application/json' -H 'Content-Type: application/json' -X PUT -d '{\"record\": {\"name\": \"foo\",\"content\": \"${wnip}\"}}' https://acme.com/records/123"
echo $command
content=$($command)
echo $content
After some hacking, I got this to work. Strange.
wanip=\"4.3.2.1\"
echo $wanip
content=$(curl --insecure -H "X-DNSimple-Token: foo:bar" -H "Accept: application/json" -H "Content-Type: application/json" -X PUT -d "{\"record\": {\"name\": \"foo\",\"content\": $wanip }}" https://acme.com/records/123)
echo $content

Resources