Simple VBS script to check internet connection

I previously used a basic dos bat script which performed a ping to check if an internet connection is available or not. Now, I can no longer ping a server outside of our office network as it has been identified as a security threat. Now I would need a different manner to determine if an internet connection is available. I was thinking to do this with a small vbscript.
I will start the vbs script out of dos with the following command:
START /WAIT "CHECKNET.vbs"

I would need the vbs script to:
1) Check if smtp.google.com can be successfully reached
2) Send "1" if yes, and "0" if not, to C:\IResult.txt
3) Close vbs

I can then use that .txt file and check the value inside it with a dos bat script. I need the vbs script to be as simple as possible. It must just set a 0 or 1 in the txt file. Nothing is required to be displayed on screen. It would be good if a timeout can be set for how long it tries to find a connection.

Any help with this will be greatly appreciated.
Rebel_no_1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

stergiumCommented:
for the pinging
If My.Computer.Network.Ping("smtp.google.com") Then
WScript.Echo "1"
Else
WScript.Echo "0"
End If
0
Rebel_no_1Author Commented:
No idea how that works? Tried and it gives an error. And it,s definitely not putting a value in a text file as required.
0
josikaCommented:
Here you go.  smtp.google.com blocks echo requests so I changed it to google.com:

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oLogFile = oFSO.OpenTextFile("C:\IResult.txt", 2, True)
Set oShell = WScript.CreateObject("WScript.Shell")
strHost = "google.com"
strPingCommand = "ping -n 1 -w 300 " & strHost
ReturnCode = oShell.Run(strPingCommand, 0 , True)
If ReturnCode = 0 Then
	oLogFile.WriteLine "Successful ping."
Else
	oLogFile.WriteLine "Unsuccessful ping."
End If
oLogFile.Close

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Rebel_no_1Author Commented:
I changed your script a little. Will test tomorrow when the office is open again and let know. It seems that it works similar to my attached script which does not work but will double check tomorrow. Thanks for the help so far.


Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oLogFile = oFSO.OpenTextFile("C:\IResult.txt", 2, True)
Set oShell = WScript.CreateObject("WScript.Shell")
strHost = "www.google.com"
strPingCommand = "ping -n 1 -w 1000 " & strHost
ReturnCode = oShell.Run(strPingCommand, 0 , True)
If ReturnCode = 0 Then
      oLogFile.WriteLine "1"
Else
      oLogFile.WriteLine "0"
End If
oLogFile.Close

@Echo OFF
:START
CLS
ECHO PERFORMING INTERNET TEST NOW...
ECHO.

Ping -n 2 -w 1000 209.85.149.103> C:\temp.txt
FIND "Reply from 209.85.149.103" C:\temp.txt > C:\TEMPOUT.txt

IF %ERRORLEVEL% EQU 0 SET INETSTATUS=ALLFINE
IF %ERRORLEVEL% EQU 0 color 0A
IF "%INETSTATUS%" EQU "ALLFINE" ECHO INTERNET STATUS=ALLFINE
IF "%INETSTATUS%" EQU "ALLFINE" ECHO 1 > C:\IResult.txt

IF %ERRORLEVEL% EQU 1 SET INETSTATUS=NOTFINE
IF %ERRORLEVEL% EQU 1 color 0C
IF "%INETSTATUS%" EQU "NOTFINE" ECHO INTERNET STATUS=NOTFINE
IF "%INETSTATUS%" EQU "NOTFINE" ECHO 0 > C:\IResult.txt

Ping -n 2 -w 1000 LOCALHOST > C:\TEMPOUT.txt


GOTO :START

Open in new window

0
josikaCommented:
Mine works, I tested it on my machine.  Thanks :)
0
Rebel_no_1Author Commented:
I agree it does work, I also tested it on my machine. But, I'm on my home network now. I'm hoping the .vbs will work past the security at the office as they have blocked pinging outside servers.
0
josikaCommented:
Probably not then if it's firewalled.  It's still the same type of request that the firewall will see and if there is a block in place the same rule will be triggered.
0
Rebel_no_1Author Commented:
And I don't want to get fingered...   :-)

