For bulk administration of hosts via ssh, there are powerful tools such as Ansible for the job. However, if you need to automate ssh for a one-off task, you may find the remote host systems throwing up interactive authentication prompts for sudo that need to be answered.
If you need to run sudo commands that force an interactive authentication, you can echo the password to sudo, using the “-S” flag to force it to read standard input like below.
sudoPass="abc@123FakePass" ssh myuser@my.remote.host "echo \"$sudoPass\" | sudo -S /bin/bash; sudo /root/myProgram param1 param2; sudo /root/myOtherProgram"
This would invoke the “/root/myProgram” using sudo, then “/root/myOtherProgram”. You may notice that I started by immediately invoking sudo on /bin/bash and answering the interactive sudo authentication. This satisfies the sudo interactive password requirement for some default timeout, and means I do not have to worry about interactive prompts when I execute the subsequent requests.
Taking this to its logical conclusion, I would use the sshpass utility to avoid the password prompt for the initial ssh session.
sshpass -p "$sudoPass" ssh myuser@my.remote.host "echo \"$sudoPass\" | sudo -S /bin/bash; sudo /root/myProgram param1 param2; sudo /root/myOtherProgram"
REFERENCES
golinuxcloud.com, helpful ways to avoid ssh login
NOTES
If you have special characters in the password, you may need to use “-e” with a synthesized newline to have them interpreted correctly
echo -e "$sudoPass\n" | sudo -S /bin/bash
If you want to pipe a local script to an ssh session
ssh root@my.remote.host 'bash -s' < myscript.sh