3

I'd like to write data to a file, but the file handle should be opened with sudo or else I get permission denied error. But looks like something like following is not possible in perl?

sudo open (FH, "> $filename") or die "$!\n";
user1060517
  • 355
  • 1
  • 5
  • 17
  • 1
    possible duplicate of [How to open Perl file handle to write data via sudo (or as another user)](http://stackoverflow.com/questions/5823665/how-to-open-perl-file-handle-to-write-data-via-sudo-or-as-another-user) – scrappedcola Sep 09 '13 at 16:07
  • 3
    http://stackoverflow.com/questions/9739653/perl-system-calls-when-running-as-another-user-using-sudo – fugu Sep 09 '13 at 16:08
  • The problem is similar to How to open Perl file handle to write data via sudo (or as another user), but I cannot run the script which is opening a file as sudo. – user1060517 Sep 09 '13 at 16:30
  • Then take a step back and help us understand what you actually mean to accomplish, and why that is not an adequate solution. – tripleee Sep 09 '13 at 16:36
  • I am trying to open a file in a secure location from a perl script. The perl script cannot run as "sudo", so I am trying to elevate the user permissions in the perl script when it tries to open the file/write to file. What are the possible ways to do so? – user1060517 Sep 09 '13 at 16:41

2 Answers2

5

sudo is a linux command, it is not a Perl function. You can run the whole Perl script with sudo (sudo perl script.pl), or you can change your user id in Perl by assigning to $< and $> special variables (see perlvar - Perl predefined variables) which will only be possible with extra privileges, anyway.

BTW, open sets $! on failure, not $@.

choroba
  • 231,213
  • 25
  • 204
  • 289
0
open(my $pipe_fh, '-|', 'sudo', 'cat', $filename) or die "Unable to open pipe: $!\n";

It creates another process to solve your problem that may be better solved by running the script with the correct rights.

tjd
  • 4,064
  • 1
  • 24
  • 34
  • Ok, so if i have to create a file when it doesnt exist or append to existing one, what should be the above command? My tried something like - my $table_mapping = '/opt/temp'; unless (-e $table_mapping) { open (my $TBLFILE, '-|', 'sudo', '>> $table_mapping') or die "Can't open '$table_mapping': $!\n"; close $TBLFILE; } – user1060517 Sep 09 '13 at 17:46
  • `-|` is to a pipe what `<` is to a file. Conversely, `|-` allows you to write to a pipe, but if you need external programs to proxy so much of your IO, you *really* need to reexamine the rights under which you're running your script. ... and no, the external command `sudo >> $table_mapping` probably won't do what you want. – tjd Sep 09 '13 at 18:32
  • Thanks, I got the reading from file right. However, writing a simple string to the file seems complicated. Can you give me an example of appending a line to a file? I am also trying to re examine user permission issue. – user1060517 Sep 09 '13 at 19:16
  • In the same way I've used `cat` above to read a file, you can probably abuse `tee` to write a file. Of course it will have the side effect of also sending everything you write to STDOUT. This way lies madness..... – tjd Sep 09 '13 at 19:33