Nutshell
Nutshell

Reputation: 1

Compare current IP to expected IP

Win7

I'm a noob making a script that changes my proxy settings via registry, opens and closes Internet Explorer to force the proxy settings to take effect, checks my public IP to make sure the proxy settings worked, and then runs a program. I'm having a problem with the part that checks to see if the changes worked. Here is that part:

 function WAN_IP() 
     set http = createobject("Microsoft.XMLHTTP") 
     call http.open("get", "http://icanhazip.com/", false) 
     http.send() 
     IP = http.responsetext 
     set http = nothing 
     WAN_IP = trim(IP) 
 end function 

 myIP = WAN_IP() 

 theIP = "0.0.0.0" 

 IPCompare = StrComp(myIP, theIP, vbTextCompare)  


 if IPCompare = 0 then 
     Wscript.Echo "it worked!"  
 else 
     Wscript.Echo "something went wrong"  
 end if 

Note that in my actual script, theIP is set to the actual proxy IP I'm trying to check. I just changed it here for security. If you are testing this code, you'll have to change it to your public IP. Anyway, the problem is that theIP and myIP are coming up as the same exact string, but IPCompare is returning as 1, not 0 like I would expect. I tested each variable by doing the following in copies of the script:

 Wscript.Echo theIP 

returned the correct IP address

 Wscript.Echo myIP 

returned the same as theIP

 Wscript.Echo IPCompare 

returned 1 (which means string1 > string2) when I expected 0 (string1 = string2)

My only guess is that either theIP or myIP has a trailing or leading space or something that I can't see.

Upvotes: 0

Views: 484

Answers (2)

Hackoo
Hackoo

Reputation: 18847

Try something like this :

Option Explicit
Dim Title,myIP,theIP
Title = "Compare Two IP(s) Address"
myIP = WAN_IP() 
theIP = "0.0.0.0" 
'MsgBox myIP,VbInformation,Title
If IPCompare(myIP,theIP) = True Then
    MsgBox "It worked ! The two IP(s) are the same !" & VbCrLF & "IP = " & myIP,VbInformation,Title
else 
    Msgbox "Oupps, something went wrong. The two IP address dosen't match !" & VbCrLF &_
    "IP1 = " & myIP & " <> IP2 = " & theIP,VbCritical,Title 
end if 
'***************************************************
Function WAN_IP() 
    Dim http,IP
    set http = createobject("Microsoft.XMLHTTP") 
    call http.open("get", "http://icanhazip.com/", false) 
    http.send() 
    IP = http.responsetext 
    set http = nothing 
    WAN_IP = trim(IP) 
End Function 
'***************************************************
Function IPCompare(IP1,IP2)
    Dim aOctetIP1,sOctet11,sOctet12,sOctet13,sOctet14
    Dim aOctetIP2,sOctet21,sOctet22,sOctet23,sOctet24
    aOctetIP1 = Split(IP1,".")
    sOctet11 = CInt(aOctetIP1(0))
    sOctet12 = CInt(aOctetIP1(1))
    sOctet13 = CInt(aOctetIP1(2))
    sOctet14 = CInt(aOctetIP1(3))

    aOctetIP2 = Split(IP2,".")
    sOctet21 = CInt(aOctetIP2(0))
    sOctet22 = CInt(aOctetIP2(1))
    sOctet23 = CInt(aOctetIP2(2))
    sOctet24 = CInt(aOctetIP2(3))

    If sOctet11 = sOctet21 And sOctet12 = sOctet22 And sOctet13 = sOctet23 And sOctet14 = sOctet24 Then
        IPCompare = True 
    else 
        IPCompare = False  
    end if 
End Function
'************************************************************

Upvotes: 1

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38765

First add diagnostic output (cunningly designed to make invisibles visible):

myIP = WAN_IP()
WScript.Echo "-----[" & myIP & "]-------", Len(myIP)

You'll see something like:

cscript 25702895.vbs
-----[12.123.12.123
]------- 14
something went wrong

Then re-read the docs for Trim - removing spaces won't affect the trailing EOL. So:

'    WAN_IP = trim(IP) ' <-- removes only spaces
     WAN_IP = Left(IP, Len(IP) - 1) ' assume EOL = vbLf

Now it should 'work', maybe accidentially.

At last think about making this hack safer (checking the assumption, deleting non-digits from the end of IP, traversing IP as long as you find digits or ., using a RegExp, ...)

Upvotes: 0

Related Questions