Should be similar on other switch types.

  1. Retrieve current settings about tagged/untagged VLAN
  2. Modify VLAN settings
  3. Write it back
  4. 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:

<?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"));