Nortel portdiscoverer script

Nortel portdiscoverer script

Nortel portdiscoverer script 150 150 Roderick Derks

A couple of months ago I developped this script that pulls information out of Nortel routers and switches via snmp, and then creates a list with the following information about each connected device of which the interface (NIC) is up and running:

  • MAC address
  • IP address
  • DNS name
  • Stack
  • Switch
  • Portnumber

Result:

  • The output of this scripts makes it easy to locate the switch and portnumber a device is connected on. This can save you a lot of time and.
  • A csv file with the info from the list above sorted like this:
    00 1B 25 49 46 42;10.0.0.1;t001b252e4662.domain.com.;stack-h1;450;24;3;25
  • A webpage that is hosted on the same server so that your collegues can easily access the information (secure the webpage with i.e. Apache LDAP access because it contains sensitive info about your network)
  • A history so you can see when a network node has been moved, switched off (not available)
    Remember that every csv file is just a moment in time.
  • This script saves me a lot of time daily when I have to look up the location of a device!

The following hardware is supported:

  • Nortel Passport 8600
  • Nortel switches 5520, 5510, 470, 450
  • Other models have not been tested but might be supported too after some script adjustment

Prequisits

  • linux OS, perl, the snmptool snmpbulkwalk
  • all switches need to have the same SNMP read community string
  • I know that you need the Nortel SNMP MIB file rfc1573rcc.mib (it's in the downloadable package and installed automaticaly when running the install script). You might need other Nortel MIB files, I'm not sure. Let me know!
  • if you want to use the webpage you need to have Apache running on your server

Installation

  • uncompress the tar file i.e. in /tmp
  • run the install script
  • files will be added in /usr/local/portdiscoverer /etc/http/conf.d and /usr/share/snmp/mibs, never overwritten
  • edit the file that contains the list of switches you want to query in /usr/local/portdiscoverer/etc
  • reload the local apache install to activate http://localhost/portdiscoverer
  • a configuration example for ldap access to a W2003R2 Active Directory is located in /etc/http/conf.d/portdiscoverer.conf

Execution

  • Two command line parameters:
    portdiscoverer.pl full
    fullscan alle switches, output in csv file and on html page, email to subscribers to let people know the script was executed
    portdiscoverer.pl <switch dnsname|ipaddress>
    scan one switch, output on console (fast and furious)
  • schedule the cron like this:
    8 9 * * * /usr/local/portdiscoverer/bin/portdiscoverer.pl full > /dev/null 2>&1
    It takes me around 15 minutes to query around 20 stacks with 60 switches and around 2500 connected devices. Mind you that the older switchtypes (nortel 450 and 470) take much longer to query, especially when the stack contains more then 3 switches.

Download!

Enjoy! Let me know if you like it or not when you use it, I would appreciate that.

Cheers!

 

 

#!/usr/bin/perl # #    Copyright (C) 2008 Roderick Derks #    W: http://www.r71.nl #     #    This program is free software; you can redistribute it and/or modify #    it under the terms of the GNU General Public License as published by #    the Free Software Foundation; either version 2 of the License, or #    (at your option) any later version. #     #    This program is distributed in the hope that it will be useful, #    but WITHOUT ANY WARRANTY; without even the implied warranty of #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.    See the #    GNU General Public License for more details. #     #    You should have received a copy of the GNU General Public License #    along with this program; if not, write to the Free Software #    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA    02111-1307    USA # #    Adjusted by Roderick Derks (email: roderick at r71.nl) #    Output of snmp query for remaing battery runtime is in minutes or in hours. This #    is a problem when you want to create some nice graphs because of the different #    output. Now hours are converted to minutes. # #    Networkscan for collecting and combining the following data #    on Nortel Passport 8600, 5520, 5510, 470, 450 routers and switches #     - MAC addresses #     - IP addresses #     - DNS names #     - Stack #     - Switch #     - Portnumber # #    v1.0.4 25-08-2008 #    v1.0.5 26-08-2008 #    v1.0.6 26-08-2008 #    v2.0.0 28-08-2008 -> strategie gewijzigd, per switch mac adressen opvragen ipv aan de hand van arptabel op router #    v2.1.1 29-08-2008 -> webpagina genereren #    v2.1.2 29-08-2008 -> webpagina en csv file publiceren op andere lokatie #    v2.2.0 03-09-2008 -> history opbouwen door files met datumnotatie te kopieren naar history dir #    v2.3.1 16-09-2008 -> PORTID's al van te voren opvragen en in een hash gooien (sub FindPortId2) -> 30% tijdwinst #    v2.4.1 16-09-2008 -> Switch- en poortnummers van te voren op te vragen en in een hash gooien -> 50% tijdwinst #    v2.4.2 24-09-2008 -> Fout hersteld met uitprinten tijdens debug #    v2.4.3 24-09-2008 -> bug: webpagina verscheen niet als debugmode lager dan 3 was #    v2.5.0 24-09-2008 -> Uitbreiding: per stack info opvragen en op commandline weergeven. 1e commandline parameter is stack adres #    v2.5.1 24-09-2008 -> Kleine aanpassing: commandline parameter full #    v2.5.2 07-05-2009 -> Download version #    v2.5.3 26-08-2009 -> Added support to use a different snmp string for each stack # ###############################################################  # configurable parameters:  $router=ezh2; $communitystring_router = "public";  # snmp read access community string for router $communitystring_default = "public"; # read access community string for switches $email_address="your_address\@domain.com"; # leave in the \ before the @ $debug="0";  # do not adjust anything from here if you have doubts about what you are doing  $baystacklist="/usr/local/portdiscoverer/etc/baystacklist";  ############################################################## &current_time_and_date;  $procesfile="/usr/local/portdiscoverer/bin/portdiscoverer.lock"; $resultfile="/usr/local/portdiscoverer/var/portdiscoverer.csv"; $tempfile="/usr/local/portdiscoverer/var/portdiscoverer.tmp"; $resultfileonweb="/var/www/html/portdiscoverer/portdiscoverer.csv"; $webpagedir="/var/www/html/portdiscoverer"; $webpage="/var/www/html/portdiscoverer/portdiscoverer.html"; $fakemibfile="/usr/share/snmp/mibs/SNMPv2-SMI.mib"; # to prevent snmp commands using the MIBS ALL setting which preolads MIB files $historydir="/usr/local/portdiscoverer/var/history"; $historyfile="${historydir}/${year}${mon}${mday}${hour}${min}_portdiscoverer.csv";   ############################################################################# # MAIN ROUTINE ############################################################################# # # &make_html; # &OrderByIp; # &make_html; #exit 0;   #&GetBayStacks;  &load_baystacks;   if ( "${ARGV[0]}" eq "" ) {    print "$0 <full>\nfullscan alle switches, output on html page\n\n";    print "$0 <switch dnsname|ipaddress> [snmp RO string]\nscan one switch, output on console\n\n";    exit 1; } if ( "${ARGV[0]}" eq "full" ) {      $mode = "fullreport";      system "echo $$ > $procesfile";     &GetArpTableFromRouter;      open(FILE_WRITE, "> $resultfile");      &current_time_and_date;     $starttime="${mday}-${mon}-${year} ${hour}:${min}:${sec}";      foreach $line (@BayStacks) {          # split info uit $line (stack adres en snmp RO string)         $Stack =  ((split /:/, ${line})[0]);         $communitystring =  ((split /:/, ${line})[1]);         &ZuigDeStackLeeg      }      &current_time_and_date;     $endtime="${mday}-${mon}-${year} ${hour}:${min}:${sec}";      close (FILE_WRITE);      &OrderByIp;       if (! -d "$historydir") {        print "dir $historydir does not exist, creating it...\n" if ( $debug >= 1) ;        system "mkdir $historydir";        }     if (! -d "$webpagedir") {        print "dir $webpagedir does not exist, creating it...\n" if ( $debug >= 1) ;        system "mkdir $webpagedir";        }       system "cp -f $resultfile $webpage";     system "cp -f $resultfile $historyfile";     system "echo \"Portdiscoverer tabel is beschikbaar: http://\${HOSTNAME}/portdiscoverer/portdiscoverer.html\" | /bin/mail -s \"Portdiscoverer tabel\" $email_address";      &make_html;      system "cp -f $resultfile $resultfileonweb";     system "rm -f $procesfile";  }  else {       $Stack = "$ARGV[0]" ;     $stack = "$ARGV[0]" ;     $mode = "singlestack";      if ( "${ARGV[1]}" eq "" ) {        $communitystring = "$communitystring_default"; # read access needed     }     else {        $communitystring = "${ARGV[1]}"; # read access needed     }      print "Scan one switch ${ARGV[0]}, output on console\n";     print "Informatie ophalen uit de routers...\n";     &GetArpTableFromRouter;     print "Informatie ophalen uit stack ${ARGV[0]}...\n";     &ZuigDeStackLeeg ; }  exit 0;   ############################################################################# #############################################################################  sub GetArpTableFromRouter {      print "Getting Arp table from ${router}\n" if ($debug >= 3);      # opvragen MAC adressen per switch en in een tabel jassen      open(ARPTABLE, "snmpbulkwalk -v2c -c ${communitystring_router} $router IP-MIB::ipNetToMediaPhysAddress|");     @ARPTABLE = <ARPTABLE>;     close(ARPTABLE);      print "command: snmpbulkwalk -v2c -c ${communitystring_router} $router IP-MIB::ipNetToMediaPhysAddress\n" if ($debug >= 3);      %ArpTable=();                                         # create the table      foreach $IpMac (@ARPTABLE) {         chomp $IpMac;        $Ip = $IpMac;        $Mac = $IpMac;         # RFC1213-MIB::ipNetToMediaPhysAddress.2056.10.20.180.254 = Hex-STRING: 00 00 5E 00 01 B4        $Ip = ((split / = /, ${Ip})[0]);        $Ip = ((split /ipNetToMediaPhysAddress./, ${Ip})[1]);        $Ip =~ s/[0-9][0-9][0-9][0-9].//g;        #print "Ip: ${Ip}\n" if ($debug >= 3);                $Mac = ((split /Hex-STRING: /, ${Mac})[1]);        chomp $Mac;        chop $Mac;        $Mac2 = $Mac;        $Mac2 =~ s/ /:/g;        #print "Mac: ${Mac} ${Mac2}\n" if ($debug >= 3);         # Toevoegen aan tabel        #print "Naar tabel ArpTable: $Mac, $Ip\n" if ($debug >= 3);        ${ArpTable}{"$Mac"} =  "${Ip}";      }     print "size of hash:  " . keys( %ArpTable ) . ".\n" if ($debug >= 3);     for $key ( keys %ArpTable ) {  $value = $ArpTable{$key};  print "$key => $value\n" if ($debug >= 2); }  }  #############################################################################  sub GetSwitchTypeInfo {    open(OUTPUT, "snmpbulkwalk -m ${fakemibfile} -v2c -c ${communitystring} ${Stack} SNMPv2-MIB::sysDescr.0|");   @OUTPUT = <OUTPUT>;   close(OUTPUT);    print "command: snmpbulkwalk -m ${fakemibfile} -v2c -c ${communitystring} ${Stack} SNMPv2-MIB::sysDescr.0\n" if ($debug >= 3);   $Stacktype="@OUTPUT";   chomp $Stacktype;   print "searching for Stacktype and number\n" if ($debug >= 3);   print "command: snmpbulkwalk -v2c -c ${communitystring} ${Stack} SNMPv2-MIB::sysDescr.0\n" if ($debug >= 3);   print "Stacktype $Stacktype\n" if ($debug >= 3);   $Stacktype = ((split /STRING: /, ${Stacktype})[1]);   print "Stacktype $Stacktype\n" if ($debug >= 3);    if ( $Stacktype =~ /BayStack/ ) {       $Stacktype = ((split /BayStack /, ${Stacktype})[1]);       print "BayStack Stacktype $Stacktype\n" if ($debug >= 3);   }   else {       $Stacktype = ((split /Switch /, ${Stacktype})[1]);       print "Stacktype $Stacktype\n" if ($debug >= 3);   }       $Stacktype = ((split / /, ${Stacktype})[0]);       print "Stacktype $Stacktype\n" if ($debug >= 3);    my @Stacktype_char = split /-/, $Stacktype;    $StackTypeNumber = "${Stacktype_char[0]}";   $NrPortsOnSwitch = ((split /T/, ${Stacktype_char[1]})[0]);    chomp $NrPortsOnSwitch; chomp $StackTypeNumber;    print "Stacktypenumber: ${StackTypeNumber}\n" if ($debug >= 2);   print "nr of ports: ${NrPortsOnSwitch}\n" if ($debug >= 2);  }   #############################################################################  sub current_time_and_date {     # get time and date and set layout of how time variables are printed to the screen    ($sec, $min, $hour, $mday, $mon, $year, $wday) = localtime(time) ;   $mon = sprintf("%0.2d", ++$mon);   $mday = sprintf("%0.2d", $mday);   $hour = sprintf("%0.2d", $hour);   $min = sprintf("%0.2d", $min);   $sec = sprintf("%0.2d", $sec);   $year = $year + 1900;   } # close subroutine  #############################################################################  sub FindPortId {       $we_got_em=0;      foreach $PortId ( @PORTIDS ) {         chomp $PortId;         print "looking for SNMPv2-SMI::mib-2.17.4.3.1.2.${HostId} in $PortId\n" if ($debug >= 3);         if ( "$PortId" =~ "SNMPv2-SMI::mib-2.17.4.3.1.2.${HostId} " ) {     # spatie erachter om einde oid aan te geven            print "PortId 1: $PortId\n"  if ($debug >= 3);             $PortId = ((split /INTEGER: /, ${PortId})[1]);            print "PortId 2: $PortId\n" if ($debug >= 3);             $we_got_em=1;            $PortId2 = "$PortId";         }         last if ( $we_got_em == 1 );      }      $PortId = "$PortId2";  }  ##############################################################################  sub FindSwitchAndPortNumber {       $we_got_em=0;      foreach $SwitchAndPortNr ( @SWITCH_PORT_NUMBERS ) {        chomp $SwitchAndPortNr;         if ( ${StackTypeNumber} == "450" ) {           print "looking for RFC1213-MIB::ifDescr.${PortId} in $SwitchAndPortNr\n" if ($debug >= 3);          if ( "$SwitchAndPortNr" =~ "RFC1213-MIB::ifDescr.${PortId} " ) {     # spatie erachter om einde oid aan te geven             print "switch_and_port:\t$SwitchAndPortNr\n" if ($debug >= 3);              $Switch = ((split /module /, $SwitchAndPortNr)[1]);            print "switch 1:\t${Switch}\n" if ($debug >= 3);             $Switch = ((split /\, port/, ${Switch})[0]);            print "Switch 2:\t${Switch}\n" if ($debug >= 3);              $port = ((split /port /, $SwitchAndPortNr)[1]);            $port = ((split /\"/, ${port})[0]);            print "port 1:\t${port}\n" if ($debug >= 3);              $we_got_em=1;            $Switch2 = "$Switch";            $port2 = "$port";          }        }        else {          print "looking for IF-MIB::ifName.${PortId} in $SwitchAndPortNr\n" if ($debug >= 3);          if ( "$SwitchAndPortNr" =~ "IF-MIB::ifName.${PortId} " ) {     # spatie erachter om einde oid aan te geven             print "switch_and_port:\t$SwitchAndPortNr\n" if ($debug >= 3);              $Switch = ((split /Slot: /, $SwitchAndPortNr)[1]);            print "Switch 1:\t${Switch}\n" if ($debug >= 3);             $Switch = ((split / Port:/, ${Switch})[0]);            print "Switch 2:\t${Switch}\n" if ($debug >= 3);              $port = ((split /Port: /, $SwitchAndPortNr)[1]);            $port = ((split /\)/, ${port})[0]);            print "port 1:\t${port}\n" if ($debug >= 3);              $we_got_em=1;            $Switch2 = "$Switch";            $port2 = "$port";          }        }        last if ( $we_got_em == 1 );         $Switch = "$Switch2";        $port   = "$port2";      } # $SwitchAndPortNr ( @SWITCH_PORT_NUMBERS ) }  ##############################################################################  sub GetAllInfoRightNow {      #     # opvragen MAC adressen van een stack/switch en in een tabel jassen     #      open(OUTPUT, "snmpbulkwalk -v2c -m ${fakemibfile} -c ${communitystring} ${Stack} SNMPv2-SMI::mib-2.17.4.3.1.1|");     @MAC_ADDRESSES = <OUTPUT>;     close(OUTPUT);     print "Collect MAC addresses on the stack ${Stack}\n" if ($debug >= 3);     print "command: snmpbulkwalk -v2c -m ${fakemibfile} -c ${communitystring} ${Stack} SNMPv2-SMI::mib-2.17.4.3.1.1\n" if ($debug >= 3);      # Find port ID's and put 'm in a hash..     # snmpbulkwalk -v2c -c public 10.20.3.250 SNMPv2-SMI::mib-2.17.4.3.1.2.0.17.133.194.65.24     # SNMPv2-SMI::mib-2.17.4.3.1.2.0.17.133.194.65.24 = INTEGER: 133      print "Colect port ID's and put 'm in a hash..\n" if ($debug >= 3);     print "command: snmpbulkwalk -v2c -m ${fakemibfile} -c ${communitystring} ${Stack} SNMPv2-SMI::mib-2.17.4.3.1.2\n" if ($debug >= 3);     open(PORTIDS, "snmpbulkwalk -v2c -m ${fakemibfile} -c ${communitystring} ${Stack} SNMPv2-SMI::mib-2.17.4.3.1.2|");     @PORTIDS = <PORTIDS>;     close(PORTIDS);      #     # Find port- and switchnumbers and put 'm in a hash     #      if ( ${StackTypeNumber} == "450" ) {         print "Collect switchnr and portnr and put 'm in a hash:\n" if ($debug >= 3);        print "command: snmpbulkwalk -v2c -m ${fakemibfile} -c ${communitystring} ${Stack} RFC1213-MIB::ifDescr\n" if ($debug >= 3);        open(SWITCH_PORT_NUMBERS, "snmpbulkwalk -v2c -m ${fakemibfile} -c ${communitystring} ${Stack} RFC1213-MIB::ifDescr|");        @SWITCH_PORT_NUMBERS = <SWITCH_PORT_NUMBERS>;        close(SWITCH_PORT_NUMBERS);     }     else {        print "Collect switchnr and portnr and put 'm in a hash:\n" if ($debug >= 3);        print "command: snmpbulkwalk -v2c -m ${fakemibfile} -c ${communitystring} ${Stack} IF-MIB::ifName\n" if ($debug >= 3);        open(SWITCH_PORT_NUMBERS, "snmpbulkwalk -v2c -m ${fakemibfile} -c ${communitystring} ${Stack} IF-MIB::ifName|");        @SWITCH_PORT_NUMBERS = <SWITCH_PORT_NUMBERS>;        close(SWITCH_PORT_NUMBERS);     }  }  ##############################################################################  sub ZuigDeStackLeeg {      # Het Plan:     # 1. stack systeeminfo uitlezen     # 2. met 1 snmp query mac tabel op stack uitlezen     # 3. met 1 snmp query portid tabel op de stack uitlezen     # 4. met 1 snmp query port en switch tabel op de stack uitlezen     # 5. info combineren: mac -> portid -> port en switch nummer -> ip adres arp tabel router     # 6. dns naam resolven indien ip adres is gevonden     # 7. schrijven naar array en naar file        &GetSwitchTypeInfo;     &GetAllInfoRightNow;       foreach $MacOpDeStack (@MAC_ADDRESSES) {         chomp $MacOpDeStack;        chop $MacOpDeStack;        $HostId = $MacOpDeStack;        # RFC1213-MIB::mib-2.17.4.3.1.1.0.224.216.10.82.174 = Hex-STRING: 00 E0 D8 0A 52 AE        print "MacOpDeStack: $MacOpDeStack\n" if ($debug >= 3);        $MacOpDeStack = ((split /Hex-STRING: /, ${MacOpDeStack})[1]);        print "MacOpDeStack: ${MacOpDeStack}\n" if ($debug >= 3);        $MacId = ${MacOpDeStack};        $MacId =~ s/\ //g;        $MacOpDeStackOfficieel = ${MacOpDeStack};        $MacOpDeStackOfficieel =~ s/\ /\:/g;        print "MacId: ${MacId}\n" if ($debug >= 3);         $HostId = ((split /17.4.3.1.1./, ${HostId})[1]);        print "HostId: ${HostId}\n" if ($debug >= 3);        $HostId = ((split / = /, ${HostId})[0]);        print "HostId: ${HostId}\n" if ($debug >= 3);         &FindPortId;        &FindSwitchAndPortNumber;         if ( $port != ${NrPortsOnSwitch} ) {            $IpArpTable =""; # flush it           $MacArpTable =""; # flush it            # HIER OPZOEKEN BIJBEHORENDE IP ADRES IN %ArpTable           print "Zoek naar ${MacOpDeStack} en bijbehorend ip adres\n" if ($debug >= 3 );            for $MacArpTable ( keys %ArpTable ) {                      $IpArpTable = $ArpTable{$MacArpTable};                      print "Zoek naar ${MacOpDeStack}: $MacArpTable => $IpArpTable\n" if ($debug >= 3 );                      $MacArpTable2 = "$MacArpTable";                      if ( "$MacArpTable" eq "$MacOpDeStack" ) { $MacArpTable2 = "$MacArpTable"; $SearchSuccess=1; }                       if ( "$MacArpTable" ne "$MacOpDeStack" ) { $IpArpTable = ""; $SearchSuccess=0; }                      last if ( "$MacArpTable" eq "$MacOpDeStack" ) ;            }            if ( $SearchSuccess == 0 ) {              print "MAC: $MacOpDeStack in ArpTabel niet teruggevonden. Geen bijbehorend IP adres gevonden $IpArpTable\n" if ($debug >= 3 );           }           elsif ( $SearchSuccess == 1 ) {              print "MAC: $MacOpDeStack in ArpTabel teruggevonden: ${MacArpTable2}, bijbehorend IP adres gebruiken: $IpArpTable\n" if ($debug >= 3 );           }            # DNS resolve alleen indien MAC en IP adres in ARP tabel zijn gevonden           if ($IpArpTable) {              @octet = split /\./, $IpArpTable;             if ( "${octet[3]}"  < 255 ) {                print "resolven ${IpArpTable}\n" if ($debug >= 3);                open(DNS, "dig -x $IpArpTable +short +time=1 2> /dev/null|");                @DNS = <DNS>;                close(DNS);                if ( $? == 0 ) {                   $Dns="@DNS";                   chomp $Dns;                   $Dns =~ s/\n//g; #alias wordt aangeleverd op twee regels, <CR> verwijderen                   if ( $Dns =~ "reply from unexpected source" ) {                      $Dns="dns server returned error";                   }                }                print "Dns: ${Dns}\n" if ($debug >= 3);             }           }            # SCHRIJVEN NAAR EEN TABEL             $HostTable="HostId_${MacId}";           print "Naar tabel ${HostTable}: ${MacOpDeStack}, ${HostId}, ${Stack}, ${StackTypeNumber}, ${NrPortsOnSwitch}, $port, $Switch\n" if ($debug >= 3);           %${HostTable}=();                                         # create the table           %${HostTable} = ( 'MacOpDeStack' => "${MacOpDeStack}",                           'HostId' => ${HostId},                  # add key and value to the table                           'Stack' => ${Stack},                           'StackTypeNumber' => ${StackTypeNumber},                            'NrPortsOnSwitch' => ${NrPortsOnSwitch},                           'Port' => ${port},                           'Switch' => ${Switch},                            'Ip' => ${IpArpTable},                            'Dns' => ${Dns}, );             print "size of hash:  " . keys( %${HostTable} ) . ".\n" if ($debug >= 3);           #for my $key ( keys %${HostTable} ) {  my $value = $$HostTable{$key};  print "$key => $value\n" if ($debug >= 2); }           for $key ( keys %${HostTable} ) {  $value = $$HostTable{$key};  print "$key => $value\n" if ($debug >= 2); }    #          print FILE_WRITE "${MacOpDeStack}, ${IpArpTable}, ${Dns}, ${Stack}, ${StackTypeNumber}-${NrPortsOnSwitch}, $Switch/$port\n";            $rapport = "${MacOpDeStack}\;${IpArpTable}\;${Dns}\;${Stack}\;${StackTypeNumber}\;${NrPortsOnSwitch}\;${Switch};${port}\n";   #             # this is to create a report that can be printed to the screen or to a file #             formline q{@<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<< @>>>-@< @>/@<< #             }, ${MacOpDeStackOfficieel}, $IpArpTable, $Dns, ${Stack}, ${StackTypeNumber}, ${NrPortsOnSwitch}, ${Switch}, ${port}; #             #this is to print the collected statistics to a variable #             $rapport = $^A; #             $^A = ""; # leegmaken anders wordt het rapport cumulatief               # singlestack or full report              if ( "$mode" eq "fullreport" ) {                 print FILE_WRITE "$rapport";              }              else {                 print "$rapport";              }               $MacOpDeStackOfficieel="";              $IpArpTable="";              $Dns="";              $Switch="";              $port="";         }        else {             print "Found MAC on port $port and number of ports on switch is ${NrPortsOnSwitch} and this is maybe a trunk port so we are skipping it.\n" if ($debug >= 3);        }         } # foreach $Stack  }  ##############################################################################  sub WriteToHash {         #$${table}{$MacOpDeStack} = "$HostId";				# add key $MacOpDeStack with value $ServerId to table %table         #$${HostTable}{'HostId'}=${HostId};                        # add to table a key and a value        #$${HostTable}{'MacOpDeStack'}=${MacOpDeStack};         #$${HostTable}{'Stack'}=${Stack};         #$${HostTable}{'StackTypeNumber'}=${StackTypeNumber};         #$${HostTable}{'NrPortsOnSwitch'}=${NrPortsOnSwitch};    }  ##############################################################################   sub OrderByIp {    #system "cp ${resultfile} ${tempfile}";    $column = 1; # 0 is startingpoint, 1 is 2nd column    open(FILEWRITE, "> ${tempfile}");   open(FILEREAD, "< ${resultfile}");    my %lines;   while (<FILEREAD>) {       chomp;       #my $ip = (split)[$column]; # scheidingsteken is spatie       my $ip = (split /;/)[$column];       if ($ip =~ m/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/) {           $ip = ($1 * 256 * 256 * 256) + ($2 * 256 *256) + ($3 * 256) + $4;       } else {           $ip = 0;       }       $lines{$ip} = []           unless $lines{$ip};       push @{ $lines{$ip} }, $_;   }   foreach my $ary (sort keys %lines) {       foreach my $l (@{ $lines{$ary} }) {           print $l, "\n";           print FILEWRITE $l, "\n";       }   }   close (FILEREAD);   close (FILEWRITE);    system "mv -f ${tempfile} ${resultfile}";   #system "rm -f ${tempfile}"; }  ##############################################################################  sub load_baystacks {     # Open file for input    open(BAYSTACKLIST, "<$baystacklist");    # Walk through data file    while (<BAYSTACKLIST>) {        # Skip blank lines       next if (/^\n$/);       # Skip comments       next if (/^#/);        #print "DEBUG: adding $_ to our list of devices \n" if ($DEBUG);        # Remove the CR/LF       chomp;        push (@BayStacks, $_);     } #end while     close(BAYSTACKLIST);     return 1;  } #end sub load_baystacks   ##############################################################################  sub make_html {    # Open the webpage we are going to create open(WEBPAGE, ">$webpage"); flock(WEBPAGE,2) or die "cannot lock file: $!" ;  #write directly to the file, don't wait till the filehandle is closed select((select(WEBPAGE), $| = 1)[0]);  open(FILEREAD, "< ${resultfile}") || die print ">>>ERROR: Can't open FILEREAD: ${resultfile}\n"; @filecontent = <FILEREAD>; close FILEREAD; $NrOfLines = @filecontent;     print WEBPAGE qq| <HEAD>\n|;    print WEBPAGE qq| <TITLE>Portdiscoverer</TITLE>\n|;   # sommige speciale karakters worden voorafgegaan door een escape (oftwel een \) print WEBPAGE qq {    <meta name="author" content="Roderick Derks www.r71.nl">    <STYLE TYPE="text/css">    body {background-color: #FFFFFF}    a.alarm:link {line-height: 2; color: #990000; font-family: arial; font-weight: bold; text-decoration: none; font-size: 70% }   a.alarm:visited {line-height: 2; color: #990000; font-family: arial; font-weight: bold; text-decoration: none; font-size: 70% }   a.alarm:hover {line-height: 2; color: #990000; font-family: arial; font-weight: bold; text-decoration: none; font-size: 75% }   a.alarm:active {line-height: 2; color: #990000; font-family: arial; font-weight: bold; text-decoration: none; font-size: 75% }    a.melding:link {line-height: 2; color: navy; font-family: arial; text-decoration: none; font-size: 70% }   a.melding:visited {line-height: 2; color: navy; font-family: arial; text-decoration: none; font-size: 70% }   a.melding:hover {line-height: 2; color: navy; font-family: arial; text-decoration: none; font-weight: bold; font-size: 70% }   a.melding:active {line-height: 2; color: navy; font-family: arial; text-decoration: none; font-size: 70% }    a.headline:link {color: #000066; font-family: "CG Times"; font-weight: bold; text-decoration: none; font-size: 180% }   a.headline:visited {color: #000066; font-family: "CG Times"; font-weight: bold; text-decoration: none; font-size: 180% }   a.headline:hover {color: #000066; font-family: "CG Times"; font-weight: bold; text-decoration: underline; font-size: 180% }   a.headline:active {color: #000066; font-family: "CG Times"; font-weight: bold; text-decoration: none; font-size: 180% }    a.voetnoot:link {color: #000066; font-family: arial; font-weight: bold; text-decoration: none; font-size: 70% }   a.voetnoot:visited {color: #000066; font-family: arial; font-weight: bold; text-decoration: none; font-size: 70% }   a.voetnoot:hover {color: #000066; font-family: arial; font-weight: bold; text-decoration: underline; font-size: 70% }   a.voetnoot:active {color: #000066; font-family: arial; font-weight: bold; text-decoration: underline; font-size: 70% }    p.description {color: #000000; font-size: 90%; font-family: "Arial"; font-weight: bold; }   p.uitleg {color: #000000; font-size: 70%; font-family: "Arial";}   p.alinea {padding-left: 2cm}    </STYLE>   </HEAD>   <BODY leftmargin="10" topmargin="10" marginwidth="10" marginheight="20" bgcolor="#FFFFFF" onload="blinking_header()" onunload="stoptimer()">   <TABLE align="center" valign="top" id="Headline" cellSpacing="0" cellPadding="0" width="100%" border="0">    <tr bgcolor="#B6C4E0">     <th width="12"><img src="./images/bl_corner_top_left.jpg" width="12" height="12" alt=" "></th>     <td width="100%" height="12"></td>     <th width="12" height="10"><img src="./images/bl_corner_top_right.jpg" width="12" height="12" alt=" "></th>   </tr>   <tr bgcolor="#B6C4E0">     <td width="12" height="100%">&nbsp;</td>     <td width="100%" height="100%" align="center">  };    # eerste drie regels altijd vast: $link_same_page = "http://${HOSTNAME}/portdiscoverer/portddiscoverer.html"; print WEBPAGE qq|     <a target="_top" class="headline" href="$link">Portdiscoverer</a><br>\n|; print WEBPAGE qq|     <p target="_top" class="description"></a>overzicht netwerkapparatuur st. elisabeth ziekenhuis<br>\n|;  &current_time_and_date;            print WEBPAGE qq|       <p class="uitleg" >Gevonden MAC adressen: $NrOfLines</p>\n|;           print WEBPAGE qq|       <a target="_top" class="voetnoot" href="http://ezhappl21/portdiscoverer/portdiscoverer.csv">download csv file</a>\n|;           print WEBPAGE qq|       <p class="uitleg" >pagina gegenereert op ${mday}-${mon}-${year} ${hour}:${min}:${sec}</p><br>\n|;  print WEBPAGE qq {    <td width="12" height="100%">&nbsp;</td>   </tr>   <tr bgcolor="#B6C4E0">     <th width="12" height="10"><img src="./images/bl_corner_bottom_left.jpg" width="12" height="12" alt=" "></th>     <td  height="12"></td>     <th width="12" height="10"><img src="./images/bl_corner_bottom_right.jpg" width="12" height="12" alt=" "></th>   </tr>  </TABLE>   <TABLE align="center" valign="top" id="Alerts" cellSpacing="0" cellPadding="0" width="100%" border="0" bgcolor="#e7ecf6">    <tr bgcolor="#E7ECF6">     <th width="12"><img src="./images/lb_corner_top_left.jpg" width="12" height="12" alt=" "></th>     <td  height="12"></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td>     <th width="12" height="10"><img src="./images/lb_corner_top_right.jpg" width="12" height="12" alt=" "></th>   </tr>   <tr bgcolor="#E7ECF6">     <td width="12" height="100%">&nbsp;</td>     <td  height="100%" align="left"><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><p class="alinea" >      <br>  };          print WEBPAGE qq|     <tr class="melding"><td></td><td><p class="description">MAC</td><td><p class="description">IP</td><td><p class="description">DNS</td><td><p class="description">Stack</td><td><p class="description">Type</td><td><p class="description">Aantal poorten</td><td><p class="description">Switch</td><td><p class="description">Port</td><td></td></tr>|;  foreach my $line (@filecontent) {         #$line =~ s/\;/<\/td><td>/g;         $line =~ s/\;/<\/td><td><p class="uitleg">/g;         print "$line\n" if ($debug >= 3) ;         $line = "<td><p class=\"uitleg\">$line<\/td>";         #print WEBPAGE qq|     <a target="_top" class="melding" href="$link">$line</a><br>\n|;         print WEBPAGE qq|     <tr><td></td>$line<td></td></tr>|; }    # sommige speciale karakters worden voorafgegaan door een escape (oftwel een \) print WEBPAGE qq {  <br> </td> <td width="12" height="100%">&nbsp;</td> </tr> <tr bgcolor="#E7ECF6"> <th width="12" height="10"><img src="./images/lb_corner_bottom_left.jpg" width="12" height="12" alt=" "></th> <td height="12"></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td> <th width="12" height="10"><img src="./images/lb_corner_bottom_right.jpg" width="12" height="12" alt=" "></th> </tr>  </TABLE>  <br>  <TABLE align="center" valign="top" id="Bottom" cellSpacing="0" cellPadding="0" width="100%" border="0">  <tr bgcolor="#FFFFFF"> <th width="12"></th> <td height="12"></td> <th width="12" height="10"></th> </tr> <tr bgcolor="#FFFFFF"> <td width="12" height="100%">&nbsp;</td> <td  height="100%" align="center">  };             print WEBPAGE qq|       <a target="_self" class="voetnoot" href="http://${HOSTNAME}/portdiscoverer/portdiscoverer.html">portdiscoverer started at $starttime and finished at $endtime </a><br>\n|;     # Laatste deel vd tabel print WEBPAGE qq {  <td width="12" height="100%">&nbsp;</td>   </tr>   <tr bgcolor="#FFFFFF">     <th width="12" height="10"></th>     <td width="100%" height="12"></td>     <th width="12" height="10"></th>   </tr>  </TABLE>  </BODY> };  close(WEBPAGE);  } # einde sub   

Roderick Derks

Liefhebber van fietsen, van het oplossen van IT puzzels, en van het delen van informatie om anderen te helpen.

All stories by:Roderick Derks

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

    Your Name (required)

    Your Email (required)

    Subject

    Your Message

      Your Name (required)

      Your Email (required)

      Subject

      Your Message