2

Is there a way to use the multiple commands in sshpass, I tried with difference combinations but it throws error.

$ sudo sshpass -p $password ssh -t -oStrictHostKeyChecking=no $username@$ipaddress << EOF
  command_one
  command_two
  command_three
EOF

What is the correct and exact command, please provide me with an example if possible.

closed as off-topic by Jesse_b, Rui F Ribeiro, G-Man, Archemar, Kiwy Aug 2 '18 at 7:50

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Jesse_b, Rui F Ribeiro, Archemar
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Your example should work. sshpass is not really relevant. – Jesse_b Aug 1 '18 at 19:45
  • 1
    What error do you get? – Jesse_b Aug 1 '18 at 20:03
5

Your solution works with or without sshpass, if it's not working for you there is likely something else wrong.

Also note you should always quote every variable.

Additionally you shouldn't disable stricthostkeychecking lightly. If your host key is changing frequently your server is probably compromised

Here are some ways to accomplish your goal though:


Using a command separator (;, &&, ||)

sshpass -p "$password" ssh -t -oStrictHostKeyChecking=no "$username@ipaddress" 'command_one; command_two; command_three'

Using a here-doc (Single quote EOF to have any variables/command substitutions expand on the remote host):

sshpass -p "$password" ssh -t -oStrictHostKeyChecking=no "$username@ipaddress" <<EOF
  command_one
  command_two
  command_three
EOF

Using a separate file:

file.sh

command_one
command_two
command_three

Then execute like this:

cat file.sh | sshpass -p "$password" ssh -t -oStrictHostKeyChecking=no "$username@ipaddress"

Using an ssh tunnel:

sshpass -p "$password" ssh -f -L 3306:localhost:3306 "$username@ipaddress" sleep 300 # This will open a tunnel to the remote host and hold it open for 5 minutes
ssh -T "$username@ipaddress" 'command_one'
ssh -T "$username@ipaddress" 'command_two'
ssh -T "$username@ipaddress" 'command_three'

3

Try it like this:

$ sudo sshpass -p $password ssh -t -oStrictHostKeyChecking=no $username@$ipaddress << EOF
command_one;
command_two;
command_three;
EOF

I did the above and it works fine.

Not the answer you're looking for? Browse other questions tagged or ask your own question.