4

I need to ssh into a machine and execute a bunch of commands under sudo bash. Here is what I've tried:

sshpass -p "vagrant" ssh vagrant@33.33.33.100 "sudo bash -i -c <<EOF
    echo
    ls
    echo
EOF"

But it throws me 'bash: -c: option requires an argument\n'. How can I fix this?

linkyndy
  • 17,038
  • 20
  • 114
  • 194
  • 2
    The `-c` option requires an argument in the command line, not a heredoc. A heredoc is an input redirection. – Jdamian Nov 19 '14 at 08:38
  • Just remove `-c` and it will work fine. – anubhava Nov 19 '14 at 08:40
  • In addition, `-i` means 'interactive', does it not? How is it possible if you are redirecting its standard input? – Jdamian Nov 19 '14 at 08:42
  • 1
    @anubhava, thank you, it solved my problem! Can you write an answer so that I can accept it? Also, I would like more details about why it wasn't working in the first place, if you have the time. – linkyndy Nov 19 '14 at 08:45

1 Answers1

5

You need to remove -c from your command line to make it accept heredoc:

sshpass -p "vagrant" ssh vagrant@33.33.33.100 "sudo bash <<EOF
    echo
    ls
    echo
EOF"

Also you may remove -i (interactive) option too.

bash -c expects you to provide all commands on command line so this may work too:

sshpass -p "vagrant" ssh vagrant@33.33.33.100 "sudo bash -c 'echo; ls; echo'"
anubhava
  • 761,203
  • 64
  • 569
  • 643