using sudo in a perl script

Mike Waychison mike-DlQxw/23Tq2aMJb+Lgu22Q at public.gmane.org
Wed Dec 8 18:56:32 UTC 2004


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Madison Kelly wrote:
> 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!


See perldoc -f open:


"(You are not allowed to "open" to a command that pipes both in and out,
but see IPC::Open2, IPC::Open3, and "Bidirectional Communication" in
perlipc for alternatives.)"



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

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFBt05fdQs4kOxk3/MRAiq9AJ4tB5cjtigYyp6g4yjXbb60bPO6YACfQz4R
NfGDlUiEFe09dsbObzyNOk0=
=EZfy
-----END PGP SIGNATURE-----
--
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