Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

288
Visualizações
How can use javascript to find the second occurance of an item in a Windows log file entry?

I am using Zabbix to parse Windows event logs. Here is an example:

4624    
An account was successfully logged on.
Subject:
    Security ID:        NT AUTHORITY\SYSTEM
    Account Name:       SERVER$
    Account Domain:     COMPANY
    Logon ID:       0x3E7

Logon Information:
    Logon Type:     7
    Restricted Admin Mode:  -
    Virtual Account:        No
    Elevated Token:     No

New Logon:
    Security ID:        COMPANY\Susan
    Account Name:       SUSAN
    Account Domain:     COMPANY
    Logon ID:       0x3ED0915C
    Linked Logon ID:        0x0
    Network Account Name:   -
    Network Account Domain: -
    Logon GUID:     {7bac704d-8521-0b5e-4548-5c61a3614dc0

And here is the javascript I am using to pull the data I want:

var lines = value.split("\n");
var accountName = "";
var loginType = "";
var sourceIp = "";
lines.forEach(function(line) {
  if (line.trim().substring(0, 11) === "Logon Type:") {
    loginType = line.substring(12).trim();
  } 
  if (line.trim().substring(0, 13) === "Account Name:") {
    accountName = line.substring(14).trim();
  } 
  if (line.trim().substring(0, 23) === "Source Network Address:") {
    sourceIp = line.substring(24).trim();
  }
});
return  loginType + " " + accountName + " " + sourceIp;

When this is ran against the log data, it will grab the first occurrence of Account Name. I need it to grab the second one as that is where the user's name is.

How can I modify what I am doing to grab this second one rather than the first one?

Thank you.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you only need to solve that specific problem, just track whether you have already found an Account Name and skip the first one:

var lines = value.split("\n");
var accountName = "";
var loginType = "";
var sourceIp = "";
// store the count of how many you have found while iterating
let accountsFound = 0;
lines.forEach(function(line) {
  if (line.trim().substring(0, 11) === "Logon Type:") {
    loginType = line.substring(12).trim();
  } 
  if (line.trim().substring(0, 13) === "Account Name:") {
   if (accountsFound === 1) {
    accountName = line.substring(14).trim();
   }
   // keep track of how many you have found so far
   accountsFound++;
  } 
  if (line.trim().substring(0, 23) === "Source Network Address:") {
    sourceIp = line.substring(24).trim();
  }
});
return  loginType + " " + accountName + " " + sourceIp;

A better approach though would be to avoid substringing things and just split the strings into the key and value once as shown here:

const content = `4624    
An account was successfully logged on.
Subject:
    Security ID:        NT AUTHORITY\SYSTEM
    Account Name:       SERVER$
    Account Domain:     COMPANY
    Logon ID:       0x3E7

Logon Information:
    Logon Type:     7
    Restricted Admin Mode:  -
    Virtual Account:        No
    Elevated Token:     No

New Logon:
    Security ID:        COMPANY\Susan
    Account Name:       SUSAN
    Account Domain:     COMPANY
    Logon ID:       0x3ED0915C
    Linked Logon ID:        0x0
    Network Account Name:   -
    Network Account Domain: -
    Logon GUID:     {7bac704d-8521-0b5e-4548-5c61a3614dc0`;
function getDetails(value) {
  var lines = value.split("\n");
  var accountName = "- not found -";
  var loginType = "- not found -";
  var sourceIp = "- not found -";
  // store the count of how many you have found while iterating
  let accountsFound = 0;
  lines.forEach(function(line) {
    // just split each line on the : and trim each part to avoid all the substrings
    let key, value;
    [key, value] = line.trim().split(':');
    if (key !== undefined) {
        key = key.trim();
    }
    
    if (value !== undefined) {
      value = value.trim();
    }
    // console.log(key, value);
    if (key === "Logon Type") {
      loginType = value;
    } 
    if (key === "Account Name") {
     if (accountsFound === 1) {
      accountName = value;
     }
     // keep track of how many you have found so far
     accountsFound++;
    } 
    if (key === "Source Network Address") {
      sourceIp = value;
    }
  });
  return  loginType + " " + accountName + " " + sourceIp;
}

console.log(getDetails(content));

See here: https://jsfiddle.net/mcgraphix/nrwau8ex/13/

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda