Johannes
Johannes

Reputation: 55

VBS cut first line of String

I have the following Visual Basics code snippet:

Option Explicit
Dim objshell, strCmd, objExec, strLine, strIP

set objShell = CreateObject("Wscript.Shell")

strCmd = "%comspec% /c nslookup LT1130"
Set objExec = objShell.Exec(strCmd)
Do Until objExec.StdOut.AtEndOfStream
    strLine = objExec.StdOut.ReadLine()
    If (Left(strLine, 8) = "Address:") Then
        strIP = Trim(Mid(strLine, 11))
        Wscript.Echo strIP
    End If
Loop

Which outputs 2 Lines. The first line beeing the IP Address of my DNS Server and the second line beeing the resolved IP Address.

How can i cut the first line containing the IP Address of my DNS Server and only output the second line containing the resolved IP Address?

(If possible without using a temporary text file)

Upvotes: 0

Views: 168

Answers (1)

Hackoo
Hackoo

Reputation: 18827

Just tested this modified code on my side and it works 5/5


Option Explicit
Dim objshell, strCmd, objExec, strLine, strIP ,CountLine,MyArrayLine
set objShell = CreateObject("Wscript.Shell")
CountLine = 0
strCmd = "%comspec% /c nslookup myip.opendns.com. resolver1.opendns.com |find ""Address:"""
Set objExec = objShell.Exec(strCmd)
Do Until objExec.StdOut.AtEndOfStream
    strLine = objExec.StdOut.ReadLine()
    CountLine = CountLine + 1
    If Instr(strLine,":") > 0 Then
        MyArrayLine = Split(strLine,":")
        strIP = Trim(MyArrayLine(1))
        If CountLine = 2 Then
            Wscript.Echo "My External IP Address : " & strIP
        End If
    End If
Loop

Upvotes: 2

Related Questions