How to use CURL over SSH and get the file as well as the return value?
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=$?"
bash curl ssh
add a comment |
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=$?"
bash curl ssh
1
The exit status ofssh
will be the exit status ofcurl
if that's the only command you run.sshpass
doesn't allow you to retrieve the exit status ofssh
, which is another reason to set up public-key authentication instead of usingsshpass
.
– chepner
Nov 13 '18 at 17:00
add a comment |
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=$?"
bash curl ssh
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=$?"
bash curl ssh
bash curl ssh
asked Nov 13 '18 at 16:39
user1587451user1587451
445718
445718
1
The exit status ofssh
will be the exit status ofcurl
if that's the only command you run.sshpass
doesn't allow you to retrieve the exit status ofssh
, which is another reason to set up public-key authentication instead of usingsshpass
.
– chepner
Nov 13 '18 at 17:00
add a comment |
1
The exit status ofssh
will be the exit status ofcurl
if that's the only command you run.sshpass
doesn't allow you to retrieve the exit status ofssh
, which is another reason to set up public-key authentication instead of usingsshpass
.
– chepner
Nov 13 '18 at 17:00
1
1
The exit status of
ssh
will be the exit status of curl
if that's the only command you run. sshpass
doesn't allow you to retrieve the exit status of ssh
, which is another reason to set up public-key authentication instead of using sshpass
.– chepner
Nov 13 '18 at 17:00
The exit status of
ssh
will be the exit status of curl
if that's the only command you run. sshpass
doesn't allow you to retrieve the exit status of ssh
, which is another reason to set up public-key authentication instead of using sshpass
.– chepner
Nov 13 '18 at 17:00
add a comment |
1 Answer
1
active
oldest
votes
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.
Almost there. When I do this,$CONTENT
shows a mixture in a single line of the multiline-output of the servers output. Why is that?
– user1587451
Nov 13 '18 at 17:07
Whats the actual output and what do you expect the output to be?
– max
Nov 13 '18 at 17:09
8 lines of json output
– user1587451
Nov 13 '18 at 17:19
1
Sounds like you forgot to put double quotes around"$CONTENT"
or maybe the value contains DOS carriage returns.
– tripleee
Nov 13 '18 at 17:21
You are a bash master...
– user1587451
Nov 13 '18 at 17:27
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285645%2fhow-to-use-curl-over-ssh-and-get-the-file-as-well-as-the-return-value%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Almost there. When I do this,$CONTENT
shows a mixture in a single line of the multiline-output of the servers output. Why is that?
– user1587451
Nov 13 '18 at 17:07
Whats the actual output and what do you expect the output to be?
– max
Nov 13 '18 at 17:09
8 lines of json output
– user1587451
Nov 13 '18 at 17:19
1
Sounds like you forgot to put double quotes around"$CONTENT"
or maybe the value contains DOS carriage returns.
– tripleee
Nov 13 '18 at 17:21
You are a bash master...
– user1587451
Nov 13 '18 at 17:27
add a comment |
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.
Almost there. When I do this,$CONTENT
shows a mixture in a single line of the multiline-output of the servers output. Why is that?
– user1587451
Nov 13 '18 at 17:07
Whats the actual output and what do you expect the output to be?
– max
Nov 13 '18 at 17:09
8 lines of json output
– user1587451
Nov 13 '18 at 17:19
1
Sounds like you forgot to put double quotes around"$CONTENT"
or maybe the value contains DOS carriage returns.
– tripleee
Nov 13 '18 at 17:21
You are a bash master...
– user1587451
Nov 13 '18 at 17:27
add a comment |
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.
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.
edited Nov 13 '18 at 17:10
answered Nov 13 '18 at 17:03
maxmax
360319
360319
Almost there. When I do this,$CONTENT
shows a mixture in a single line of the multiline-output of the servers output. Why is that?
– user1587451
Nov 13 '18 at 17:07
Whats the actual output and what do you expect the output to be?
– max
Nov 13 '18 at 17:09
8 lines of json output
– user1587451
Nov 13 '18 at 17:19
1
Sounds like you forgot to put double quotes around"$CONTENT"
or maybe the value contains DOS carriage returns.
– tripleee
Nov 13 '18 at 17:21
You are a bash master...
– user1587451
Nov 13 '18 at 17:27
add a comment |
Almost there. When I do this,$CONTENT
shows a mixture in a single line of the multiline-output of the servers output. Why is that?
– user1587451
Nov 13 '18 at 17:07
Whats the actual output and what do you expect the output to be?
– max
Nov 13 '18 at 17:09
8 lines of json output
– user1587451
Nov 13 '18 at 17:19
1
Sounds like you forgot to put double quotes around"$CONTENT"
or maybe the value contains DOS carriage returns.
– tripleee
Nov 13 '18 at 17:21
You are a bash master...
– user1587451
Nov 13 '18 at 17:27
Almost there. When I do this,
$CONTENT
shows a mixture in a single line of the multiline-output of the servers output. Why is that?– user1587451
Nov 13 '18 at 17:07
Almost there. When I do this,
$CONTENT
shows a mixture in a single line of the multiline-output of the servers output. Why is that?– user1587451
Nov 13 '18 at 17:07
Whats the actual output and what do you expect the output to be?
– max
Nov 13 '18 at 17:09
Whats the actual output and what do you expect the output to be?
– max
Nov 13 '18 at 17:09
8 lines of json output
– user1587451
Nov 13 '18 at 17:19
8 lines of json output
– user1587451
Nov 13 '18 at 17:19
1
1
Sounds like you forgot to put double quotes around
"$CONTENT"
or maybe the value contains DOS carriage returns.– tripleee
Nov 13 '18 at 17:21
Sounds like you forgot to put double quotes around
"$CONTENT"
or maybe the value contains DOS carriage returns.– tripleee
Nov 13 '18 at 17:21
You are a bash master...
– user1587451
Nov 13 '18 at 17:27
You are a bash master...
– user1587451
Nov 13 '18 at 17:27
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285645%2fhow-to-use-curl-over-ssh-and-get-the-file-as-well-as-the-return-value%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
The exit status of
ssh
will be the exit status ofcurl
if that's the only command you run.sshpass
doesn't allow you to retrieve the exit status ofssh
, which is another reason to set up public-key authentication instead of usingsshpass
.– chepner
Nov 13 '18 at 17:00