That's what I thought.

I know there must be other ways (apart from a ping) of checking your internet connection.
What about trying to telnet somewhere and analyzing an %errorlevel% or something?
0
Rebel_no_1Author Commented:
Shit, I read fingered instead of triggered. Not my day today... :-(
0
josikaCommented:
I'd almost guarantee that telnet is blocked if pinging is.  Telnet is plan text and is very insecure.  I think you'll like this one.  It does an HTTP load test and I'd bet HTTP is allowed.


On Error Resume Next
Set objHTTP = CreateObject("MSXML2.XMLHTTP") 
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oLogFile = oFSO.OpenTextFile("C:\IResult.txt", 2, True)
strURL = "http://www.google.com"
objHTTP.Open "GET", strURL, FALSE
objHTTP.Send
If objHTTP.statusText = "OK" Then
    oLogFile.WriteLine "1"
Else
	oLogFile.WriteLine "0"
End If
oLogFile.Close

Open in new window

0
Rebel_no_1Author Commented:
Keeps sending a 0 to the txt file regardless if I switch internet on or off?
0
Rebel_no_1Author Commented:
But I think you are on the right track!  :-)
0
josikaCommented:
You probably need MSXML 3.0 or later installed on your machine first.

http://www.microsoft.com/download/en/details.aspx?id=19662
0
Rebel_no_1Author Commented:
Ok, You are gonna hate me for it but I would really like to try something which does not require additional sw. This will makr it difficult as this program will have to be installed on 100's of computers.

If there is no other way, we can test with the download you suggested but I'm not sure if it will be a real solution to the requirement.
0
Rebel_no_1Author Commented:
Ok, I followed that link, installed the 5mb package, restarted, retested, but still it returns a zero if the internet is on or off to the txt file. If we can just get it to work correctly in online and offline scenarios...
0
josikaCommented:
Which version of .NET do you have installed?
0
Rebel_no_1Author Commented:
.net framework 4 client profile
.net framework 4 extended
And there is previous versions too...


Does it work on your pc? I'm on win 7 now if that helps. Script should be able to work on xp, 7, etc... right?
0
josikaCommented:
Remove the 'On Error Resume Next' from Line 1 so it throws an error we can see.  Then post the error here.
0
Rebel_no_1Author Commented:
Ok, did it...

scr.png
0
Rebel_no_1Author Commented:
The exact script...
Set objHTTP = CreateObject("MSXML2.XMLHTTP") 
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oLogFile = oFSO.OpenTextFile("C:\IResult.txt", 2, True)
strURL = "http://www.google.com"
objHTTP.Open "GET", strURL, FALSE
objHTTP.Send
If objHTTP.statusText = "OK" Then
    oLogFile.WriteLine "1"
Else
	oLogFile.WriteLine "0"
End If
oLogFile.Close

Open in new window

0
Rebel_no_1Author Commented:
There is a clean row missing at line 1.
With the above sample code the error would say Line 6
0
josikaCommented:
Try this:

Set objHTTP = CreateObject("Microsoft.XMLHTTP")
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oLogFile = oFSO.OpenTextFile("C:\IResult.txt", 2, True)
strURL = "http://www.google.com"
objHTTP.Open "GET", strURL, FALSE
objHTTP.Send
If objHTTP.statusText = "OK" Then
    oLogFile.WriteLine "1"
Else
    oLogFile.WriteLine "0"
End If
oLogFile.Close

Open in new window

0
Rebel_no_1Author Commented:
Same error. After removing the first line as you said an empty txt file is created. (no more zero)
0
Rebel_no_1Author Commented:
Can someone please help? I believe this to be a really simple request. I am sure that we are close to a solution.
0
josikaCommented:
It is a simple request, but not when your firewall is blocking ping requests.

