LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-13-2014, 03:59 AM   #1
npk85
LQ Newbie
 
Registered: Jan 2011
Posts: 6

Rep: Reputation: 0
sudo in bash script


Hi All,

Requirement: we have many servers and we have to collect information of outputs such as dmidecode or multipath -ll in the remote servers. To run those commands in the server, we actually switch to the root using `sudo su - root` and then run the commands dmidecode or multipath -ll etc... Now i want to collect the information of these commands from a single server using bash script, however i am not able to achieve this as including sudo su - root in the script doesn't work.
OS: redhat linux

for e.,g: to view dmidecode

for i in `cat serverlist.txt`; do ssh $i " sudo su - root; dmidecode; multipath -ll"; done > commandoutput.txt -----> This doesn't work.
 
Old 07-13-2014, 05:18 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
write a separate script that does the sudo work and execute that as a script in a single sudo command. Opening a shell as root naturally means that the script itself is no longer in charge.
 
Old 07-13-2014, 06:38 AM   #3
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Look at what you wrote for the command.

"sudo su - root; dmidecode; multipath -ll".

The problem is that sudo is already root... Possible issues are that he ssh doesn't have a terminal defined (sudo by default requires a terminal, but it can be configured to work without).

But it would be better (and simpler) to put the "dmidecode; multipath -ll" in a script and direct sudo to directly execute that.

That way you can extend the script when you find other things to add to it without having change the information gathering.
 
Old 07-13-2014, 08:36 AM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
You can use `-s` option. Run bash with sudo/su however you do it and pass commands through input:

Code:
bash -s <<'EOD'
dmidecode; multipath -ll 
EOD
Or

Code:
bash -s <<< "dmidecode; multipath -ll"
Perhaps it's also no longer necessary. Same concept may apply without the option as long as there are no extra file arguments to bash.
 
  


Reply

Tags
bash, script, sudo


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sudo inside bash shell script radu.pop Linux - Software 3 06-07-2013 03:14 AM
[SOLVED] sudo in bash script grep environment geep1980 Programming 15 11-28-2011 06:09 AM
BASH - question about using sudo in SSH script xmichielx Programming 7 09-03-2010 04:08 AM
bash - how to test, if a script is exececuted with sudo ? almatic Programming 6 10-19-2008 04:36 PM
bash shell script "sudo - lunlun Linux - Newbie 2 05-09-2008 10:42 PM


All times are GMT -5. The time now is 07:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration