Home > other >  Run command by ssh and 1 jump in the middle - ubuntu
Run command by ssh and 1 jump in the middle - ubuntu

Time:11-25

I am currently running the following command from a sh file, which checks if a file exists and reads the response:

sshpass -p "$PASS" ssh -t $USER@$IP 'echo '$PASS' | sudo -S [ -d /myFile ]' && echo "INFO: already exists /myFile, continue? [Y/n]" && read RESULT

Now I need to do the same thing but with another device in the middle. So I have:

myComputer -> Device_1 -> Device_2

Device_1: IP_1, USER_1, PASS_1; Device_2: IP_2, USER_2, PASS_2.

So I want to be able to do the same from myComputer to Device_2 directly. Logging must be automatic, without asking for a password.

Note:

I don't know if this is useful but I was able to connect directly to Device_2 running the following command:

env SSHPASS="$PASS_2" \
  sshpass -d 123 ssh \
    -o ProxyCommand="sshpass -e ssh -W %h:%p $USER_2@$IP_2" \
  $USER_1@$IP_1 \
  123<<<$PASS_1

Anyway this wasn't useful for me because I don't want to connect to the device, I just want to run a command.

CodePudding user response:

You can chain ssh commands in order to connect to your 2nd device.

Something like that should work :

sshpass -p "$PASS_DEVICE1" ssh -t "$USER_DEVICE1"@"$IP_DEVICE1" <<EOF
sshpass -p "$PASS_DEVICE2" ssh -t "$USER_DEVICE2"@"$IP_DEVICE2" <<EOD
echo '$PASS_DEVICE2' | sudo -S [ -d /myFile ] && echo "INFO: already exists /myFile, continue? [Y/n]" && read RESULT
EOD
EOF
  • Related