What OS is this machine and what IE version?
0
josikaCommented:
I'm not sure if you need this installed as well or not:

http://support.microsoft.com/kb/317244
0
Rebel_no_1Author Commented:
It is windows 7. IE8 (I use firefox though)
There must be some way of achieving this without a ping. Surely we can somehow check the internet connection in some other way? Is the solution that you gave me working on your PC? I appreciate your help and effort!
0
josikaCommented:
I tried echoing the results of a telnet connection over port 80 and it'll return a 0 no matter what.  Still looking.
0
markdmacCommented:
Use IE to check inner HTML for a web site. Then visit a page you have created and read the value to verify you are getting out. This would only require port 80.
0
matrixnzCommented:
Here is an AutoIT Script, just download AutoIT copy the code into Scite, Save and Compile to an executable, could also be done with VB, I just prefer AutoIT.

Cheers
Local $URL_STRING = 'http://www.google.com'
Local $URL_FLAGS = 0x00000001
Local $URL_RESERVED = 0
Local $CHK_IRESULT = DllCall("WinInet.dll", "int", "InternetCheckConnection", "str", $URL_STRING, "uint", $URL_FLAGS, "uint", $URL_RESERVED)
Local $OPN_IRESULT = FileOpen('C:\IResult.txt', 2)
FileWrite($OPN_IRESULT, $CHK_IRESULT[0] & @CRLF)
FileClose($OPN_IRESULT)

Open in new window

0
RobSampsonCommented:
It could also be possible that www.google.com is blocking a response from a simple SEND request.  Try another site, maybe not one so "commercial".

Rob.
0
Daz_1234Commented:
Ok my turn :o)

Change the first line on the VBScript as I've done below and try that (I've also added message boxes in for testing).  This works for me on a Windows 7 computer with UAC enabled, and does not require elevated privileges.  You must obviously also double-check that the user has permissions to write to the output file.

Hope this helps,
Daz.

Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oLogFile = oFSO.OpenTextFile("C:\IResult.txt", 2, True)
strURL = "http://www.google.com"
objHTTP.Open "GET", strURL, FALSE
objHTTP.Send
If objHTTP.statusText = "OK" Then
    oLogFile.WriteLine "1"
    MsgBox "Internet is OK"
Else
    oLogFile.WriteLine "0"
    MsgBox "Internet not accessible"
End If
oLogFile.Close

Open in new window

0
Rebel_no_1Author Commented:
Great! Daz, you are finallly on the right track. Your script works, roughly...
1) Can we please remove the message boxes as this script will run silently and the value in IResult-txt is interpreted and actioned by another program.
2) When there is no connection, "Line 6 Char 1, The server name or address could not be resolved" error box is displayed. When the internet is not working we just need a zero in that txt file and the script must close. No msg box, no warnings, just a 0.

