Ssh – change user after ssh and execute multiple commands

bashssh

I have a simple shell script to login as a normal user, and this normal user has a shell ready to switch into a user that has sudo privileges. However, I can't get it to work:

sshpass -p PASS ssh -t -o StrictHostKeyChecking=no user@host "sudo -s -u SUDOUSER;sudo cp -r "${STAGING}/${THEME}/*" /opt/themes/"${THEME}/.";exit;rm -rf "${STAGING}/*""

This logs me into the server fine and then switch into SUDOUSER but then it actually just stops there and prompts me as SUDOUSER on the host (basically logged me into SUDOUSER on the server and awaits command)

I need the script to run and return back to my originating terminal with cp and rm tasks completed.

Can someone please help?

Thanks

Best Answer

This seems largely a quoting problem. Try this:

sshpass -p PASS ssh -t -o StrictHostKeyChecking=no user@host \
    "sudo -s -u SUDOUSER '\
        cp -r \"${STAGING}/${THEME}/*\" /opt/themes/\"${THEME}/.\"; \
        rm -rf \"${STAGING}/*\"'"

I assume $STAGING and $THEME are variables in your local shell.