Remoting sudo commands & bypassing bashrc


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remoting sudo commands & bypassing bashrc
# 1  
Old 01-26-2015
RedHat 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.
Code:
# 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?

Last edited by rbatte1; 01-27-2015 at 07:03 AM.. Reason: Added CODE tags
# 2  
Old 01-27-2015
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.
# 3  
Old 01-27-2015
Why are you using the -i (simulate initial login) flag if you don't want the bash profiles sources? See sudo for details. But if it is necessary, then perhaps:
Code:
ssh remotehost sudo -i -u oracle bash --noprofile script.bash

Or since you are root:
Code:
ssh remotehost su oracle -c script.bash

# 4  
Old 01-27-2015
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:

Code:
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!Smilie

Last edited by rbatte1; 01-27-2015 at 12:07 PM.. Reason: Added CODE tags
# 5  
Old 01-27-2015
As RudiC pointed out you can check for an interactive shell before running the menu:

eg:

Code:
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:

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

in the hope the menu will close if it encounters EOF
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

Tracking what commands were executed after sudo to another user

All team members has sudo access to user "batch55". Need to track all the commands used by team members after sudo to "batch55". Using HP-UX and ksh shell in our environment. How can i acheive this? Thanks In Advance. (2 Replies)
Discussion started by: venkatababu
2 Replies

2. Ubuntu

Sudo commands without puting in .bashrc

dear all, When I start my laptop, I need to run one command /etc/init.open-afs start and it require sudo privilege. The only solution which occur to me is to put this command in .bashrc. But then the trouble comes as everytime I open any new tab it ask for the sudo password, which is pretty... (5 Replies)
Discussion started by: emily
5 Replies

3. Shell Programming and Scripting

How to run sudo commands under a script?

Hi, I am new to scripting. I am trying to write a script to ssh one remote machine and run a sudo command. ssh <hostname> sudo -S <command> < ~/pass.txt I am stored my password in pass.txt. I am getting error sudo: no tty present and no askpass program specified Please suggest me how can... (1 Reply)
Discussion started by: venkia9
1 Replies

4. AIX

track commands run as root after sudo

I'm looking for a way to track commands that are run as root after a user runs sudo su - root. I have a profile set up for root that will track the commands by userid but if we change the shell it only stores it in that shells history file. (2 Replies)
Discussion started by: toor13
2 Replies

5. UNIX for Advanced & Expert Users

sudo: blocking specific commands

Hello all, I manage some HP-UX 11.31 servers. I have some users that have sudo access. All of them belong to the 'sudoers' user group. Right now, sudo is configured as wide open: %sudoers ALL=(ALL) ALL We are using sudo mostly for auditing purposes - when a user wants to run a... (9 Replies)
Discussion started by: lupin..the..3rd
9 Replies

6. Programming

Using Commands over SSH using Sudo

Is there a way to transfer my sudo password via ssh so that I can copy files remotely and pass them locally, so: cat sudo-passwd-file|ssh -t user@10.7.0.180 'sudo find / -depth|cpio -oacv|gzip' > /path/to/dir/file.cpio.gz I am in the process of a creating a script. Everytime I try and just... (16 Replies)
Discussion started by: metallica1973
16 Replies

7. UNIX for Dummies Questions & Answers

sudo commands list

Hi, Can you please give me a list of commands executed through 'sudo' command, thank you. (1 Reply)
Discussion started by: Dev_Dev
1 Replies

8. AIX

Add sudo executable commands

Guy's I have sudo already installed in AIX , just I want to know how can I add for example the following commands to be executed by sudo by (appuser).. shutdown /usr/startapp.sh /usr/stopapp.sh (5 Replies)
Discussion started by: ITHelper
5 Replies

9. UNIX for Dummies Questions & Answers

.bashrc question re: rm -i & ls --colors

QUESTION #1: I have this in my .bashrc file: alias rm='rm -i' Problem is, there are 3 files that I remove many times a day and would like this command to ignore these 3 files. In other words, prompt me on everything EXCEPT these 3 files. Is this possible? QUESTION #2: Also in... (16 Replies)
Discussion started by: kthatch
16 Replies

10. UNIX for Advanced & Expert Users

Logging all commands after a sudo su-

Hi there, It might seem tricky, I confess. We use sudo to allow people to initiate priviledged commands (but not all commands) on our Unix systems. To by pass this, some people initiate the sudo su - command ; The main issue is to 'know' what those people do when they gain root access.... (4 Replies)
Discussion started by: linuxmtl
4 Replies
Login or Register to Ask a Question