using sudo in a perl script

Madison Kelly linux-5ZoueyuiTZhBDgjK7y7TUQ at public.gmane.org
Wed Dec 8 16:06:23 UTC 2004


Hi all,

   I have a very frustrating problem I have been fighting since last 
night... help pwetty pwease?

   I need to get a perl script to call a shell program through sudo and 
read in the resulting output. I have it working well enough now that the 
script will accept the password from the program but the output goes to 
the web page (it's a web-interface) seemingly no matter what I do. 
Please note that for testing to be sure that the sudo timestamp is not 
still valid I issue: 'system "sudo -K\n";' just before the code below. 
Here is what I know so far:

If I issue:
open (BLKID, '|sudo /sbin/blkid -c /dev/null 2>&1 |');
print BLKID "$passwd\n";
for (<BLKID>)
{
	s/\n//;
	$test=$_;
	print "$test\n";
}
system "sudo -K\n";
close (BLKID);

Then sudo accepts the password (in the '$passwd' variable) just fine but 
it forces the output to the browser (no line feeds so I know the 
printing is not coming from: 'print "$test\n";'). If I have a valid 
'sudo' time stamp and remove the leading pipe from the 'open' call then 
the output does go to the program so I know that the '2>&1 |' is 
redirecting the output back into the program just fine.

   The problem is that when I invalidate the 'sudo' timestamp just 
before calling the code above so that it must get the password from the 
second line it fails. If I have the leading pipe, it gets the password 
but the output goes to the browser. If I remove the leading pipe the 
output goes to the script but it fails to receive the password.

   Any ideas or tips? Thanks very much as always!

Madison
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list