{"id":370,"date":"2015-06-06T19:11:00","date_gmt":"2015-06-06T18:11:00","guid":{"rendered":"http:\/\/tricker.cz\/?p=370"},"modified":"2017-03-10T11:32:40","modified_gmt":"2017-03-10T10:32:40","slug":"setting-vlan-ports-with-snmp-and-php-on-d-link-dgs-1210-28","status":"publish","type":"post","link":"https:\/\/tricker.cz\/?p=370","title":{"rendered":"Setting VLAN ports with SNMP and PHP on D-Link DGS-1210-28"},"content":{"rendered":"<p>Should be similar on other switch types.<\/p>\n<ol>\n<li>Retrieve current settings about tagged\/untagged VLAN<\/li>\n<li>Modify VLAN settings<\/li>\n<li>Write it back<\/li>\n<li>Save switch state<\/li>\n<\/ol>\n<p>Be sure not to cut yourself from switch management \ud83d\ude42<\/p>\n<p>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.<\/p>\n<p>Full example, we are removing port 10 from VLAN1 and adding port 9 tagged:<\/p>\n<pre class=\"toolbar:1 lang:php decode:true  \">&lt;?php\r\n\r\n  \/\/ FUNCTIONS\r\n\r\n  \/**\r\n   * Sorry for this ugly thing\r\n   * @return string\r\n   *\/\r\n  function fillWithZeroesFromLeftSide($text, $chars)\r\n  {\r\n\r\n    $string = \"\";\r\n\r\n    if(strlen($text) &lt; $chars)\r\n    {\r\n\r\n      \/\/ How many zeroes to add?\r\n      $needTo = $chars-(strlen($text));\r\n      \r\n      \/\/ Make string to glue $text with\r\n      for($a = 0; $a &lt; $needTo; $a++)\r\n      {\r\n        $string .= \"0\";\r\n      }\r\n\r\n    }\r\n\r\n    return $string.$text;\r\n\r\n  }\r\n\r\n  \/**\r\n   * Returns array with ports which are in VLAN\r\n   * @param $hex \"FF 7F FE F0 00 00 00 00\"\r\n   * @return array\r\n   *\/\r\n\r\n  function hexToPortArray($hex)\r\n  {\r\n\r\n    $p = array();\r\n\r\n    \/\/ \"sanitize\"\r\n    $hex = str_replace(\" \", \"\", trim($hex));\r\n\r\n    \/\/ For every char in HEX STRING, do decomposition\r\n    for($level = 0; $level &lt; strlen($hex); $level++)\r\n    {\r\n\r\n      \/\/ Take char\r\n      $part = substr($hex, $level, 1);\r\n\r\n      \/\/ Leading \"B\" is there to have right offset starting with 1\r\n      $binary = \"B\".fillWithZeroesFromLeftSide((string)base_convert($part, 16, 2), 4);\r\n\r\n      \/\/ Decompose\r\n      for($a = 1; $a &lt;= 4; $a++)\r\n      {\r\n\r\n        $value = substr($binary, $a, 1);\r\n\r\n        if($value == \"1\")\r\n        {\r\n          $p[($level*4)+$a] = 1;\r\n        }\r\n        else\r\n        {\r\n          $p[($level*4)+$a] = 0;\r\n        }\r\n\r\n      }\r\n\r\n    }\r\n\r\n    return $p;\r\n\r\n  }\r\n\r\n  \/**\r\n   * Returns back HEX STRING with ports\r\n   * @param $pole Array\r\n   * @return string\r\n   *\/\r\n\r\n  function portArrayToHex($array)\r\n  {\r\n\r\n    $o = \"\";\r\n\r\n    \/\/ Take element from array\r\n    for($i = 1; $i &lt;= count($array); $i++)\r\n    {\r\n\r\n      \/\/ Convert it to HEX it with 3 elements that follows\r\n      $o .= base_convert($array[$i].$array[$i+1].$array[$i+2].$array[$i+3], 2, 16);\r\n\r\n      \/\/ Skip those 3 elements\r\n      $i = $i + 3;\r\n\r\n    }\r\n\r\n    return strtoupper($o);\r\n\r\n  }\r\n\r\n  \/\/ EXAMPLE\r\n\r\n  \/\/ Connect to switch\r\n  $snmp = new \\snmp(SNMP::VERSION_2C, $IP, \"private\", \"200000\" , 1);\r\n  \/\/ We dont need SNMP type hint in returned value\r\n  snmp_set_valueretrieval(SNMP_VALUE_PLAIN);\r\n  \r\n  \/\/ VLAN number is the last part of OID\r\n  $VLAN1taggedOID = \".1.3.6.1.4.1.171.10.76.20.1.7.6.1.2.1\";\r\n  $VLAN1untaggedOID = \".1.3.6.1.4.1.171.10.76.20.1.7.6.1.4.1\";\r\n  \r\n  \/\/ Retrieve and make an array with ports from returned value\r\n  $VLAN1tagged = hexToPortArray($snmp-&gt;get($VLAN1taggedOID));\r\n  $VLAN1untagged = hexToPortArray($snmp-&gt;get($VLAN1untaggedOID));\r\n  \r\n  \/\/ We got array, where key is port number and value is if VLAN is present \r\n  \/\/ (1) or not (0) in (un)tagged state\r\n\r\n  \/\/ Now, we want port 10 removed from VLANs and port 9 to have tagged.\r\n  \/\/ To be sure, we remove both ports from (un)tagged VLANS and \r\n  \/\/ just add port 9 to tagged VLAN\r\n  $VLAN1tagged[9] = 0;\r\n  $VLAN1tagged[10] = 0;\r\n  $snmp-&gt;set($VLAN1taggedOID, \"x\", portArrayToHex($VLAN1tagged));\r\n  $VLAN1untagged[9] = 0;\r\n  $VLAN1untagged[10] = 0;\r\n  $snmp-&gt;set($VLAN1untaggedOID, \"x\", portArrayToHex($VLAN1untagged));\r\n  \r\n  \/\/ Adding port 9 tagged\r\n  $VLAN1tagged[9] = 1;\r\n  $snmp-&gt;set($VLAN1taggedOID, \"x\", portArrayToHex($VLAN1tagged));\r\n  \r\n  \/\/ Save configuration\r\n  var_dump($snmp-&gt;set(\".1.3.6.1.4.1.171.10.76.20.1.1.10.0\", \"i\", \"1\"));\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 \ud83d\ude42 You should have SNMP enabled and configured on&#8230; <a class=\"more-link\" href=\"https:\/\/tricker.cz\/?p=370\">Pokra\u010dovat ve \u010dten\u00ed &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"_links":{"self":[{"href":"https:\/\/tricker.cz\/index.php?rest_route=\/wp\/v2\/posts\/370"}],"collection":[{"href":"https:\/\/tricker.cz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tricker.cz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tricker.cz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tricker.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=370"}],"version-history":[{"count":6,"href":"https:\/\/tricker.cz\/index.php?rest_route=\/wp\/v2\/posts\/370\/revisions"}],"predecessor-version":[{"id":502,"href":"https:\/\/tricker.cz\/index.php?rest_route=\/wp\/v2\/posts\/370\/revisions\/502"}],"wp:attachment":[{"href":"https:\/\/tricker.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tricker.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tricker.cz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}