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: Perl Programming Help: Beginner:
writing to a file from perl

 



Xploit
Novice

Apr 18, 2011, 9:09 AM

Post #1 of 2 (444 views)
writing to a file from perl Can't Post

all im trying to do here is put the output of the command into a file. and its not working. what am i doing wrong here? code im using is below:

#!/usr/bin/perl
use warnings;
use strict;


open INPUT, "<ips.txt" or die $!;
my @file = <INPUT>;
open OUTPUT, ">>output.txt" or die $!;

for (@file) {
select OUTPUT;
system("ping -c 1 $_");

}


miller
User

Apr 18, 2011, 10:05 AM

Post #2 of 2 (441 views)
Re: [Xploit] writing to a file from perl [In reply to] Can't Post

Using select in that way won't work because the output of system is printing to a different STDOUT.

Instead either capture the results of your external command using backticks ``, or have the system command append to the file.


Code
#!/usr/bin/perl  
use warnings;
use strict;

my @ips = do {
open my $fh, 'ips.txt' or die $!;
<$fh>;
};
chomp @ips;

open my $fh, '>>', 'output.txt' or die $!;

for (@file) {
print $fh `ping -c 1 $_`;
}


or


Code
for (@file) {  
system("ping -c 1 $_ >> output.txt");
}


- Miller


(This post was edited by miller on Apr 18, 2011, 10:06 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