Latest LQ Deal: Latest LQ Deals
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 10-16-2017, 08:02 PM   #1
LQ Newbie
Registered: Jul 2017
Posts: 19

Rep: Reputation: Disabled
How can I script Mass Sudo commands?

I'm trying to do remote ssh commands to a list of servers. The only problem is that I'm grepping info from files that are root owned and require elevated permissions. This isn't usually an issue as I can just preface my command with "sudo" and it lets me, but when I script it I get an error message saying:

"sudo: no tty present and askpass program specified"

My command is like this:

while read host;do ssh -n $host " sudo grep blah /var/log/file";done < /tmp/serverlist | tee /tmp/server_results

Normally, I could just login as root, edit sudoers or adjust my default/primary group, but this is in a very restrictive environment and none of those things are possible. Also, it uses LDAP, so my credentials aren't local to any of these servers.

Old 10-16-2017, 10:20 PM   #2
LQ Guru
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Ubuntu MATE, Mageia, and whatever VMs I happen to be playing with
Posts: 19,233
Blog Entries: 28

Rep: Reputation: 6116Reputation: 6116Reputation: 6116Reputation: 6116Reputation: 6116Reputation: 6116Reputation: 6116Reputation: 6116Reputation: 6116Reputation: 6116Reputation: 6116
I think this issue might be best directed to your network administrators, so that they can give you the necessary rights if deemed appropriate.
Old 10-17-2017, 12:57 AM   #3
LQ Guru
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
You just sudo every command in the script. After you enter your sudo password one time, all the rest will run. Otherwise, you can sudo the script, and that should work too.
Old 10-17-2017, 01:09 AM   #4
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,217
Blog Entries: 3

Rep: Reputation: 3704Reputation: 3704Reputation: 3704Reputation: 3704Reputation: 3704Reputation: 3704Reputation: 3704Reputation: 3704Reputation: 3704Reputation: 3704Reputation: 3704
If it is the case that you could normally just log in and use sudo manually, then you must have a pseudo-terminal there in your script.

So swap the -n for -t:

while read host;do 
	ssh -t $host "sudo grep blah /var/log/file";
done < /tmp/serverlist | tee /tmp/server_results
That will allow you to type the password for sudo.

If you must be able to do this without a password, then I agree it is a job for your system administrator. However two modifications are needed. One is in your script. That is that because of how * misbehaves in /etc/sudoers it is safer to use string literals, that means you have a rare situation where cat is of use:

while read host;do 
	ssh -t $host "sudo cat /var/log/file | grep  blah";
done < /tmp/serverlist | tee /tmp/server_results
The other is in /etc/sudoers, they can set the following to allow you to read that one log file and only that one log file:

%robbotsgroup ALL=(root:root) NOPASSWD: /bin/cat /var/log/file
I highly recommend Michael W Lucas' book sudo Mastery to fill in the gaps with configuring sudo.


remote, script, ssh, sudo, sudoers

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
Running certain commands as sudo in a bash script? PACMANchasingme Linux - Newbie 4 05-25-2016 09:56 PM
[SOLVED] Running sudo commands from a script oneindelijk Linux - Newbie 4 12-11-2012 03:42 AM
[SOLVED] Allow commands without SUDO Touch Linux - Newbie 6 01-05-2011 09:33 AM
sudo and at commands ulto Programming 2 05-01-2004 09:36 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:23 PM.

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
Open Source Consulting | Domain Registration