CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
Append two lines to a file through perl!!

 



UnixAAP
Novice

Mar 28, 2014, 3:42 AM

Post #1 of 4 (19340 views)
Append two lines to a file through perl!! Can't Post

HI Gurus,

I have created a dummy script to append two lines in an existing file.

my $fileone = "/etc/fileone";
my $fileadd = "#Forward messages to server by IP" & ".*.\@1.2.3.4";
$ssh->system("echo $fileadd >> $fileone");


But my issue is I want a line break between
Forward messages to server by IP
&
.*.\@1.2.3.4

That means the two lines should be added to '' fileone" one after the other ..

Dunno how to add it .... Please help!!

Thanks in advance :-)


BillKSmith
Veteran

Mar 29, 2014, 8:02 AM

Post #2 of 4 (19280 views)
Re: [UnixAAP] Append two lines to a file through perl!! [In reply to] Can't Post


Code
use strict; 
use warnings;
my $fileone = "/etc/fileone";
my @fileadd = (
"&",
".*.\@1.2.3.4",
);

open my $ADD, '>>', $fileone or die "Cannot open fileone: $!";
local $" = "\n";
print {$ADD} "@fileadd\n";
close $ADD;

Good Luck,
Bill


UnixAAP
Novice

Apr 10, 2014, 3:14 AM

Post #3 of 4 (13672 views)
Re: [BillKSmith] Append two lines to a file through perl!! [In reply to] Can't Post

Hi Bill,
I tried modifying with your inputs
It looks like this .The Ip 1.2.3.4 is the Ip address where i want to forward my local syslogs to remote machine 1.2.3.4.
I want to add this two lines in local machine syslog.conf

# Forward messages to server by IP
.*.\@1.2.3.4


vi newtest1.pl
1 #!/usr/bin/perl -w
2
3 use strict;
4 use warnings;
5 use Net::OpenSSH;
6
7 #print "Password: ";
8 system('stty','-echo');
9 #chomp(my $password = <STDIN>);
10 system('stty','echo');
11 print "\n";
12
13 open my $fh, '<', 'host-list.txt' or die "failed to open 'host-list.txt' <$!>";
14
15 while (my $host = <$fh>) {
16 chomp $host;
17 if ($host !~ /^\d+\.\d+\.\d+\.\d+$/) {
18 print "$host is not in IPv4 format.\n";
19 next;
20 }
21 print "Adding files to $host";
22 my $ssh = Net::OpenSSH->new("root\@$host");
23 $ssh->error and die "Couldn't establish SSH connection: ". $ssh->error;
24 my $syslog_filename = "/etc/syslog.conf";
25 my $syslogadd = {"# Forward messages to server by IP"}, "&"
26 {".*.\@1.2.3.4"};
27 open my $ADD, '>>', "$syslog_filename";
28 local $" = "\n";
29 print $ADD "$syslogadd\n";
30 close $ADD;



But i get below error:-

./newtest1.pl
syntax error at ./newtest1.pl line 26, near "{"
Global symbol "$syslog_filename" requires explicit package name at ./newtest1.pl line 27.
Global symbol "$syslogadd" requires explicit package name at ./newtest1.pl line 29.
Missing right curly or square bracket at ./newtest1.pl line 30, at end of line
Execution of ./newtest1.pl aborted due to compilation errors.


BillKSmith
Veteran

Apr 11, 2014, 5:42 AM

Post #4 of 4 (12968 views)
Re: [UnixAAP] Append two lines to a file through perl!! [In reply to] Can't Post

Your last error message tells you that the perl compiler got to the end of your script and did not find a match for the brace "}" on line 15. Fixing this will probably fix the other "errors".

However, you missed my point. Not hard to do because I had misunderstood your problem. (I thought that the "&" was part of you text and that the "\" was an escape character) Please post an accurate sample of the output that you want. I recommend that you attach your input and output files rather than pasting them into a window.

UPDATE: Here is my best guess of what you want!


Code
#!/usr/bin/perl -w  

use strict;
use warnings;
use IO::Prompt;
use Net::OpenSSH;
use Regexp::Common::net qw/net/;


my $password = prompt( 'Password: ', -echo=>'*');

open my $fh, '<', 'host-list.txt'
or die "failed to open 'host-list.txt' <$!>";

while ( my $host = <$fh> ) {
chomp $host;
if ( $host !~ /^RE{net}{IPv4}$/ ) {
warn "$host is not in IPv4 format.\n";
next;
}
print "Adding files to $host";
my $ssh = Net::OpenSSH->new("root\@$host");
$ssh->error and die "Couldn't establish SSH connection: " . $ssh->error;
my $syslog_filename = "/etc/syslog.conf";
open my $ADD, '>>', $syslog_filename
or die "Could not open '$syslog_filename' <$!>";
print {$ADD} "# Forward messages to server by IP\n";
print {$ADD} ".*.\@1.2.3.4",
close $ADD;
}

Good Luck,
Bill

(This post was edited by BillKSmith on Apr 12, 2014, 11:54 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives