Sorry in advance for any formatting.
Check out the Expect scripting language. Expect - Wikipedia
You can design it to log into another system and perform the sudo (or su) command.
To avoid putting passwords into command prompts (security violation), have it prompt the user for the remote system password(s).
----
You can 'expect' prompts like the rsa key and have it answer and continue.
----
Example of an embedded expect function inside a shell (/bin/sh) script. I handles several scenarios including rsa prompt.
function nonrootuser {
expect << END
set timeout 30
spawn ssh $2@$1
expect {
"No route" {
exit 5;
} "yes/no" {
send "yes\r"
exp_continue;
} "password:" {
send "$3\r"
}
}
expect "home/$2"
send "$4\r"
expect "home/$2"
send "exit\r"
expect EOF
END
}
Call it with:
nonrootuser $remotehost "$USERID" "$USERPASSWORD" "$COMMAND"
-----
This function sends a single linux command to the remote host. It does not check the output, but it will be on the screen. You can also redirect it to a file if you choose.
Since it is contained within a script, you can prompt the user for the password so it is never hardcoded or put in a command line (where it can been seen using 'history')
You can also add a looping mechanism for multiple devices.
-----
If sudo is already set-up on your end device, you can just call it.
If it isn't, here is the added steps for becoming root (before the send "$4\r" statement), you will also need another send "exit\r" that is not shown here.
send "su -\r"
expect "word:"
send "$5\r"
expect {
":/root" {
expect ":/root"
} "denied" {
exit 6
} "timeout {
exit 7
}
}
Of course since it has an additional parameter (the root password), it would need to be called using such.
nonrootuser $remotehost "$USERID" "$USERPASSWORD" "$COMMAND" "$ROOTPASSWORD"