Use of RegEx in Network Scanner

Started by rmpf2

rmpf2

Use of RegEx in Network Scanner   10 February 2024, 05:20

1) I'm testing some regular expressions, but the results are unexpected. Can you give me a hand and tell me what I'm doing wrong? Specially with the Replacement Expression for Columns Display Options.

Example:

Regex for MAC Address:
([A-Za-z0-9]{2})([A-Za-z0-9]{2}).([A-Za-z0-9]{2})([A-Za-z0-9]{2}).([A-Za-z0-9]{2})([A-Za-z0-9]{2})

Regex Replacement:
\1-\2-\3-\4-\5-\6

To change MAC Address format from f8b1.56e0.0e07 to f8-b1-56-e0-0e-07.


2) I'm trying to adapt the Packet Loss Script (Ping Example) to check for computers license status, specially to find out if they are activated but I'm lost with the following error message: Invalid procedure call or argument.

Script:
'Create process object and execute
Set proc = Internal.CreateObject("process")
proc.execute("cscript.exe " & "//nologo " & "c:\windows\system32\slmgr.vbs " & Input.Column("Host name") & " -xpr")

'Check cscript exit code
If proc.ExitCode = 0 Then

    'Setup a regex object
  Set objRegex = New RegExp
  objRegex.IgnoreCase = True
  objRegex.Multiline = True
  objRegex.Global = True
  'Regex to look for a specific string
  objRegex.Pattern = "activated"

'Traverse output lines
  For Each line in proc.output
    Set matches = objRegex.Execute(line)
      
           'Print regex match
    For Each match in matches
         Output.Write match.SubMatches(0)
    Next
  Next
Else
  Output.Write "Exit code " & proc.ExitCode
End If

Thanks!!
SoftPerfect Support forum - Andrew avatar image

Re: Use of RegEx in Network Scanner   10 February 2024, 16:18

1) Network Scanner uses a slightly different syntax for backreferences. Like JavaScript, Python and Perl, it uses $1 $2 $3 instead of \1 \2 \3.

Simply replace
\1-\2-\3-\4-\5-\6
with the following, and it should work as expected:
$1-$2-$3-$4-$5-$6

2) Regarding the script, try the following modifications. It uses a capture group to collect any associated information and trims the output:
'Create process object and execute
Set proc = Internal.CreateObject("process")
proc.execute("cscript.exe " & "//nologo " & "c:\windows\system32\slmgr.vbs " & Input.Column("Host name") & " -xpr")
'Check cscript exit code
If proc.ExitCode = 0 Then
    
    'Setup a regex object
    Set objRegex = New RegExp
    objRegex.IgnoreCase = True
    objRegex.Multiline = True
    objRegex.Global = True
    'Regex to look for a specific string
    objRegex.Pattern = "(.+activated.+)"
    
    'Traverse output lines
    For Each line In proc.output
        Set matches = objRegex.Execute(line)
        'Print regex match
        For Each match In matches
            Output.Write Trim(match.SubMatches(0))
        Next
    Next
Else
    Output.Write "Exit code " & proc.ExitCode
End If
Here on our machine it prints:
The machine is permanently activated.
rmpf2

Re: Use of RegEx in Network Scanner   14 February 2024, 00:09

Hi Andrew.

Thanks for all your help.

Reply to this topic

Sometimes you can find a solution faster if you try the forum search, have a look at the knowledge base, or check the software user manual to see if your question has already been answered.

Our forum rules are simple:

  • Be polite.
  • Do not spam.
  • Write in English. If possible, check your spelling and grammar.

Author:

Email:

Subject

A brief and informative title for your message, approximately 4–8 words:

     

Spam prevention: please enter the following code in the input field below.

 **     **  **     **   ******   **     **  ******** 
 **     **  **     **  **    **   **   **      **    
 **     **  **     **  **          ** **       **    
 **     **  **     **  **           ***        **    
  **   **   **     **  **          ** **       **    
   ** **    **     **  **    **   **   **      **    
    ***      *******    ******   **     **     **    

Message: