Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have a Windows XP machine that I'm accessing remotely which has tendancy to get into a weird state with the network connection - when you're on the machine locally it shows as connected still but nothing gets through. Disabling and Enabling the network connection solves the problem. I guess this symptomatic of a more serious problem, but I can't get to the machine to sort it out until at least Monday, so I was hoping to either find some utility or put to together a quick script to test the network connection (i.e. telnet google.com 80 or something) and restart the network connection if possible or else reboot the machine. Googling on this suggests only really complicated lash-ups, and I don't want to lock myself out before I can get to it... Can anyone suggest something simple? Or even a way to automatically restart the connection every couple of hours?

share|improve this question

3 Answers 3

up vote 1 down vote accepted

If you're going to punt and reboot, at least test the connection first; so if you can't retrieve something over the internet, then reboot. We can quibble about you punting on 2nd down with 70 to go, but it's just silly to punt on every 2nd down.

EDIT: After further reflection and curiosity, here's the PowerShell way of doing this, with some bonus ideas. No need to mess with schedules, it automatically sleeps for three minutes, so just run it once on startup (powershell -File testandpunt.ps1). I've added a simple counter, so it doesn't reboot until three failed attempts. Obviously, change any of these values. Changed serverfault.com to google.com since the latter is probably faster and i'd rather you siphon their bandwidth.

TestAndPunt.ps1:

$url = "http://google.com"
$i = 0 # failure count
while(1){
  try{
    $req=[system.Net.HttpWebRequest]::Create($url)
    $req.Timeout = 10000 # in milliseconds
    $res=$req.getresponse()
    $res.Close()
    $i=0 # got it, reset failure count
  }
  catch{
    $i++ # failed, bump failure count
  }
  if( $i -eq 3 ){ # i give up, reboot
    shutdown -r -t 60 -f -c "Lost connection. Rebooting in 60 seconds..."
  }
  Start-Sleep -s 180 # wait a bit
}

ORIGINAL ANSWER: There are probably better ways, but i've written a little script to only restart if the connection fails. If you don't or can't use vbs, then curl or python or perl or whatever can do the same thing.

So, you'll be able to schedule it tighter--say, every five minutes--to cut down on the restarts and reduce the max time your connection is down.

TestAndPunt.vbs:

Set WshShell = WScript.CreateObject("WScript.Shell")
Set http = CreateObject("Microsoft.XmlHttp")
http.open "GET", "http://serverfault.com/", FALSE
http.send ""
strData = http.ResponseText
if( len(strData) = 0 ) Then
    Wscript.Echo "Restarting"
    WshShell.Run "%SystemRoot%\System32\shutdown.exe -r -t 600"
End If
set WshShell = nothing
set http = nothing
share|improve this answer
    
Very nice! Thank you, that is exactly what I was after. –  Colin Pickard Feb 26 '10 at 23:16
    
@Colin - no problem. Have fun! –  bill weaver Feb 27 '10 at 5:14
    
@Colin - added fancy-pants PowerShell script that does the waiting so you don't have to schedule it, adds a retry count, and is generally much better. –  bill weaver Feb 27 '10 at 7:23
    
very slick! I really must have a proper play with PowerShell one of these days. –  Colin Pickard Feb 27 '10 at 9:20

What I've done for now is to schedule the following as a task every 30 mins:

%SystemRoot%\System32\shutdown.exe -r -t 600

So if it dies, worst case I will be back on in under 40 mins. It does mean I've got to be ready with shutdown -a when I'm using it.

Anyone know if it might do any harm to leave your machine rebooting every half hour?

share|improve this answer
    
@Colin Pickard - i've added a vbs script that restarts only if the connection is down. Save you some restarts and you can schedule your task for every 5 minutes and have quicker turnaround if the connection goes down. –  bill weaver Feb 26 '10 at 21:26

From the command prompt you can use:

netsh interface set interface "Local Area Connection " DISABLE

netsh interface set interface "Local Area Connection " ENABLE

You can paste that into a new text file, save with a .BAT or .CMD extension then set up a scheduled task to launch however often you want.

However, I would recommend trying to update the driver and if it doesn't work, buy a PCI network card, you can get cheap ones for around £2 which are more than capable of doing the job.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.