CMDB- Need help in splitting name to get MAC address

PrashanthS
Tera Expert

Hi, for a certain class of devices in our Org the data has been updated manually such that the Name field contains MAC address for the device. A sample name value is abc000832c61d1c.domain.net where the 12 digit MAC address is between abc and .domain.net. I need help with a fix script to get the MAC address field populated from this value in the required format like 00:08:32:c6:1d:1c. 

Please suggest if you have come across this scenario. Thanks in advance.

1 ACCEPTED SOLUTION

Hi Brahmjeet,

 

So I figured out the mistake, had to make use of slice:

 

var extractedString = inputString.slice(3).replace('.domain.net', '');

 

and then added the usuals

 

var newMac = extractedString.replace(/(.{2})/g,"$1:").slice(0,-1);
//gs.print(newMac);
}
macad.mac_address = newMac;
gs.info(newMac);
macad.update();
}

View solution in original post

7 REPLIES 7

SteveMacWWT
Kilo Sage

I'd suggest using your favorite parsing methods and do the following, using your example of abc000832c61d1c.domain.net

  1. Find the position of the first '.' in the string (16th character in the example)
  2. Grab the previous 12 characters. (000832c61d1c)
  3. From the 12 characters
    1. Grab each set of 2 characters ('00', '08', '32', 'c6', '1d', '1c')
    2. Concatenate each set of 2 characters, add a ':' in between each (00:08:32:c6:1d:1c)

BrahmjeetTanwar
Tera Guru

Hey, please use the below code to test once:
var
inputString = "abc000832c61d1c.domain.net";
var regexPattern = /abc(.*?)\.domain\.net/;
var extractedString = inputString.match(regexPattern)[1];
gs.print(extractedString);

 

Please let me know if you need any further assistance.
Thanks,
Brahm

Hi Brahmjeet,

 

Thanks for the response ,I'm getting this alphanumeric output using the script: 000832c61d1c.

Now the next step remains to concatenate it with : for MAC format.

 

Thanks

Prashanth

Hi Prashanth,

Please use the following code to convert it into MAC format after getting the string:

function hexToMAC(hexString) {
let macAddress = '';
for (let i = 0; i < hexString.length; i += 2) {
macAddress += hexString.substr(i, 2);
if (i < hexString.length - 2) macAddress += ':';
}
return macAddress;
}

var hexString = '000832c61d1c';
var macAddress = hexToMAC(hexString);
gs.print(macAddress)