Gheek.net

January 27, 2008

vlanrangeset_cgi

Filed under: cgi, Cisco, perl — Tags: , , , — lancevermilion @ 11:38 pm
#!/usr/bin/perl
#Author: Lance Vermilion
#Description: Create a list of vlans in a range format "1,3-300,302-999"
#                 to be set to on trunk ports.
################################################


use strict;
use warnings;
use CGI qw(:standard);

sub usage () {
print
  "Content-type: text/html\n\n" .
  "\n\n" .
  "Vlan Range Results\n" .
  "\n" .
  "\n" .
  "

ERROR

\n" . "

\n" . "\n"; print "\nYOU MUST FILL OUT EVERY FIELD\n"; print "\n" . "\n\n"; exit 0; } #### # Question for the user #### my $numbers = param('numbers'); if ( $numbers eq "" ) { &usage; } else { my @nums; #my @lines = split(/\s+/, $numbers) if ($numbers =~ /\s+/); my @lines = split(/\n/, $numbers); #my @lines = split(/,/, @lines) if @lines =~ /,/; foreach my $line (@lines) { if ( $line ne "" ) { chomp $line; split(/\s+/, $line); split(/\n/, $line); split(/,/, $line); push @nums, $line; } } #Sample data #my @num = sort {$a$b} qw( 1 20 21 22 23 24 40 60 100 101 102 140 141 145 ); my @num = sort {$a$b} @nums; my @new = (); my $span = 0; for( my $ii = 0; $ii <= $#num; $ii++ ) { if( defined $num[$ii - 1] && $num[$ii - 1] + 1 == $num[$ii] ) { if( $span == 1 ) { next; } else { $new[$#new] .= '-'; $span = 1; } } elsif( $span == 1 ) { $new[$#new] .= $num[$ii - 1]; push @new, $num[$ii]; $span = 0; } else { push @new, $num[$ii]; } } my @DISPLAY = join(',', @new)."\n"; print "Content-type: text/html\n\n" . "\n\n" . "LAN IP Range Results\n" . "\n" . "\n" . "

LAN IP Range Results

\n" . "

\n" . "\n"; print "@DISPLAY"; print "\n" . "\n\n"; exit 0; }

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: