LinuxQuestions.org
Review your favorite Linux distribution.
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 10-16-2017, 07:02 PM   #1
Robbot
LQ Newbie
 
Registered: Jul 2017
Posts: 19

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


Hi,
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.


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

Rep: Reputation: 5451Reputation: 5451Reputation: 5451Reputation: 5451Reputation: 5451Reputation: 5451Reputation: 5451Reputation: 5451Reputation: 5451Reputation: 5451Reputation: 5451
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-16-2017, 11:57 PM   #3
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,513

Rep: Reputation: 1010Reputation: 1010Reputation: 1010Reputation: 1010Reputation: 1010Reputation: 1010Reputation: 1010Reputation: 1010
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, 12:09 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,690
Blog Entries: 3

Rep: Reputation: 2920Reputation: 2920Reputation: 2920Reputation: 2920Reputation: 2920Reputation: 2920Reputation: 2920Reputation: 2920Reputation: 2920Reputation: 2920Reputation: 2920
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:

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

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

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


Reply

Tags
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 08:56 PM
[SOLVED] Running sudo commands from a script oneindelijk Linux - Newbie 4 12-11-2012 02:42 AM
[SOLVED] Allow commands without SUDO Touch Linux - Newbie 6 01-05-2011 08:33 AM
sudo and at commands ulto Programming 2 05-01-2004 08:36 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:54 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
Open Source Consulting | Domain Registration