0

I'm trying to use a netsh script to change the IP address on a box that's headless. This works fine, unless there's an IP conflict. When an IP address conflict occurs, I see a dialog box that says "The static IP address that was just configured is already in use on the network. Please reconfigure a different IP address." In this case, netsh just hangs waiting for someone to acknowledge the dialog box.

I won't be the one using the script, and the users who are will definitely cause an IP conflict sometimes. Is there a way to get netsh to just return an error code instead? Anything would be better than just hanging.

2 Answers 2

1

I suspect the dialog is not being displayed by netsh, but rather by the operating system when it detects an IP address conflict.

You could try and ping the desired IP address, and if no response is obtained, assume the address is free and set it using netsh. The following vbscript provides an example:

Set oShell = CreateObject("WScript.Shell")
Set oExec = oShell.Exec("ping -n 2 -w 2000 192.168.2.3")

sResult = LCase(oExec.StdOut.ReadAll)
If InStr(sResult, "reply from") Then
  WScript.Echo "IP address is in use"
Else
  oShell.Run "netsh interface ip set address name=" _
  & Chr(34) & "Local Area Connection" _
  & Chr(34) & " static 192.168.2.3 255.255.255.0 192.168.2.1 1", 1, 1
End If

The script sends 2 ping packets, 2 seconds apart, and checks for a response that includes "reply from". You could even configure the script to increment the IP address and try again until an unused address is found.

You could also try a something similar using nslookup, as ping will only work if the computer with the conflicting IP is currently turned on.

2
  • Ping isn't reliable enough for me. Plenty of devices can refuse pings while still using the IP.
    – TimK
    Nov 14, 2009 at 13:45
  • In fact the correct way is to do a ARP request. May 6, 2011 at 10:16
0

The best way I found to do this was to wait for a certain amount of time, and then kill the netsh process if it hadn't completed. I did this by spawning a separate process for netsh from C#.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .