2016-06-20 6 views
0

Ich erstelle snmp Sitzung alles funktioniert wie erwartet, ich werde dynamische Wert von host zu Sitzung erstellen, ich möchte nur wenn Bedingung, Ip-Adresse zu überprüfen ist richtig oder nicht, gibt es Weg, IP-Adresse in Javascript zu validieren ?Wie überprüfe ich, ob die IP-Adresse korrekt ist?

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); 
} 
+2

Was meinst du mit "richtig"? Wie in, ist es richtig formatiert? –

+2

http://stackoverflow.com/questions/10006459/regular-expression-for-ip-address-validation –

Antwort

1

von here Angepasst:

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) 
} 
+0

Thansk, half es, das Problem zu lösen. – hussain