0

I am creating snmp session everything is working as expected , i will be getting dynamic value of host to create session, I just want to put if condition to check Ip address is correct or not , is there way to validate IP address in javascript ?

main.js

var host = "135.01.01.01";
var sessionOptions = {
    port: 161,
    retries: 1,
    timeout: 5000,
    transport: "udp4",
    trapPort: msg.event.body.trapPort,
    version: snmpVersion
};
//Create snmp Session
var session = snmp.createSession(host, "public", sessionOptions);

try {
    if (!host) {
        console.log("Could not find host");
    } else {
        session.trap(trapOid, varbinds, options, function(error) {
            if (error)
                console.log(error);
            else
                console.log('SNMP successfully delivered');
        });
    }
} catch (e) {
    console.log("SNMP processing error: " + e);
}
hussain
  • 6,587
  • 18
  • 79
  • 152

1 Answers1

1

Adapted from here:

function ValidateIPaddress(ipaddress)   
{  
 if (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(host))  
  {  
    return (true)  
  }  
return (false)  
}  
dr_
  • 2,400
  • 1
  • 25
  • 39