Description:

This script can be used to manipulate the IPv4 octet.

List of used variables:

VariableDescription




Add-in code:

/**
 * @param {Config} config
 * @param {Context} context
 * Do not edit this comment or parameter types. Required for code suggestions
*/
exports.main = function (config, context) {
    // Example 1: manually change an octet
    var myIp = config.vars.myIP; // myIp = "192.168.1.1"
    var octs = myIp.split( "." );   // octs = ["192","168", "1", "1"]
    octs[ 3 ] = "33" ;               // octs = ["192","168", "1", "33"]
    var newIp = octs.join( "." );   // newIp = "192.168.1.33"




    // Example 2: use a function to set a specific octet to a value
    var setOct = function (ip, oct, value) {
        var octs = ip.split( "." );
        octs[oct] = value;
        return octs.join( "." );
    }

    var inputIp = "10.11.12.13"
    var gatewayIp = setOct(inputIp, 3 , 1 ); // "10.11.12.1"
    var switchIp = setOct(inputIp, 3 , 65 ); // "10.11.12.65"


    // Example 3: use a function to increment an octet value by a given value
    var incrOct = function (ip, oct, value) {
        var octs = ip.split( "." );
        var oldValue = parseInt(octs[oct]);
        if (oldValue + value <= 255 ) {
            // don't go above 255
            octs[oct] = "" + (oldValue + value);
        }
        return octs.join( "." );
    }

    var inputIp = "10.11.12.13"
    var switchIp = incrOct(inputIp, 3 , 2 ); // 10.11.12.15
};

Add-in as JSON file:

ipv4_octet_manipulation.json