Skip to content

Instantly share code, notes, and snippets.

@jlrjr
Last active April 12, 2016 14:02
Show Gist options
  • Save jlrjr/590844764e3ed3068f94447ab0b0fa4b to your computer and use it in GitHub Desktop.
Save jlrjr/590844764e3ed3068f94447ab0b0fa4b to your computer and use it in GitHub Desktop.
Create ServiceNow configuration items from security vulnerability data. Use for populating test data, not for production use.
//create sample CIs based on vul items
//createVulCIs();
linkVulsToCIs();
function createVulCIs() {
var item = new GlideAggregate("sn_vul_vulnerable_item");
item.activeQuery();
item.addNotNullQuery("dns");
item.groupBy("dns");
item.groupBy("ip_address");
item.query();
while (item.next()) {
setupCI(item.dns, item.ip_address);
}
}
function setupCI(dnsName, ip) {
var prefix = dnsName.substring(0, 3);
var table = "cmdb_ci_server"; //default
if (prefix == "lin" || prefix == "man" || prefix == "cen" || prefix == "deb" || prefix == "fed" || prefix == "rhe" || prefix == "ubu") {
table = "cmdb_ci_linux_server";
} else if (prefix == "des") {
table = "cmdb_ci_computer";
} else if (prefix == "esx") {
table = "cmdb_ci_esx_server";
} else if (prefix == "jbo") {
table = "cmdb_ci_app_server_jboss";
} else if (prefix == "ora") {
table = "cmdb_ci_database";
} else if (prefix == "win") {
table = "cmdb_ci_win_server";
} else if (prefix == "sol") {
table = "cmdb_ci_solaris_server";
}
createCI(table, dnsName, ip);
}
function createCI(table, dnsName, ip) {
var name = dnsName;
var dnsDomain = "";
var parts = dnsName.toString().match(/(.*?)\.(.*)/);
if (!gs.nil(parts)) {
var name = parts[1];
var dnsDomain = parts[2];
}
gs.info("Creating {0} . {1} : {2} as {3}", name, dnsDomain, ip, table);
var ci = new GlideRecord(table);
ci.setValue("name", name);
ci.setValue("ip_address", ip);
ci.setValue("dns_domain", dnsDomain);
ci.insert();
}
function linkVulsToCIs() {
var vul = new GlideRecord("sn_vul_vulnerable_item");
//vul.setLimit(100);
vul.addNullQuery("cmdb_ci");
vul.addNotNullQuery("ip_address");
vul.query();
while (vul.next()) {
vul.cmdb_ci = getCIFromIP(vul.getValue("ip_address"));
vul.setWorkflow(false)
vul.update();
gs.info("Updated {0} with CI:{1}", vul.getDisplayValue(), vul.getDisplayValue("cmdb_ci"));
}
}
function getCIFromIP(ip) {
var ci = new GlideRecord("cmdb_ci");
//will only get first matchin item, but fine for testing
ci.get("ip_address", ip);
return ci.getUniqueValue();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment