1

I cannot get StrComp to work correctly and I'm not sure why. Can anyone see what I might be doing wrong here? (all ip numbers have been changed)

Option Explicit
Dim http : Set http = CreateObject( "MSXML2.ServerXmlHttp" )
Dim externalip

http.Open "GET", "http://icanhazip.com", False
http.Send
externalip = http.responseText

wscript.Echo externalip
wscript.Echo StrComp(externalip, "71.215.176.202")

if StrComp(externalip, "71.215.176.202") = 0 Then
  wscript.Echo "Connected to Comcast"
else
  wscript.echo "Connected to Office"
end if

Set http = Nothing

This will never return 0, even when externalip is correct. The strange thing is that there seems to be an extra CR/LF in the externalip. I've tried using the Trim function but that doesn't seem to help. Here's an example of the output while connected to the office:

C:\BATFiles>cscript ip.vbs
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.

107.62.166.159

-1
Connected to Office

And here is the output when connected to my local ISP (Comcast)

C:\BATFiles>cscript ip.vbs
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.

71.215.176.202

1
Connected to Office

I know from the output that I could just check if strcomp returns > 0, but the office ip is subject to change depending on which location I'm connecting to but the IP on my local is pretty much static. And I'm not quite sure how -1 and 1 are decided upon when doing a string comparison. Is it doing a length comparison?

1 Answers1

1

http://icanhazip.com is returning the IP with a line feed character at the end (noticee in your examples, there's a line between the IP address and -1 or 1).

Use this instead:

externalip = replace(http.responseText, chr(10), "")

This will replace the line feed with nothing, and it should compare correctly.

To answer your question, -1 0 and 1 are based on how the strings sort. EG, 0 comes before 1, so you'd get -1. T comes after A, so you'd get 1. T = T, so you'd get 0

Jonno
  • 21,217
  • 4
  • 64
  • 71
  • Perfect! Thank you! I knew it had something to do with the blank line, just wasn't sure what the problem was. And thanks for the explanation on how StrCompare works. – user2021539 Dec 24 '15 at 14:03