Solved

text line with no range or eg

Posted on 2011-02-21
2
291 Views
Last Modified: 2012-05-11
Pls refer to my earlier question:
http://www.experts-exchange.com/Programming/Languages/Scripting/Perl/Q_26836792.html

I have lines which do not have range or eq as listed in question earlier.

Sample text:
permit      tcp      host_10_13_20_62      host_10_14_6_84      port_30094
permit      tcp      host_10_13_35_51      host_10_14_6_84      port_30094

permit      tcp      10_96_72_64      29      host_10_14_5_53      Netbackup1      

permit      tcp      host_10_96_72_64       10_14_5_53 30      Netbackup1      

permit      tcp      10_13_35_51      30  10_14_6_84 22       port_30094

Here again I can have combination of 10_ and host_ addresses for source and destination. All 10_ addresses have a single/two digit mask separated by space or tab.
For application I need the last column.

Expected output:
source [ host_10_13_20_62 host_10_13_35_51 ] destination host_10_14_6_84 application port_30094

source 10_96_72_64_29 destination host_10_14_5_53 application Netbackup1

source host_10_96_72_64 destination 10_14_5_53_30 application Netbackup1

source 10_13_35_51_30 destination 10_14_6_84_22 application port_30094

Thank you.
0
Comment
Question by:dpk_wal
2 Comments
 
LVL 16

Accepted Solution

by:
sjklein42 earned 500 total points
ID: 34957794
This should handle both cases with and without a range

sub FlushIt
{
	my @sources = sort(keys(%sources));
	my $sourceCount = @sources;
	my $sourceString = ($sourceCount > 1 ) ? ( "[ " . join(" ",@sources) . " ]" ) : $sources[0];

	my @destinations = sort(keys(%destinations));
	my $destinationCount = @destinations;
	my $destinationString = ($destinationCount > 1 ) ? ( "[ " . join(" ",@destinations) . " ]" ) : $destinations[0];

	my @protocols = sort(keys(%protocols));
	my $protocolCount = @protocols;
	my $protocolString = join("_",@protocols) . (( $minPort ne '' ) ? ("_" . $minPort) : '') . (( $maxPort ne '' ) ? ("_" . $maxPort) : '');

	if ( $sourceString ne '' )
	{
		print "source $sourceString destination $destinationString application $protocolString\n";
	}

	undef %sources;
	undef %destinations;
	undef %protocols;
}


while ( <> )
{
	s/[\r\n]//g;

	if ( $_ eq '' ) { FlushIt(); }
	else
	{
		# All IP subnets starting with 10_ and followed by two digit mask;
		# should get listed as subnet_mask in the final output.
		# Eg, in text above, 10_12_10_0      23 should get listed as 10_12_10_0_23

		while ( s/(\s+10\_[0-9\_]+)\s+([0-9]+)(\s+)/$1\_$2$3/ ) {}

		#       permit      tcp      host_10_14_1_50            host_10_13_5_46      range      1531      1550

		s/^\s+//;		# trim leading spaces
		s/\s+$//;		# trim trailing spaces

		($disposition, $protocol, $source, $destination, $operator, $minPort, $maxPort) = split(/\s+/);
		##print STDERR join("\n", $disposition, $protocol, $source, $destination, $operator, $minPort, $maxPort) . "\n\n";

		$sources{$source} = 1;
		$destinations{$destination} = 1;
		if ( $operator eq 'range' ) { $protocols{$protocol} = 1; }
		else { $protocols{$operator} = 1; }
	}
}

FlushIt();

Open in new window


c:\temp>perl foo.pl foo.dat
source [ host_10_13_20_62 host_10_13_35_51 ] destination host_10_14_6_84 application port_30094
source 10_96_72_64_29 destination host_10_14_5_53 application Netbackup1
source host_10_96_72_64 destination 10_14_5_53_30 application Netbackup1
source 10_13_35_51_30 destination 10_14_6_84_22 application port_30094

Open in new window

0
 
LVL 32

Author Closing Comment

by:dpk_wal
ID: 34958013
Works great; thank you!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now