{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":1049,"path":"api-reference/server-api-reference/c_IPAddressFixupAPI.md","title":"IPAddressFixup - Global","bundle":null,"doc_type":null,"release":"australia","body":"\n# IPAddressFixup- Global\n\nThe IPAddressFixup script include provides methods that ensure that no other device has the same IP address, after a device has been successfully discovered. If any duplicates are found, the IP address field is cleared.\n\nUse with any server-side Discovery script to validate IP addresses.\n\n**Parent Topic:**[Server API reference](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/api-server.md)\n\n## IPAddressFixup - dedupe\\(String tableName, String ip\\)\n\nRemoves duplicates of the specified IP address in the specified table.\n\nAfter a device has been successfully discovered, you can use the dedupe\\(\\) method to remove the duplicate IP addresses for that device.\n\n|Name|Type|Description|\n|----|----|-----------|\n|tableName|String|Table to check for duplicates.|\n|ip|String|IP address to check for.|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\nThe following example shows how to use dedupe\\(\\) in a background script to remove duplicate IP addresses for the specified IP address within the cmdb\\_ci\\_hardware table.\n\n```\nvar grDiscovery=new GlideRecord(\"discovery_device_history\");\ngrDiscovery.addQuery('sys_id','<sys id of the record>'); // sys_id of the device discovery_device_history\ngrDiscovery.query();\nif(grDiscovery.next())\n  {\n    var fx=new IPAddressFixup(grDiscovery.cmdb_ci); // Instantiate IPAddressFixup - pass cmdb reference \n    fx.dedupe('cmdb_ci_hardware','192.161.1.1'); // Pass the IP address and table which removes duplicates of the specified IP address in the specified table.\n  }\n```\n\nThe following example shows how to use dedupe\\(\\) in a business rule to remove duplicate IP addresses for the specified IP address within the cmdb\\_ci\\_hardware table.\n\n```\n(function executeRule(current, previous /*null when async*/) {\n\nvar cmdb_ci = current.cmdb_ci + '';\nif (cmdb_ci) \n  {\n    var ipf = new IPAddressFixup(cmdb_ci); // Instantiate IPAddressFixup - pass cmdb reference\n    ipf.dedupe('cmdb_ci_hardware','168.128.1.1'); // Removes duplicates of the specified IP address in the specified table.\n  }\n})(current, previous);\n```\n\n## IPAddressFixup - dedupeAll\\(\\)\n\nRemoves all duplicate IP addresses from the tables.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|void| |\n\nThe following example shows how to use dedupeAll\\(\\) in a background script to remove all duplicate IP addresses.\n\n```\nvar grDiscovery=new GlideRecord(\"discovery_device_history\"); \ngrDiscovery.addQuery('sys_id','<sys id of the record>'); // sys_id of the device discovery_device_history\ngrDiscovery.query();\nif(grDiscovery.next())\n{\n  var fx=new IPAddressFixup(grDiscovery.cmdb_ci); // Call script include IPAddressFixup and pass cmdb reference\n  fx.dedupeAll(); //Removes all duplicate IP addresses from the tables.\n}\n```\n\nThe following example shows how to use dedupeAll\\(\\) in a business rule to remove all duplicate IP addresses.\n\n```\n(function executeRule(current, previous /*null when async*/) {\n\nvar cmdb_ci = current.cmdb_ci + '';\nif (cmdb_ci) \n{\n  var ipf = new IPAddressFixup(cmdb_ci); //call script include IPAddressFixup and pass cmdb reference\n  ipf.dedupeAll(); //Removes all duplicate IP addresses from the tables.\n})(current, previous);\n```\n\n## IPAddressFixup - fix\\(\\)\n\nRemoves all duplicate IP addresses and ensures that the parent ip\\_address record is set to one of the network interface card's \\(NIC\\) IP addresses.\n\nAfter a device has been successfully discovered, you can use the dedupe\\(\\) method to remove the duplicate IP addresses for that device and set the parent ip\\_address record value.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|void| |\n\nThe following example shows how to use fix\\(\\) in a background script to remove duplicate IP addresses and set the parent IP address.\n\n```\nvar grDiscovery=new GlideRecord(\"discovery_device_history\"); \ngrDiscovery.addQuery('sys_id','<sys id of the the record>'); // sys_id of the device discovery_device_history\ngrDiscovery.query();\nif(grDiscovery.next())\n  {\n    var fx=new IPAddressFixup(grDiscovery.cmdb_ci); // Instantiate IPAddressFixup - pass cmdb reference\n    fx.fix(); // Remove all duplicate IP addresses and ensure that the parent ip_address record is set to one of the NIC's IP addresses.\n  }\n```\n\nThe following example shows how to use fix\\(\\) in a business rule to remove duplicate IP addresses and set the parent IP address.\n\n```\n(function executeRule(current, previous /*null when async*/) {\n\nvar cmdb_ci = current.cmdb_ci + '';\nif (cmdb_ci) \n  {\n    var ipf = new IPAddressFixup(cmdb_ci); // Instantiate IPAddressFixup - pass cmdb reference\n    ipf.fix(); /// Remove all duplicate IP addresses and ensure that the parent ip_address record is set to one of the NIC's IP addresses.\n  }\n})(current, previous);\n```\n\n## IPAddressFixup - getParentIP\\(\\)\n\nReturns the parent IP address for the current device.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|String|Parent IP address|\n\nThe following example shows how to use getParentIP\\(\\) in a background script to obtain the parent IP address.\n\n```\nvar grDiscovery=new GlideRecord(\"discovery_device_history\"); // current.cmdb_ci can be used in case of Business rule on discovery_device_history table\ngrDiscovery.addQuery('sys_id','<sys id of the the record>'); // sys_id of the device discovery_device_history\ngrDiscovery.query();\nif(grDiscovery.next())\n  {\n    var fx=new IPAddressFixup(grDiscovery.cmdb_ci); // Instantiate IPAddressFixup - pass cmdb reference\n    gs.info(fx.getParentIP()); // Display the parent IP address of the device \n  }\n```\n\nOutput:\n\n```\n172.21.12.11\n```\n\nThe following example shows how to use getParentIP\\(\\) in a business rule to obtain the parent IP address.\n\n```\n((function executeRule(current, previous /*null when async*/) {\n\nvar cmdb_ci = current.cmdb_ci + '';\nif (cmdb_ci) \n  {\n    var ipf = new IPAddressFixup(cmdb_ci); // Instantiate IPAddressFixup - pass cmdb reference\n    gs.log(ipf.getParentIP()); // Log the parent IP address of the device. Located in the system logs table \n  }\n})(current, previous);\n```\n\n## IPAddressFixup - setParentIP\\(String ip\\)\n\nSets the parent IP address field for the current CI.\n\n|Name|Type|Description|\n|----|----|-----------|\n|ip|String|Parent IP address for the current CI.|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\nThe following example shows how to use setParentIP\\(\\) in a background script to store the parent IP address.\n\n```\nvar grDiscover=new GlideRecord(\"discovery_device_history\"); \ngrDiscover.addQuery('sys_id','a14e43b31bb4051070cb96c6b04bcb23'); // sys_id of the device in discovery_device_history table. Put your own sys_id here.\ngrDiscover.query();\nif(grDiscover.next())\n  {\n    var fx=new IPAddressFixup(grDiscover.cmdb_ci); // Instantiate IPAddressFixup - pass cmdb reference.\n    fx.setParentIP('197.111.1.1'); // Pass IP address to set as new IP on current mentioned CI.\n  }\n```\n\nThe following example shows how to use setParentIP\\(\\) in a business rule to store the parent IP address.\n\n```\n((function executeRule(current, previous /*null when async*/) {\n\nvar cmdb_ci = current.cmdb_ci + '';\nif (cmdb_ci) \n  {\n    var ipf = new IPAddressFixup(cmdb_ci); // Instantiate IPAddressFixup - pass cmdb reference\n    fx.setParentIP('195.11.1.1'); // Set the new IP address of the CI \n  }\n})(current, previous);\n```\n\n## IPAddressFixup - syncIP\\(\\)\n\nEnsures that the parent ip\\_address record is set to one of the NIC's IP addresses, or leaves it alone if there were no NICs.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|void| |\n\nThis shows an example of using the syncIP\\(\\) method in a background script.\n\n```\nvar grDevHistory=new GlideRecord(\"discovery_device_history\");\ngrDevHistory.addQuery('sys_id','a14e43b31bb4051070cb96c6b04bcb23'); // Sys_id of the device in discovery_device_history table. Put your own sys_id here.\ngrDevHistory.query();\nif(grDevHistory.next())\n  {\n    var fx=new IPAddressFixup(grDevHistory.cmdb_ci); // Call the script include IPAddressFixup and pass cmdb reference\n    fx.syncIP(); // Ensures that the parent ip_address record is set to one of the NIC's IP addresses, or leaves it alone if there were no NICs.\n  }\n```\n\nThis shows an example of using the syncIP\\(\\) method in a business rule.\n\n```\n(function executeRule(current, previous /*null when async*/) {\n\nvar cmdb_ci = current.cmdb_ci + '';\nif (cmdb_ci) \n  {\n    var ipf = new IPAddressFixup(cmdb_ci); // Call script include IPAddressFixup and pass cmdb reference\n    ipf.syncIP(); // Ensures that the parent ip_address record is set to one of the NIC's IP addresses, or leaves it alone if there were no NICs.\n  }\n})(current, previous);\n\n```\n\n"}}