Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.54 KB | None | 0 0
  1. <?php
  2. # HP OfficeConnect / 3com SFP Plus series Port/MAC/IP Mapper
  3. # pre-alpha / proof of concept version
  4. # Usage: $scriptname HOST COMMUNITY
  5.  
  6. if ( empty($argv[1]) ) {
  7.   printf("%s : Faltando host\ndigite %s ip-ou-host-do-switch snmp_community\n", $argv[0], $argv[0]);
  8.   exit;
  9. }
  10.  
  11. if ( empty($argv[2]) ) {
  12.   printf("%s : Faltando community \ndigite %s %s snmp_community\n", $argv[0], $argv[0], $argv[1]);
  13.   exit;
  14. }
  15.  
  16. $snmp_ip = $argv[1];
  17. $snmp_community = $argv[2];
  18.  
  19. #Force snmpwalk to output numeric OIDs
  20. snmp_set_oid_output_format( SNMP_OID_OUTPUT_NUMERIC );
  21.  
  22. ####### OIDs collect ###########
  23. #1.3.6.1.2.1.17.4.3.1.1
  24. #    [IF-MIB::ifName.18] => STRING: GigabitEthernet1/0/18
  25. $snmprawports = snmprealwalk( $snmp_ip , $snmp_community, "1.3.6.1.2.1.31.1.1.1.1");
  26.  
  27. #1.3.6.1.2.1.17.4.3.1.1 -> MACS
  28. #    [.1.3.6.1.2.1.17.4.3.1.1.0.24.243.61.250.16] => Hex-STRING: 00 18 F3 3D FA 10
  29. $snmprawmaclist = snmprealwalk( $snmp_ip , $snmp_community, "1.3.6.1.2.1.17.4.3.1.1");
  30.  
  31. #1.3.6.1.2.1.17.4.3.1.2 -> PORTINT
  32. #    [.1.3.6.1.2.1.17.4.3.1.2.0.24.243.61.250.16] => INTEGER: 469
  33. $snmprawportint = snmprealwalk( $snmp_ip , $snmp_community, "1.3.6.1.2.1.17.4.3.1.2");
  34.  
  35. #1.3.6.1.2.1.17.1.4.1.2. -> PORTIF
  36. #    [.1.3.6.1.2.1.17.1.4.1.2.469] => INTEGER: 31
  37. $snmprawportif = snmprealwalk( $snmp_ip , $snmp_community, "1.3.6.1.2.1.17.1.4.1.2");
  38.  
  39. #OID: 1.3.6.1.2.1.3.1.1.2.30 -> IPs/MAC
  40. #    [.1.3.6.1.2.1.3.1.1.2.30.1.192.168.69.5] => Hex-STRING: 38 2C 4A 6C E0 E6
  41. $snmprawipmac = snmprealwalk( $snmp_ip , $snmp_community, "1.3.6.1.2.1.3.1.1.2.30");
  42.  
  43. #TODO set it before, change code to make life easier
  44. snmp_set_quick_print(1);
  45.  
  46. #Switch data:
  47. $swname = snmp2_get($snmp_ip , $snmp_community, "sysName.0");
  48. $swlocation = snmp2_get($snmp_ip , $snmp_community, "sysLocation.0");
  49. $swip = snmp2_get($snmp_ip , $snmp_community, "1.3.6.1.2.1.67.1.2.1.1.3.1.2.1");
  50. $swuptime = snmp2_get($snmp_ip , $snmp_community, "1.3.6.1.2.1.1.3.0");
  51.  
  52.  
  53. # Data arrays
  54. $hp_portnames = @array();
  55. $macip_table = @array();
  56. $hp_portdata = @array();
  57.  
  58.  
  59. # Data generation
  60.  
  61. foreach($snmprawports as $oid =>  $data) {
  62.   $portname = str_replace("STRING: ", "", $data);
  63.   $idx = str_replace(".1.3.6.1.2.1.31.1.1.1.1.", "", $oid);
  64.   $hp_portnames[$idx] = $portname;
  65. }
  66.  
  67. foreach($snmprawipmac as $key => $value) {
  68.   $macip = str_replace(".1.3.6.1.2.1.3.1.1.2.30.1.", "", $key);
  69.   $macaddr = str_replace("Hex-STRING: ", "", $value);
  70.   $macaddr = str_replace(" ", "-", rtrim($macaddr));
  71.   $macip_table[$macaddr] = $macip;
  72. }
  73.  
  74. foreach ($snmprawportint as $keys =>  $valor) {
  75.   $portentry = str_replace("INTEGER: ", "", $valor);
  76.   $keyoid = str_replace(".1.3.6.1.2.1.17.4.3.1.2.", "", $keys);
  77.   $macoid = ".1.3.6.1.2.1.17.4.3.1.1." . $keyoid;
  78.   $macentry = str_replace(" ", "-", rtrim(str_replace("Hex-STRING: ", "", $snmprawmaclist[$macoid])));
  79.   $portifoid = ".1.3.6.1.2.1.17.1.4.1.2." . $portentry ;
  80.   $portif = str_replace("INTEGER: ", "", $snmprawportif[$portifoid]);
  81.   if (array_key_exists($macentry, $macip_table)) {
  82.     $macipaddr = $macip_table[$macentry];
  83.   } else {
  84.     $macipaddr = "NO-IP";
  85.   }
  86.   $macip = array ( 'macaddr' => $macentry, 'ipaddr'=> $macipaddr );
  87.   if (array_key_exists($portif, $hp_portdata)) {
  88.     array_push ($hp_portdata[$portif]['macs'], $macip);
  89.   } else {
  90.     $hp_portdata[$portif] = array( 'portentry' => $portentry, 'portif' => $portif, 'macs' => array($macip) );
  91.   }
  92. #  printf("portentry: %s, macentry: %s, portif: %s, macip: %s\n", $portentry, $macentry, $portif, $macip) ;
  93. }
  94.  
  95. echo ("\n");
  96.  
  97. printf("Switch: %s\t/\tIP: %s\t/\tUptime:%s\nLocal: %s\n\n\n", $swname, $swip, $swuptime, $swlocation);
  98.  
  99. foreach ($hp_portnames as $ifindex => $ifname) {
  100.   $portstr = " --- " . $ifname . " ";
  101.   $portstr = str_pad($portstr,100,"-");
  102.  
  103.   if (array_key_exists($ifindex, $hp_portdata)) {
  104.     printf("%s\n", $portstr );
  105.     foreach ( $hp_portdata[$ifindex]['macs'] as $macentry ) {
  106.       $macaddr = $macentry['macaddr'];
  107.       $ipaddr = $macentry['ipaddr'];
  108.       $macaddr = str_replace("-","", strtolower($macaddr));
  109.       $macaddrdash = wordwrap($macaddr, 2, '-', true);
  110.       $macaddrreal = wordwrap($macaddr, 2, ':', true);
  111.       $macaddrsw = wordwrap($macaddr, 4, '-', true);
  112.       printf("MAC: %s / %s / %s\t\tIP: %s\n", $macaddrdash, $macaddrreal, $macaddrsw, $ipaddr);
  113.     }
  114.     echo("\n");
  115.   } else {
  116. #DEBUG
  117. #    printf("%s\nNo Entry\n", $portstr );
  118.  }
  119. }
  120.  
  121. #DEBUG
  122. #print_r($snmprawports);
  123. #print_r($hp_portnames);
  124. #print_r($hp_portdata);
  125. #print_r($snmprawmaclist);
  126. #print_r($snmprawportint);
  127. #print_r($snmprawportif);
  128. #print_r($snmprawipmac);
  129.  
  130. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement