1

Let's say there is a case statement within the SSH session. I want to pass the variable entry out of remote machine and store it in local machine's csv file.
The example:

#!/bin/sh
set -x
    
read -p 'Enter the raspberry ip address you want to connect:' Rasp_id
entry=$(sshpass -pthe@Donut ssh -oStrictHostKeyChecking=no pi@"$Rasp_id" "$(cat << 'E3


    cd
    read -p 'Enter the case you want to configure\n 1.1 WM \n 2.1 LWM\n' option

    case $option in

    (1)
    read -p 'config_wide_motion token' token_w_m
    cat SoundEye/NoCAN/config_nocan.json
    cat SoundEye/NoCAN/config_processor_motion.json
    entry="1_WM"
    ;;

    (2)
    read -p 'config_laser_wide_motion token:' token_l_w_m
    cat SoundEye/NoCAN/config_nocan.json
    cat SoundEye/NoCAN/config_processor_motion.json
    entry="1_LWM"
    ;;
    esac

E3
)"
)
printf "$entry"

However, it doesn't do what I want. I would like to see the entry be either 1_WM or 1_LWM when it finished the last line

  • It strikes me as more useful to do the user input locally and then simply `ssh user@host cat "$somename" "$someothername"`. Also, you seem to want to cat the same files regardless of user input on the second question, and the variable read is not used. – Kusalananda Jan 06 '21 at 09:47
  • What exactly do you mean by "it couldn't execute as what I want"? For one, you have the same problem as in [your other question](https://unix.stackexchange.com/q/627414/377345) regarding the mix between shell variables in your local script and your remote ssh session. – AdminBee Jan 06 '21 at 09:49
  • i would like to pass the variable **entry** out of SSH session , pass the variable value either entry="1_WM" or entry="1_LWM" out of remote machine to local machine, store it to local machine CSV file. – TensorFlowGuan Jan 06 '21 at 10:07
  • Always use a format string for printf: see https://github.com/koalaman/shellcheck/wiki/SC2059 – glenn jackman Jan 06 '21 at 15:23

2 Answers2

2

Your:

entry=$(
  sshpass...
)

Captures the output of sshpass and stores it in the $entry variable.

You're outputting the contents of SoundEye/NoCAN/config_nocan.json and SoundEye/NoCAN/config_processor_motion.json (using two invocations of the concatenating command!?), so that's what will end up in $entry.

You're not outputting the contents of the $entry variable of the remote shell (which appears to be bash as you're using that bash-specific read -p), so it won't make it to the $entry variable of the local shell.

Since you're already using structured (json) content, you might as well include that information in there and, in the remote shell, output something like:

printf '{ "type": "%s", "config_nocan": %s, "config_processor_motion": %s }\n' \
  "$entry" \
  "$(<SoundEye/NoCAN/config_nocan.json)" \
  "$(<SoundEye/NoCAN/config_processor_motion.json)"

So $entry of your local shell will have some json structured data. So you can do for instance:

type=$(printf '%s\n' "$entry" | jq -r .type)
config_nocan=$(printf '%s\n' "$entry" | jq .config_nocan)
...

In your specific case though, I'd say it would make more sense to leave all the user interactions local, and only use ssh only to transfer the information you want.

Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
-3

I would think this would break modern security standards. The fun way, I would create native listener on remote machine and post it a message to act on, storing your data. Or you simply use a shared disk.

ken205726
  • 3
  • 1
  • 1
    In what way do you think it would break what modern security standards? How would you go about creating a native listener, and how would it be better? – ilkkachu Jan 06 '21 at 12:29