- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-09-2024 03:45 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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2024 08:34 AM
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();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-09-2024 05:46 AM
I'd suggest using your favorite parsing methods and do the following, using your example of abc000832c61d1c.domain.net
- Find the position of the first '.' in the string (16th character in the example)
- Grab the previous 12 characters. (000832c61d1c)
- From the 12 characters
- Grab each set of 2 characters ('00', '08', '32', 'c6', '1d', '1c')
- Concatenate each set of 2 characters, add a ':' in between each (00:08:32:c6:1d:1c)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-09-2024 05:54 AM - edited 04-09-2024 06:03 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-09-2024 06:20 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-09-2024 09:44 AM
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)