How to use CURL over SSH and get the file as well as the return value? - bash

I want to load a file from a clients webserver. This webserver is running local only. To get there I have to use ssh. I need the content as well as the return value (e.g. SSH connection broke, webserver down).
What do I have to change? My first try:
#!/bin/bash
RETURN=0
CONTENT=""
sshpass -p xxxxxx ssh root#172.17.1.33 "curl -X POST http://127.0.0.1:10000/status -H 'Content-Type: application/json' > $CONTENT | bash; RETURN=$?"

If you want to get the exit code of curl and the return value of curl:
#!/bin/bash
CONTENT=$(sshpass -p xxxxxx ssh root#172.17.1.33 "curl -X POST http://127.0.0.1:10000/status -H 'Content-Type: application/json'")
RETURN=$?
echo "$RETURN, $CONTENT"
In your script you set the variables on the server you ssh'ed into.

Related

Using Bash to make a POST request

I have a 100 Jetpacks that I have to sign in to configure. I am trying to do it in a bash script but I am having no luck. I can connect to the wifi no problem but my POST request are not achieving anything. Any Advice? Here is link to my github. I have copies of what I captured on Burp suite https://github.com/Jdelgado89/post_Script
TYIA
#!/bin/bash
nmcli device wifi rescan
nmcli device wifi list
echo "What's they last four?"
read last4
echo "What's the Key?"
read key
nmcli device wifi connect Ellipsis\ \Jetpack\ $last4 password $key
echo "{"Command":"SignIn","Password":"$key"}" > sign_on.json
echo "{"CurrentPassword":"$key","NewPassword":"G1l4River4dm1n","SecurityQuestion":"NameOfStreet","SecurityAnswer":"Allison"}" > change_admin.json
echo "{"SSID":"GRTI Jetpack","WiFiPassword":"G1l4River3r","WiFiMode":0,"WiFiAuthentication":6,"WiFiEncription":4,"WiFiChannel":0,"MaxConnectedDevice":8,"PrivacySeparator":false,"WMM":true,"Command":"SetWifiSetting"}" > wifi.json
cat sign_on.json
cat change_admin.json
cat wifi.json
sleep 5
curl -X POST -H "Cookie: jetpack=6af5e293139d989bdcfd66257b4f5327" -H "Content-Type: application/json" -d #sign_on.json http://192.168.1.1/cgi-bin/sign_in.cgi
sleep 5
curl -X POST -H "Cookie: jetpack=6af5e293139d989bdcfd66257b4f5327" -H "Content-Type: application/json" -d #change_admin.json http://192.168.1.1/cgi-bin/settings_admin_password.cgi
sleep 5
curl -X POST -H "Cookie: jetpack=6af5e293139d989bdcfd66257b4f5327" -H "Content-Type: application/json" -d #wifi.json http://192.168.1.1/cgi-bin/settings_admin_password.cgi
This is not correct:
echo "{"Command":"SignIn","Password":"$key"}" > sign_on.json
The double quotes are not being put literally into the file, they're just terminating the shell string beginning with the previous double quote. So this is writing
{Command:SignIn,Password:keyvalue}
into the file, with no double quotes. You need to escape the nested double quotes.
echo "{\"Command\":\"SignIn\",\"Password\":\"$key\"}" > sign_on.json
However, it would be best if you used the jq utility instead of formatting JSON by hand. See Create JSON file using jq.
jq -nc --arg key "$key" '{"Command":"SignIn","Password":$key}' >sign_on.json

Running long commands in powershell

I need to run a curl command on a container running in kubernetes cluster via PowerShell. Now, with curl, i need to pass various headers containing long token Strings. However, when i execute the command, the command string gets split in midway and terminal enters into newline mode from which I am not able to exit.
Below is the command.
curl -X GET http://sit-product-abcd-xyzx-adapter:8080/prdcopy/projects/PROJECT-NOVEMBER2020-eCom-SPORT_STYLE-3 -H "authorization: bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IlhSQVFGbGs3V0tNWnFzRm1ZUDcxaE55U3c4TSIsImtpZCI6IlhSQVFGbGs3V0tNWnFzRm1ZUDcxaE55U3c4TSJ9.eyJhdWQiOiJodHRwczovL2Rldi5hcGkuYWRpZGFzLmNvbS9jb3JwbWFya2V0L3ByZGNvcHkiLCJpc3MiOiJodHRwOi8vc3RzLXRlc3QuYWRpZGFzLWdyb3VwLmNvbS9hZGZzL3NlcnZpY2VzL3RydXN0IiwiaWF0IjoxNTYzOTUyOTI5LCJuYmYiOjE1NjM5NTI5MjksImV4cCI6MTU2Mzk1NjUyOSwic3ViIjoic3ZjX3BjYXBpIiwiZGlzdGluZ3Vpc2hlZG5hbWUiOiJDTj1zdmNfcGNhcGksT1U9U2VydmljZUFjY291bnRzLE9VPVVzZXJzLE9VPUVNQWRtaW5pc3RyYXRpb24sREM9ZW1lYSxEQz1h456bnQsREM9Yml6IiwiYXBwdHlwZSI6IkNvbmZpZGVudGlhbCIsImFwcGlkIjoiNTA4NzM2YTAtNzM5Mi00ODBkLTlmYWQtYWQ5NzUwMDI3ZjVhIiwiYXV0aG1ldGhvZCI6InVybjpvYXNpczpuYW1lczp0YzpTQU1MOjIuMDphYzpjbGFzc2VzOlBhc3N3b3JkUHJvdGVjdGVkVHJhbnNwb3J0IiwiYXV0aF90aW1lIjoiMjAxOS0wNy0yNFQwNzoyMjowOS43NTFaIiwidmVyIjoiMS4wIiwic2NwIjoidXNlcl9pbXBlcnNvbmF0aW9uIG9wZW5pZCJ9.kBrQO48rK_lkhDeZIQkW_If9lcYn5kERiif1VtlcXAPQ_j94KDGTfrmvF9f89EeyzExZSHFnCx0HyI8tvpeklEypWGchI4qG8V8z-AjTBrmKnX6mMSVLYaoh0TsLdeaFX-ByQ4TmyXzt5jje-CcS62w8P1J3aalVNhlTUp9e5rtP9qpttu2QmA482anQSsS6GN6ltJY9NyjfrW-ElZRrPXunIAf16jOjG0ehEUS8GpUJnc-wStJnF_zYuvnb-4m2GqzXqDHtpEvojg5N0Luq07z2mMpeEXbrGx-0fScjkSAsCJ8E-sFpbBYSXOCHfCslUPjM07kyzaZaa9tgWJGrGA" -H "cache-control: no-cache" -H "content-type: application/json" -H "project-id-type: API"
However, when i enter this command in power-shell after entering into the container, it looks like below.
PS R:\> kubectl exec -it sit-pcapi-order-event-consumer-b6f4c8797-m578d -- sh
/opt/app # curl -X GET http://sit-product-abcd-xyzx-adapter:8080/prdcopy/projects/PROJECT-NOVEMBER2020-eCom-SPORT_STYLE-
3 -H "authorization: bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IlhSQVFGbGs3V0tNWnFzRm1ZUDcxaE55U3c4TSIsImtpZCI6
IlhSQVFGbGs3V0tNWnFzRm1ZUDcxaE55U3c4TSJ9.eyJhdWQiOiJodHRwczovL2Rldi5hcGkuYWRpZGFzLmNvbS9jb3JwbWFya2V0L3ByZGNvcHkiLCJpc3M
iOiJodHRwOi8vc3RzLXRlc3QuYWRpZGFzLWdyb3VwLmNvbS9hZGZzL3NlcnZpY2VzL3RydXN0IiwiaWF0IjoxNTYzOTUyOTI5LCJuYmYiOjE1NjM5NTI5Mjk
sImV4cCI6MTU2Mzk1NjUyOSwic3ViIjoic3ZjX3BjYXBpIiwiZGlzdGluZ3Vpc2hlZG5hbWUiOiJDTj1zdmNfcGNhcGksT1U9U2VydmljZUFjY291bnRzLE9
VPVVzZXJzLE9VPUVNQWRtaW5pc3RyYXRpb24sREM9ZW1lYSxEQz1h456bnQsREM9Yml6IiwiYXBwdHlwZSI6IkNvbmZpZGVudGlhbCIsImFwcGlkIjoiNTA4
NzM2YTAtNzM5Mi00ODBkLTlmYWQtYWQ5NzUwMDI3ZjVhIiwiYXV0aG1ldGhvZCI6InVybjpvYXNpczpuYW1lczp0YzpTQU1MOjIuMDphYzpjbGFzc2VzOlBh
c3N3b3JkUHJvdGVjdGVkVHJhbnNwb3J0IiwiYXV0aF90aW1lIjoiMjAxOS0wNy0yNFQwNzoyMjowOS43NTFaIiwidmVyIjoiMS4wIiwic2NwIjoidXNlcl9p
bXBlcnNvbmF0aW9uIG9wZW5pZCJ9.kBrQO48rK_lkhDeZIQkW_If9lcYn5kERiif1VtlcXAPQ
>
>
>
After this, i need to close the terminal and re-login into the container and kill the sh process manually.
How can i exit this curl command in power-shell? How can i execute long curl commands? I tried powershell_ise, but in that, i am not able to execute kubectl exec command. After executing that command, the terminal remains stuck forever.
Maybe you can copy the token as file into the container/pod and then pass it to curl as file.
More or less like this:
# Copy file
kubectl cp /tmp/token namespace/sit-pcapi-order-event-consumer-b6f4c8797-m578d:/tmp/token
# Pass the token as file instead of literal
kubectl exec -it sit-pcapi-order-event-consumer-b6f4c8797-m578d -- sh /opt/app # curl -X GET http://.../ -H #header_file $URL
Regards

curl 400 bad request (in bash script)

I trying to do execute the following script in bash
#!/bin/bash
source chaves.sh
HEAD='"X-Cachet-Token:'$CACHET_KEY'"'
SEARCH="'{"'"status"'":1,"'"id"'":"'"7"'","'"enabled"'":true}'"
echo $SEARCH
if curl -s --head --request GET http://google.com.br | grep "200 OK" > /dev/null; then
echo 'rodou'
curl -X PUT -H '"Content-Type:application/json;"' -H '"'X-Cachet-Token:$CACHET_KEY'"' -d $SEARCH $CACHET_URL/7
else
echo 'não deu'
curl -X PUT -H '"Content-Type: application/json;"' -H $x -d '{"status":1,"id":"7","enabled":true}' $CACHET_URL/7
fi
But keep receiving a 400 bad request from the server.
When i try to run the same line (echo in the script, Ctrl+c and Ctrl+v) directly in terminal, the command run without problems.
The source file have the directions to path and a variable token i need to use, but as far as i have tested is reading ok.
edit 1 - hidding some sensitive content
edit 2 - posting the exit line (grabed trought Ctrl+c, Ctrl+v)
The command i neet to input in server is:
curl -X PUT -H "Content-Type:application/json;" -H
"X-Cachet-Token:4A7ixgkU4hcCWFReQ15G" -d
'{"status":1,"id":"7","enabled":true}'
http://XXX.XXX.XXX.XXX/api/v1/components/7
And the exit i grabed trought echo comand, give me the exact exit i want, but don't run inside script, only outside.
I'm a bit new to the curl, any help can be apreciate.
Sorry for the bad english and tks in advance.

curl return HTTP code 503 when called from a script

I'm try to call some REST API and get the resulting HTTP code using curl. If in a terminal I type :
curl -s -o /dev/null -I -w '%{http_code}' -X POST 'http://localhost/gitlab/api/v3/projects?private_token=my_private_token&name=blabla' -H 'Content-Length: 0'
It works and return me the HTTP code 201 ("created"). Now I try to use this command in a bash script replacing a part of the url with variable:
echo "Enter base URL :"
read gitlab_url # Here I type 'http://localhost/gitlab', to generate the same URL as in first code snippet
code_status=$(curl -s -o /dev/null -I -w '%{http_code}' -X POST '$gitlab_url/api/v3/projects?private_token=my_private_token&name=blabla' -H 'Content-Length: 0')
echo "$code_status"
And then it returns me the HTTP code 503 ("Service Unavailable"). To see if there is any differences between the "hard coded" URL and the generated one, I do :
echo "curl -s -o /dev/null -I -w '%{http_code}' -X POST '$gitlab_url/api/v3/projects?private_token=my_private_token&name=blabla' -H 'Content-Length: 0'"
# Output :
curl -s -o /dev/null -I -w '%{http_code}' -X POST 'http://localhost/gitlab/api/v3/projects?private_token=my_private_token&name=blabla' 'Content-Length: 0'
And if I execute this in a terminal directly, it works and return me 201. So: why do this command fails if I use it in a script ? Is there anything I missed ?
It was a proxy problem. If I use curl -v .... I can see the following output:
When curl is typed directly in terminal I have :
* About to connect() to localhost port 80 (#0)
* Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 80 (#0)
And when I used it into a bash script I get :
* About to connect() to proxy proxy.my.company port xxx (#0)
* Trying xx.xx.xx.xx... connected
* Connected to proxy.my.company (xx.xx.xx.xx) port xxx (#0)
So to fix it I added this in the top of my script :
export no_proxy=localhost,127.0.0.1
export http_proxy=""
I am very surprised to have to do this, because I already have an environment var no_proxy who already reference localhost and 127.0.0.1
Try to run it as bash -x script.sh.

Run curl from .sh script with defined Content-Type

When I'm trying to run test.sh script I've always receive error from curl:
curl: (6) Couldn't resolve host 'application'
test.sh:
#!/bin/sh
CT="Content-Type:\ application/json"
TEST="curl http://127.0.0.1 -H $CT"
echo $TEST
RESPONSE=`$TEST`
echo $RESPONSE
But if I just run following command from console everything fine:
curl http://127.0.0.1 -H Content-Type:\ application/json
Could you please let me know what is wrong in script, as I understand something is wrong with 'space' escape, but have no idea how to fix it.
Also I've tried following combination, but result the same:
CT="Content-Type: application/json"
TEST="curl http://127.0.0.1 -H \"$CT\""
UPD:
bash / dash is only available on server. (/bin/sh --> bash)
GNU bash, version 4.2.10(1)-release (x86_64-pc-linux-gnu)
Run the following: (delete the space after Content-Type)
#!/bin/bash
CT="Content-Type:application/json"
TEST="curl http://127.0.0.1 -H $CT"
echo $TEST
RESPONSE=`$TEST`
echo $RESPONSE
You can try with: bash -c your_bash_file.sh It worked for me with the same problem

Resources