Solved

send flow of text to perl

Posted on 2013-12-21
12
370 Views
Last Modified: 2013-12-21
this command  :

 sflowtool -4 -p 3601 | /scripts/DoSTargets

returns


1387652837 178.20.228.47 15 mbps
1387652837 178.20.228.47 1792 pps
1387652837 185.9.156.35 11 mbps
1387652837 185.9.159.233 1536 pps
1387652838 185.9.156.35 11 mbps
1387652839 178.20.228.47 26 mbps
1387652839 178.20.228.47 2560 pps
1387652839 85.100.207.114 8 mbps
1387652840 178.20.228.47 27 mbps
1387652840 178.20.228.47 3072 pps
1387652840 185.9.159.233 12 mbps
1387652840 185.9.159.233 2304 pps
1387652841 178.20.228.47 20 mbps
1387652841 178.20.228.47 2048 pps
1387652841 89.253.155.97 8 mbps
1387652842 178.20.228.47 20 mbps
1387652842 178.20.228.47 1792 pps
1387652843 178.20.228.47 15 mbps
1387652843 178.20.228.47 1792 pps

Open in new window


some outputs like this and how should i read them line by line on perl and split them to
$identifier
$ip
$reason
0
Comment
Question by:3XLcom
  • 6
  • 5
12 Comments
 
LVL 31

Expert Comment

by:farzanj
ID: 39733996
Ok,

For is this the input line format?
1387652837 178.20.228.47 15 mbps


And then
Identifier is 1387652837
IP is 178.20.228.47
Reason is  15 mbps

Is this correct?? How do you want to see the output?
0
 

Author Comment

by:3XLcom
ID: 39733997
yes i am trying to do it


that is what i do :

use strict;
use warnings;


use strict;
use warnings;

open(my $fh, '-|', 'sflowtool -4 -p 3601  | /scripts/DoSTargets') or die $!;
while (my $line = <$fh>) {
    # Do stuff with each $line.
        print  $line;
}
close $fh;

Open in new window



and this is the result :

[root@sflow islemler]# perl deneme.pl
1387662093 178.20.228.85 47 mbps
1387662093 178.20.228.85 4096 pps
1387662093 185.9.159.233 18 mbps
1387662093 185.9.159.233 2816 pps
1387662093 178.20.227.77 9 mbps
1387662094 185.9.159.233 2048 pps
1387662095 178.20.228.85 59 mbps
1387662095 178.20.228.85 5120 pps
1387662095 185.9.159.233 16 mbps
1387662095 185.9.159.233 4096 pps
1387662095 178.20.231.82 1792 pps
1387662095 185.9.156.136 1536 pps



i need to split into variables in  
# Do stuff with each $line.
this part
for ex.
$first
$second
$third
$forth
word should be perfect
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 39734006
while ( <$fh> ){
  my($first,$second,$third,$forth) = split;
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

by:3XLcom
ID: 39734009
i am newbee

use strict;
use warnings;


use strict;
use warnings;

open(my $fh, '-|', 'sflowtool -4 -p 3601  | /scripts/DoSTargets') or die $!;
while (my $line = <$fh>) {
       my($first,$second,$third,$forth) = split;
        print $first;
}
close $fh;

Open in new window


[root@sflow islemler]# perl deneme.pl
Use of uninitialized value in split at deneme.pl line 10, <$fh> line 1.
Use of uninitialized value in print at deneme.pl line 11, <$fh> line 1.
Use of uninitialized value in split at deneme.pl line 10, <$fh> line 2.
Use of uninitialized value in print at deneme.pl line 11, <$fh> line 2.
Use of uninitialized value in split at deneme.pl line 10, <$fh> line 3.
Use of uninitialized value in print at deneme.pl line 11, <$fh> line 3.
Use of uninitialized value in split at deneme.pl line 10, <$fh> line 4.
Use of uninitialized value in print at deneme.pl line 11, <$fh> line 4.
Use of uninitialized value in split at deneme.pl line 10, <$fh> line 5.
Use of uninitialized value in print at deneme.pl line 11, <$fh> line 5.
Use of uninitialized value in split at deneme.pl line 10, <$fh> line 6.
Use of uninitialized value in print at deneme.pl line 11, <$fh> line 6.
Use of uninitialized value in split at deneme.pl line 10, <$fh> line 7.
Use of uninitialized value in print at deneme.pl line 11, <$fh> line 7.
Use of uninitialized value in split at deneme.pl line 10, <$fh> line 8.
Use of uninitialized value in print at deneme.pl line 11, <$fh> line 8.
Use of uninitialized value in split at deneme.pl line 10, <$fh> line 9.
Use of uninitialized value in print at deneme.pl line 11, <$fh> line 9.

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 39734020
while ( <$fh> ){
       my($first,$second,$third,$forth) = split;

or

while ( my $line = <$fh> ) {
       my($first,$second,$third,$forth) = split(' ',$line);
0
 

Author Comment

by:3XLcom
ID: 39734021
nothing resulted for that


use strict;
use warnings;


use strict;
use warnings;

open(my $fh, '-|', 'sflowtool -4 -p 3601  | /scripts/DoSTargets') or die $!;
while (my $line = <$fh>) {
       my($first,$second,$third,$forth) = split(' ',$line);
        print $first;
}
close $fh;

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 39734026
that would print 138766209313876620931387662093138766209313876620931387662094138766209513876620951387662095138766209513876620951387662095 with no separation between them
0
 

Author Comment

by:3XLcom
ID: 39734029
I have changed to other scripts output as this "-" delimiter

[root@sflow islemler]# nano deneme.pl
[root@sflow islemler]# sflowtool -4 -p 3601  | /scripts/DoSTargets
1387664109-37.123.101.178-26-mbps
1387664109-37.123.101.178-2304-pps
1387664109-176.41.215.141-8-mbps
1387664109-185.9.159.233-2048-pps
1387664109-88.253.50.145-8-mbps
1387664109-5.135.155.180-2048-pps
1387664109-185.9.159.233-8-mbps
1387664109-185.9.156.136-1536-pps
1387664110-178.20.227.72-12-mbps
1387664110-178.20.227.72-2048-pps

Open in new window



and my code to :

open(my $fh, '-|', 'sflowtool -4 -p 3601  | /scripts/DoSTargets') or die $!;
while (my $line = <$fh>) {


my($first,$second,$third,$forth) = split("-",$line);
print $first;

}
close $fh;

Open in new window



again empty output
0
 
LVL 84

Expert Comment

by:ozo
ID: 39734037
Again, that would print 1387664109138766410913876641091387664109138766410913876641091387664109138766410913876641101387664110 with no separation and no line terminator
0
 

Author Comment

by:3XLcom
ID: 39734039
if i put into for each it works that gives nothing

my @answer = split(' ', $line);
print("$_\n") for (@answer);
0
 

Author Closing Comment

by:3XLcom
ID: 39734043
after print i put a "\n" it worked
0
 
LVL 84

Expert Comment

by:ozo
ID: 39734047
if you want each Identifier printed on a separate line, you could do

$\="\n";
while ( <$fh> ){
       my($first,$second,$third,$forth) = split;
       print $first;
}
or
while ( <$fh> ){
       my($first,$second,$third,$forth) = split;
       print "$first\n";
}
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question