Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 07-13-2014, 02:59 AM   #1
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, 04:18 AM   #2
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975
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, 05:38 AM   #3
Senior Member
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,706

Rep: Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270
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, 07:36 AM   #4
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:

bash -s <<'EOD'
dmidecode; multipath -ll 

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.


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 02:14 AM
[SOLVED] sudo in bash script grep environment geep1980 Programming 15 11-28-2011 05:09 AM
BASH - question about using sudo in SSH script xmichielx Programming 7 09-03-2010 03:08 AM
bash - how to test, if a script is exececuted with sudo ? almatic Programming 6 10-19-2008 03:36 PM
bash shell script "sudo - lunlun Linux - Newbie 2 05-09-2008 09:42 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:44 AM.

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