Script Center > Scripting Forums > The Official Scripting Guys Forum! > Script to detect ip read against text file and display output
Ask a questionAsk a question
 

AnswerScript to detect ip read against text file and display output

  • Sunday, November 08, 2009 2:54 AMtaz081175 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I have setup 3 wscript echo in this VBS. the third one does not work at all. I have verified the text file reads correctly and is setup x.x.x carriage return x.x.x carrage return etc...

    Any help would be appreciated


    Set objNetwork = CreateObject("Wscript.Network")
    strComputer = "."


    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

    Set colAdapters = objWMIService.ExecQuery _
        ("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled=True")

    For Each objAdapter in colAdapters
        For Each strAddress in objAdapter.IPAddress
            arrOctets = Split(strAddress, ".")
            If arrOctets(0) <> "" Then
                strSubnet = arrOctets(0) & "." & arrOctets(1) & "." & arrOctets(2)
                x = 1
                Exit For
            End If
            If x = 1 Then
                Exit For
            End If
        Next
    Next

    'wscript.echo strSubnet
    Const ForReading = 1

    Set objRegEx = CreateObject("VBScript.RegExp")
    objRegEx.Pattern = "strSubnet"

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile("C:\ip.txt", ForReading)

    Do Until objFile.AtEndOfStream
        strSearchString = objFile.ReadLine
        Set colMatches = objRegEx.Execute(strSearchString) 
    'Wscript.Echo strSearchString
             If colMatches.Count = 1 Then
             For Each strMatch in colMatches
              Wscript.Echo strSearchString
            Next
        End If
    Loop

    objFile.Close

Answers

  • Sunday, November 08, 2009 7:56 PMTom Lavedas Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    This line is passing a literal string, rather than the contents of the variable ...

      objRegEx.Pattern = "strSubnet"

    Take the double quotes off ...

      objRegEx.Pattern = strSubnet

    The rest cannot be evaluated without knowledge of what is in the file, "C:\ip.txt".
    Tom Lavedas
    • Edited byTom Lavedas Sunday, November 08, 2009 7:56 PMfix trivial typo
    • Marked As Answer bytaz081175 Sunday, November 08, 2009 8:58 PM
    •  

All Replies

  • Sunday, November 08, 2009 7:56 PMTom Lavedas Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    This line is passing a literal string, rather than the contents of the variable ...

      objRegEx.Pattern = "strSubnet"

    Take the double quotes off ...

      objRegEx.Pattern = strSubnet

    The rest cannot be evaluated without knowledge of what is in the file, "C:\ip.txt".
    Tom Lavedas
    • Edited byTom Lavedas Sunday, November 08, 2009 7:56 PMfix trivial typo
    • Marked As Answer bytaz081175 Sunday, November 08, 2009 8:58 PM
    •  
  • Sunday, November 08, 2009 9:00 PMtaz081175 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Thank you. I fell better now. I thought it was the code I was using and banging my head against the wall because of it. Works perfectly now. i did modify another part of it to collect info for multiple network adapters and if the value of the first IP octet is o then in goed to the next network adpter that has an ip.

    Set objNetwork = CreateObject("Wscript.Network")
    strComputer = "."


    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

    Set colAdapters = objWMIService.ExecQuery _
        ("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled=True")

    For Each objAdapter in colAdapters
        For Each strAddress in objAdapter.IPAddress
            arrOctets = Split(strAddress, ".")
            If arrOctets(0) > "0" Then
                strSubnet = arrOctets(0) & "." & arrOctets(1) & "." & arrOctets(2)
               x = 1
                Exit For
           End If
            If x = 1 Then
                Exit For
            End If
        Next
    Next


    'wscript.echo strSubnet
    Const ForReading = 1

    Set objRegEx = CreateObject("VBScript.RegExp")
    objRegEx.Pattern = strSubnet

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile("C:\ip.txt", ForReading)

    Do Until objFile.AtEndOfStream
        strSearchString = objFile.ReadLine
        Set colMatches = objRegEx.Execute(strSearchString) 
    'Wscript.Echo strSearchString
    'Wscript.Echo colMatches.Count
             If colMatches.Count = 1 Then
             For Each strMatch in colMatches
              Wscript.Echo strSearchString
            Next
        End If
    Loop

    objFile.Close