CMDB- Need help in splitting name to get MAC address
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-08-2024 10:48 PM - edited ‎04-09-2024 01:48 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-11-2024 08:27 PM
Hi Prashanth,
We can achieve this by the below user defined function.
function convertToMAC(input) {
// Check if the input is already in the desired MAC address format
if (/^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$/.test(input)) {
return input; // Return input as it is already in the desired format
}
// Remove anything after the first dot to remove the domain
var trimmedInput = input.split('.')[0];
// Take the last 12 characters after trimming the domain
var last12Chars = trimmedInput.substring(trimmedInput.length - 12);
// Split the last 12 characters into pairs of two and join with a colon
var macAddress = last12Chars.match(/.{1,2}/g).join(':');
return macAddress;
}
// Test cases
gs.print(convertToMAC('abc000832c61d1c.domain.net')); // Output: 00:08:32:c6:1d:1c
gs.print(convertToMAC('123456789012.hello.co.in')); // Output: 12:34:56:78:90:12
gs.print(convertToMAC('00:08:32:c6:1d:1c')); // Output: 00:08:32:c6:1d:1c (unchanged)
If my answer has helped with your question, please mark my answer as accepted solution and give a thumbs up.
Best regards,
Tharun Kumar