Skip to content

Instantly share code, notes, and snippets.

@hirman74
Last active September 13, 2023 04:39
Show Gist options
  • Save hirman74/1f820e4f0044f72d87eb360ebe0bb353 to your computer and use it in GitHub Desktop.
Save hirman74/1f820e4f0044f72d87eb360ebe0bb353 to your computer and use it in GitHub Desktop.
Per Minute SNMPwalk
var IPHosts = ["192.168.13.128"];
var cmdSNMP = '.\\bin\\snmpwalk.exe -v 2c -c public -r 2 -t 1 -Ovqs -M .\\UPSonlyMIBS\\ ';
var SNMPBranch = ['.1.3.6.1.2.1.1.3'];
//run cscript snmpPoller_007.js
function returnMinutesDate () {
var today = new Date();
return (today.getFullYear() + "" + (today.getMonth()+1) + "" + today.getDate() + "" + today.getHours() + "" + today.getMinutes())
}
function pwSHping(myHostIP){
// Get-CimInstance -ClassName Win32_PingStatus -Filter "Address='$ComputerName' and timeout=$TimeoutMillisec" | Select-Object -Property Address, StatusCode
var objShell = new ActiveXObject("WScript.shell");
var ping=false;
var pingStatus = objShell.Exec('powershell.exe -Command Test-Connection -ComputerName ' + myHostIP + ' -Count 3 -Quiet').StdOut.ReadAll();
if (pingStatus.indexOf("True") != -1) {
ping=true;
}
return ping
}
function writeToFile (fileName, perLine) {
var objFSO = new ActiveXObject("Scripting.FileSystemObject"),
ForWriting = 2, ForReading = 1,
ForAppending = 8, CreateIt = true,
dontWantCreateIt = false, AsciiMode = 0,
UnicodeMode = -1, systemDefaultMode = -2;
var objFile = objFSO.OpenTextFile(".\\testResult\\" + fileName + ".csv", ForAppending, CreateIt, UnicodeMode);
objFile.WriteLine('Host\tSNMPBranch\tSNMPResult');
for (var i = 0; i < perLine.length; i++) {
objFile.WriteLine(perLine[i]);
};
objFile.Close();
}
function runEach(IPHosts, SNMPBranch) {
var objShell = new ActiveXObject("WScript.shell"), shellapp = new ActiveXObject("Shell.Application");
var perLine = []; var strResult = "";
var startTime = returnMinutesDate ();
for (var i = 0; i < IPHosts.length; i++) {
if (pwSHping(IPHosts[i])) {
for (var j = 0; j < SNMPBranch.length; j++) {
var host = IPHosts[i];
var branch = SNMPBranch[j];
var execOut = objShell.Exec( '%comspec% /S /C ' + cmdSNMP + host + " " + branch + " 2>&1" );
var results = execOut.StdOut.ReadAll();
var listResults = results.split('\r\n');
for (var k = 0; k < listResults.length; k++) {
if (listResults[k].indexOf("Cannot find module") == -1 && listResults[k].length > 0) {
strResult = host + "\t" + branch + "\t" + listResults[k];
perLine.push (strResult);
}
// WScript.Echo (execOut.Status);
// WScript.Echo (execOut.ProcessID);
// WScript.Echo (execOut.ExitCode );
}
}
} else {
WScript.echo("Host " + IPHosts[i] + " is not reachable");
}
}
var endTime = returnMinutesDate ();
writeToFile (startTime, perLine);
return [startTime, endTime];
}
function startCollecting(IPHosts, SNMPBranch) {
var getTime = runEach(IPHosts, SNMPBranch);
var startTime = getTime[0];
while (true) {
do {
endTime = returnMinutesDate ();
} while (endTime == startTime);
if (endTime != startTime) {
getTime = runEach(IPHosts, SNMPBranch);
startTime = getTime[0];
endTime = getTime[1];
}
}
}
startCollecting(IPHosts, SNMPBranch);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment