script to insert multiple IP address & MAC address in the computer table IP address & MAC address

geet
Tera Guru

Hi ,

 

I am trying to insert multiple IP address and MAC address from the related list "CI IPs" on the computer table in the IP Address and MAC address field. Below is the example record where I have added the IP address and MAC address manually.

geet_0-1685389314156.png

I am trying to write a BR to first try to upload the IP address in the IP address field so that the process can be automated but I am not sure how to do it for multiple IP at the same time. Please help!

 


(function executeRule(current, previous /*null when async*/ ) {
var IP = current.ip_address;
gs.addInfoMessage("getting IP-Address" + " " + IP);
var gr = new GlideRecord('cmdb_ci_computer'); //your Computer table name
gr.addQuery('sys_id', current.cmdb_ci);
gr.query();
if (gr.next()) {
//Update records
var IpAddress = gr.ip_address;
if (IP != IpAddress) {
gr.ip_address = IP;
gr.update();
}
}
})(current, previous);

2 REPLIES 2

Community Alums
Not applicable

For the initial update of all of them, you'll want to do something like this in a background script. 

function updateIPs(){

    //update the below constants for use in your exact scenario

    var RELATED_LIST_TABLE_NAME = 'cmdb_ci_network_adapter';
    var IP_IP_ADDRESS_FIELD_NAME = 'ip_address';
    var MAC_MAC_ADDRESS_FIELD_NAME = 'mac_address';

    var CMDB_TABLE_NAME = 'cmdb_ci_computer';
    var CI_IP_FIELD_NAME = 'u_ip_field_name';
    var CI_MAC_FIELD_NAME = 'u_mac_address_field_name';

    var grCI = new GlideRecord(CMDB_TABLE_NAME);
    grCI.query();
    while(grCI.next()){
        var tmpArrayIP = [];
        var tmpArrayMAC = [];
        var tmpUpdateFlag = false;
        var grIP = new GlideRecord(RELATED_LIST_TABLE_NAME);
        grIP.addQuery('cmdb_ci', grCI.getUniqueValue());
        grIP.query();
        while(grIP.next()){
            tmpArrayIP.push(grIP.getValue(IP_IP_ADDRESS_FIELD_NAME));
            tmpArrayMAC.push(grIP.getValue(MAC_MAC_ADDRESS_FIELD_NAME));
        }
        if(tmpArrayIP.length > 0){
            grCI.setValue(CI_IP_FIELD_NAME, tmpArrayIP.toString());
            tmpUpdateFlag = true;
        }
        if(tmpArrayMAC.length > 0){
            grCI.setValue(CI_MAC_FIELD_NAME, tmpArrayMAC.toString());
            tmpUpdateFlag = true;
        }
        if(tmpUpdateFlag){
            grCI.update();
        }
    }
}

updateIPs();

You can then use some similar logic to build yourself a business rule to keep everything in sync. As an example, I added two IP addresses to a network adaptor and added the comma separated version to the short_description field: 

MikeReading_0-1685392228755.png

 

Warning: if you have a large CMDB / busy instance - this will take a LONG time to run, and will lock the user session ./ thread that it's using to complete the task. 

 

Hi Mike,

 

Thanks for your response. I tried your script with a particular sys_id and it has a wired output. . Below is the script and output:

function updateIPs() {

    //update the below constants for use in your exact scenario

    var RELATED_LIST_TABLE_NAME = 'cmdb_ci_ip_address';
    var IP_IP_ADDRESS_FIELD_NAME = 'ip_address';
    var MAC_MAC_ADDRESS_FIELD_NAME = 'mac_address';

    var CMDB_TABLE_NAME = 'cmdb_ci_computer';
    var CI_IP_FIELD_NAME = 'ip_address';
    var CI_MAC_FIELD_NAME = 'mac_address';

    var grCI = new GlideRecord(CMDB_TABLE_NAME);
    grCI.addQuery('sys_id','d9623f791b429d5027f5964ead4bcb16');
    grCI.query();
	gs.addInfoMessage("got record" + grCI.name);
    while (grCI.next()) {
        var tmpArrayIP = [];
        var tmpArrayMAC = [];
        var tmpUpdateFlag = false;
        var grIP = new GlideRecord(RELATED_LIST_TABLE_NAME);
        grIP.addQuery('cmdb_ci', 'grCI.getUniqueValue()');
        grIP.query();
        while (grIP.next()) {
            tmpArrayIP.push(grIP.getValue(IP_IP_ADDRESS_FIELD_NAME));
            tmpArrayMAC.push(grIP.getValue(MAC_MAC_ADDRESS_FIELD_NAME));
        }
        if (tmpArrayIP.length > 0) {
            grCI.setValue(CI_IP_FIELD_NAME, tmpArrayIP.toString());
            tmpUpdateFlag = true;
        }
        if (tmpArrayMAC.length > 0) {
            grCI.setValue(CI_MAC_FIELD_NAME, tmpArrayMAC.toString());
            tmpUpdateFlag = true;
        }
        if (tmpUpdateFlag) {
                grCI.update();
        }
    }
}

updateIPs();

 

geet_2-1685397979855.png

 

 

 

Isn't there are simpler way to do this job?


I used IP address table in above script and not the network adaptor table.