Hello nysus,

Have you tried to execute the script with sudo?

Sample:

sudo hosts.pl

For me the following worked just fine:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; sub read_hosts { my $file = "/etc/hosts"; open (my $fh, "<", $file) or die "Can't open $file for read: $!"; my @hosts = <$fh>; close $fh or die "Cannot close $file: $!"; return @hosts; } sub write_hosts { my $file = "/etc/hosts"; open (my $fh, ">", $file) or die "Can't open $file for read: $!"; for my $host (@_) { print $fh $host; } # Or print $fh $_ for (@_); close $fh or die "Cannot close $file: $!"; return; } my @hosts = read_hosts; print Dumper \@hosts; $hosts[2] = "Test_host\t127.0.0.4\n"; write_hosts(@hosts); my @updates = read_hosts(); print Dumper \@updates; __END__ sudo perl hosts.pl $VAR1 = [ '127.0.0.1 localhost ', '127.0.1.1 user ', '# The following lines are desirable for IPv6 capable hosts ', '::1 ip6-localhost ip6-loopback ', 'fe00::0 ip6-localnet ', 'ff00::0 ip6-mcastprefix ', 'ff02::1 ip6-allnodes ', 'ff02::2 ip6-allrouters' ]; $VAR1 = [ '127.0.0.1 localhost ', '127.0.1.1 user ', 'Test_host 127.0.0.4 ', '::1 ip6-localhost ip6-loopback ', 'fe00::0 ip6-localnet ', 'ff00::0 ip6-mcastprefix ', 'ff02::1 ip6-allnodes ', 'ff02::2 ip6-allrouters' ];

Update: Adding print $fh $_ for (@_);

Update2: Or use Sudo module:

Make sure the script "hosts.pl" is executable chmod +x hosts.pl

#!/usr/bin/perl use strict; use warnings; use Sudo; my $su; my $name = "root"; my $pass = "password"; $su = Sudo->new( { sudo => '/usr/bin/sudo', #sudo_args => '...', username => $name, password => $pass, program => '/home/user/hosts.pl', #program_args => '...' } ); my $result = $su->sudo_run(); if (exists($result->{error})) { printf "STDERR: %s\n",$result->{error}; #&handle_error($result); } else { printf "STDOUT: %s\n",$result->{stdout}; printf "STDERR: %s\n",$result->{stderr}; printf "return: %s\n",$result->{rc}; } __END__ $ perl sudo.pl STDOUT: $VAR1 = [ '127.0.0.1 localhost ', '127.0.1.1 user ', ' ', '::1 ip6-localhost ip6-loopback ', 'fe00::0 ip6-localnet ', 'ff00::0 ip6-mcastprefix ', 'ff02::1 ip6-allnodes ', 'ff02::2 ip6-allrouters' ]; $VAR1 = [ '127.0.0.1 localhost ', '127.0.1.1 user ', 'Test_host 127.0.0.4 ', '::1 ip6-localhost ip6-loopback ', 'fe00::0 ip6-localnet ', 'ff00::0 ip6-mcastprefix ', 'ff02::1 ip6-allnodes ', 'ff02::2 ip6-allrouters' ]; STDERR: return: 1

Hope this helps.

Seeking for Perl wisdom...on the process of learning...not there...yet!

In reply to Re: Best way to write to a file owned by root? by thanos1983
in thread Best way to write to a file owned by root? by nysus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.