0

Update

I noticed that I am able to pass JSON with this command

command -j /dev/stdin <<< '{"key":"value"}'

However, it does not work if I call it through SSH.

ssh {target} 'command -j /dev/stdin <<< '{"key":"value"}''

Looks like it send through as a string and not JSON? Anyone have any idea why?


I have a command where I need to pass a JSON string to the options but for some reasons I need it to be passed using herestring to /dev/stdin.

Example

command -j /dev/stdin <<< '{"key":"value"}'
1
  • Your problem are the unescaped single quotes. You have ' before command that is closed after <<<```. I think you need to use \'`` after <<< and after "value"}. Otherwise you close the ssh single quotes at the first inner single quote and open it again at the second one. Commented Jul 26, 2019 at 11:45

2 Answers 2

0

There is no need to make the here-string redirection part of the remote command. Data redirected into ssh would end up on the standard input of the remote command:

ssh remote 'some command' <<<'whatever string'

In your case,

ssh target 'command -j /dev/stdin' <<<'{"key":"value"}'

But this assumes that both key and value are already JSON encoded.

Constructing the JSON with jq (to get proper encoding of the value, assuming it is held in a shell variable $value):

jq -nc --arg val "$value" '{ key: $val }' | ssh target 'command -j /dev/stdin'

Or, use jo to write the JSON document:

jo key="$value" | ssh target 'command -j /dev/stdin'
-1

I figured it out, when passing " to SSH I would need to use \\\" to let it go through. So the command below works.

ssh {target} 'command -j /dev/stdin <<< '{\\\"key\\\":\\\"value\\\"}''
1
  • Note that this would possibly not JSON encode the strings in the JSON document. Commented Jun 28, 2021 at 9:53

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.