Remoting sudo commands & bypassing bashrc

What I want to do is not unique, except that our environment has a twist.

I want to ssh to a remote server and issue a sudo command to run a script. This isn't working, but you'll get the gist.

# ssh remotehost sudo -i -u oracle script.bash

The sudo to oracle is fine. The script.bash sets up the environment variables needed to run the Oracle commands and runs a very rudimentary "asmcmd lsdg" to get information.

The problem is that the Oracle user's .bashrc or .bashprofile which gets executed by the "sudo -i -u" has a shell menu that prompts the logged on user (99% of the time it's oracle) to select some variables before dumping the session to a shell prompt. Since I'm scripting the commands to source my environment then run the Oracle commands, and running it via cron, I have to be able to ignore the menu from the profile.

As root (or even myself), I can't source the env variables from Oracle and run the script (no DB perms), so that option is out.

Is there any way I can use sudo or su on the remote system and bypass or break out of the bashrc to ignore the menu prompting so I can run my script seamlessly?

Are you in a position to modify the remote .bashrc or .profile ? There you could check for the -i (interactive) option flag and, if missing, skip the menu.

Why are you using the -i (simulate initial login) flag if you don't want the bash profiles sources? See man sudo (linux) for details. But if it is necessary, then perhaps:

ssh remotehost sudo -i -u oracle bash --noprofile script.bash

Or since you are root:

ssh remotehost su oracle -c script.bash

No, I can't edit another user's profile. Actually I can since I'm root, but since the DBAs use it whenever they log on they'll be, well, rather "unsettled" if I change their logins for my use.

Thanks Derek. Unfortunately neither of your options worked. The "su" prompted me for a password, the sudo command still gave me the interactive menu (because of the -i). But I appreciate your feedback none-the-less.

I did figure it out, just this morning after bashing my head last night:

ssh -t host sudo -u oracle script.bash  

I was so close. What I had to do was to put all the environment variables (to get the stuff that the -i provided), such as sourcing the Oracle environments inside MY bash script instead of using their bash.profile. It works.

I'd like to get out of having the script run on the Oracle server, but instead pass all this TO the Oracle server from my source server (the one I'm ssh'ing from), but I know how to to that.

Thanks for the help guys, I do appreciate it!:slight_smile:

As RudiC pointed out you can check for an interactive shell before running the menu:

eg:

if [[ $- == *i* ]]
then
    menu code here
fi

This should upset the DBAs too much as they will still get the menu on an interactive login.

Otherwise you could try piping /dev/null to the sudo command:

ssh remotehost "sudo -i -u oracle script.bash </dev/null"

in the hope the menu will close if it encounters EOF