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










0















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=$?"









share|improve this question

















  • 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















0















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=$?"









share|improve this question

















  • 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













0












0








0








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=$?"









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 16:39









user1587451user1587451

445718




445718







  • 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












  • 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







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












1 Answer
1






active

oldest

votes


















1














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.






share|improve this answer

























  • 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










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
);



);













draft saved

draft discarded


















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









1














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.






share|improve this answer

























  • 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















1














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.






share|improve this answer

























  • 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













1












1








1







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.






share|improve this answer















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.







share|improve this answer














share|improve this answer



share|improve this answer








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

















  • 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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto