sudo
supports the -c
option, which passes a command to the sub-shell. Here are some of the sudo
flags that might be useful to you:
-c, --command=COMMAND
pass a single COMMAND to the shell with -c
--session-command=COMMAND
pass a single COMMAND to the shell with -c and do not create a new session
-m, --preserve-environment
do not reset environment variables
-s, --shell=SHELL
run SHELL if /etc/shells allows it
So, using something like sudo su someuser -c 'ls;date'
, you'll execute the commands ls
and date
as someuser
. Give it a try at the command-line on that host to get a feel for what you can do, then apply it to your SSH session.
See man sudo
for more information.
Also, just as a coding tip, you can reduce:
if data =~ /\[sudo\]/ || data =~ /Password/i
to:
if (data[/\[sudo\]|Password/i])