开发者

How to extract ip addresses and port numbers using Perl and compare it

I have a file containing a port numbers and ip addresses in a string. I have to extract the ip addresses, calculate the port number according to the formula and compare it to the port number in the file and print the ones that dont match. If the ip address is W.X.Y.Z the f开发者_高级运维ormula for port number is 50000+200(Y)+Z. The text file is of the following format.

exchangeA_5=53413 ;239.189.17.13 7990

exchangeA_6=53415 ;239.189.17.15 7990

exchangeA_e=53470 ;239.189.27.70 7990

exchangeA_5=53468 ;239.189.27.68 7990

What is the best way to do it?

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

open(my $fh, '<', 'fileC')
or die("Can't open fileC: $!\n");

while (<$fh>) {
    chomp;
    my ($key, $val) = split /=/;
    #print "$key\n";
    #print "$val\n";

    my ($port, $ip) = split /[;]/, $val;
    print "$port\n";
    print "$ip\n";

}


Quick and dirty:

perl -ne '($host, $port, @ip) = split /[=;.]/; print if $port != 50000+200*$ip[2]+$ip[3]' fileC

Of course, you want to rewrite this into a nice program :)

Paul


open(my $fh, '<', 'fileC.txt');
while (<$fh>) {
    chomp;
    if (!/^$/){
    ($Host,$port, @IP) =  split /[=;.]/;
    print "Host:$Host, IP:", (join '.',@IP),", and Port:$port\n";   
}}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