Should be similar on other switch types.
- Retrieve current settings about tagged/untagged VLAN
- Modify VLAN settings
- Write it back
- Save switch state
Be sure not to cut yourself from switch management 🙂
You should have SNMP enabled and configured on switch (user with permission to read and write). We are using there two functions to decompose and compose HEX-STRING readed from switch. Also SNMP PHP package need to be compiled and enabled.
Full example, we are removing port 10 from VLAN1 and adding port 9 tagged:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
<?php // FUNCTIONS /** * Sorry for this ugly thing * @return string */ function fillWithZeroesFromLeftSide($text, $chars) { $string = ""; if(strlen($text) < $chars) { // How many zeroes to add? $needTo = $chars-(strlen($text)); // Make string to glue $text with for($a = 0; $a < $needTo; $a++) { $string .= "0"; } } return $string.$text; } /** * Returns array with ports which are in VLAN * @param $hex "FF 7F FE F0 00 00 00 00" * @return array */ function hexToPortArray($hex) { $p = array(); // "sanitize" $hex = str_replace(" ", "", trim($hex)); // For every char in HEX STRING, do decomposition for($level = 0; $level < strlen($hex); $level++) { // Take char $part = substr($hex, $level, 1); // Leading "B" is there to have right offset starting with 1 $binary = "B".fillWithZeroesFromLeftSide((string)base_convert($part, 16, 2), 4); // Decompose for($a = 1; $a <= 4; $a++) { $value = substr($binary, $a, 1); if($value == "1") { $p[($level*4)+$a] = 1; } else { $p[($level*4)+$a] = 0; } } } return $p; } /** * Returns back HEX STRING with ports * @param $pole Array * @return string */ function portArrayToHex($array) { $o = ""; // Take element from array for($i = 1; $i <= count($array); $i++) { // Convert it to HEX it with 3 elements that follows $o .= base_convert($array[$i].$array[$i+1].$array[$i+2].$array[$i+3], 2, 16); // Skip those 3 elements $i = $i + 3; } return strtoupper($o); } // EXAMPLE // Connect to switch $snmp = new \snmp(SNMP::VERSION_2C, $IP, "private", "200000" , 1); // We dont need SNMP type hint in returned value snmp_set_valueretrieval(SNMP_VALUE_PLAIN); // VLAN number is the last part of OID $VLAN1taggedOID = ".1.3.6.1.4.1.171.10.76.20.1.7.6.1.2.1"; $VLAN1untaggedOID = ".1.3.6.1.4.1.171.10.76.20.1.7.6.1.4.1"; // Retrieve and make an array with ports from returned value $VLAN1tagged = hexToPortArray($snmp->get($VLAN1taggedOID)); $VLAN1untagged = hexToPortArray($snmp->get($VLAN1untaggedOID)); // We got array, where key is port number and value is if VLAN is present // (1) or not (0) in (un)tagged state // Now, we want port 10 removed from VLANs and port 9 to have tagged. // To be sure, we remove both ports from (un)tagged VLANS and // just add port 9 to tagged VLAN $VLAN1tagged[9] = 0; $VLAN1tagged[10] = 0; $snmp->set($VLAN1taggedOID, "x", portArrayToHex($VLAN1tagged)); $VLAN1untagged[9] = 0; $VLAN1untagged[10] = 0; $snmp->set($VLAN1untaggedOID, "x", portArrayToHex($VLAN1untagged)); // Adding port 9 tagged $VLAN1tagged[9] = 1; $snmp->set($VLAN1taggedOID, "x", portArrayToHex($VLAN1tagged)); // Save configuration var_dump($snmp->set(".1.3.6.1.4.1.171.10.76.20.1.1.10.0", "i", "1")); |