(I don't want to do this with auto-it @ Matrixnz but thanks anyway)

Great to see were getting there!
0
Daz_1234Commented:
Updated:
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oLogFile = oFSO.OpenTextFile("C:\IResult.txt", 2, True)
strURL = "http://www.google.com"
objHTTP.Open "GET", strURL, FALSE
On Error Resume Next
objHTTP.Send
On Error Goto 0
If objHTTP.statusText = "OK" Then
    oLogFile.WriteLine "1"
    'MsgBox "Internet is OK"
Else
    oLogFile.WriteLine "0"
    'MsgBox "Internet not accessible"
End If
oLogFile.Close

Open in new window

The message boxes have been commented out.  Just remove the apostrophe if you want them back on for testing.

Daz.
0
Daz_1234Commented:
Apologies, still problems if network disconnected.  More robust below:
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oLogFile = oFSO.OpenTextFile("C:\IResult.txt", 2, True)
strURL = "http://www.google.com"
objHTTP.Open "GET", strURL, FALSE
On Error Resume Next
objHTTP.Send
If Err.Number = 0 Then
    If objHTTP.statusText = "OK" Then
        oLogFile.WriteLine "1"
        'MsgBox "Internet is OK"
    Else
        oLogFile.WriteLine "0"
        'MsgBox "Internet not accessible"
    End If
Else
    oLogFile.WriteLine "0"
    'MsgBox "Internet not accessible"
End If
oLogFile.Close
On Error Goto 0

Open in new window


Daz.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rebel_no_1Author Commented:
It says this when the internet is off. "Line 9 Char 1  The data necessary to complete this operation is not yet available" And more importantly, there is no 0 in the txt file in such a scenario.
0
Daz_1234Commented:
Yep sorry about that.  The subsequent one is more robust.

Daz.
0
markdmacCommented:
Another way to do this as I mentioned above.  Using IE you can check for connectivity.  I have a web page that does nothing more than return my public IP same as what http://www.whatismyip.com does.

The code for that page is simple:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Your Public IP</title>
</head>
<body>
<%=Request.ServerVariables("REMOTE_ADDR")%>
</body>
</html>


I like this solution because it only relies on using port 80.
KnownExternalIP = "169.10.192.3"
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Navigate("http://www.thespidersparlor.com/ipaddress.asp")
Do While objIE.Busy
   WScript.Sleep 500
Loop
ExternalIP = objIE.Document.Body.InnerHTML


If InStr(ExternalIP,KnownExternalIP) > 0 Then
	MsgBox "Internet connection is up."
Else
	MsgBox "Internet connection is down." 
End If
objIE.Quit

Open in new window

0
Rebel_no_1Author Commented:
This script does exactly what was originally required. When run, it will simply put a "1" or a "0" in a text file to indicate if an active internet connection is available or not. Nothing is displayed onscreen. A dos script could be used to interpret the result and might be looped to continuously update you on your internet availability. This script was designed with the internet stability (or lack thereof) in the jungles of the DRC in mind.

Thank you very much for everybody's effort with this. This is a valuable and very re-usable solution.
0
Rebel_no_1Author Commented:
Sorry for taking so long to respond to the provided answers, I indeed find myself in the jungles of the DRC and the internet is a scarce and expensive commodity here. Thanks again!
0
josikaCommented:
That's the same code I posted a week ago with error checking added. :)
0
Daz_1234Commented:
==> josika: close, but critically, the object you were using did not work.

Daz.
0
markdmacCommented:
Daz, have you given mine a try?  I'll add the logging stuff if you like the solution.
0
josikaCommented:
I used that object already Daz.  He said it didn't work: post: 07/06/11 11:37 AM.
0
Rebel_no_1Author Commented:
Guys, I have been clear in my responses. I asked the question 100% clearly. I told you exactly what worked, and what didn't. I gave you feedback, information, screenshots and quotes. If anyone now want's to DEMAND points, go ahead so we can sort it out please.
0
Daz_1234Commented:
==> Rebel_no_1, I'm very sorry about this.


==> josika, this debate is not healthy, please read the posts carefully.  

You've posted using the following objects:

MSXML2.XMLHTTP
Microsoft.XMLHTTP

My version that worked uses this object:

MSXML2.ServerXMLHTTP

Now I don't pretend to understand the underlying dll calls that these objects access, but they are obviously different.

Daz.
0
josikaCommented:
Wasn't demanding points, just wanted to know what was different.  I looked on my phone so must have missed it.  My apologies and I'm glad it's working for you Rebel.
0
Rebel_no_1Author Commented:
Thanks Josika. Yeah I noticed that you were on EE mobile at that time. I really appreciate your effort. If I knew better I might have split the points but I didn't think that the situation and the answer really warranted it. If anyone would like me to do something more from my side please let me know? I hope we can put this behind us as I need to get back to work(and embed your script in a 20 000 line dos bat program). Thanks guys.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.