0

My USB network adapter (latest drivers) disappear from "network connections" window in control panel when computer wake up from sleep. So construct batch file to resetting adapter using devcon. After adapter reset I want to add some IP subnets programatically using netsh. Problem is the adapter probably need some time after reset (disable && enable commands) is performed, because netsh failed if started earlier than some (let say 6 seconds, old Atom CPU is used) time. Now I have inserted timeout using 'timeout /T' command but this time could vary by system load. Question is: Is there any way to detect adapter status programatically from batch file?

set "netcmd=netsh in ip add address ETH1 192.168.0.100 255.255.255.0"
devcon disable "USB\VID_9710&PID_7830" >NUL 
devcon enable "USB\VID_9710&PID_7830" >NUL 
timeout /T 6 >NUL
%netcmd%
user2956477
  • 1,208
  • 9
  • 17
  • does `netsh interface show interface name=Ethernet` give you what you look for? (change `Ethernet` to the name of your intereface) – Stephan Dec 26 '17 at 14:26
  • 1
    It would help potential responders were you to append your batch file content to your question. For that [edit your question](https://stackoverflow.com/posts/47979520/edit), paste in your content, select it and press the [**{}**] button to format it as code. – Compo Dec 26 '17 at 14:44
  • @user2956477, FYI, `timeout /t N` will wait at least N seconds before returning, possibly a tiny bit longer depending on system load. – jwdonahue Dec 26 '17 at 16:45
  • @user2956477, have you tried removing the timeout and checking whether netsh returns a failure code? Perhaps a loop with a one or two second timeout until the netsh command succeeds? You could also look for the media state in the `ipconfig` output. – jwdonahue Dec 26 '17 at 17:12

1 Answers1

1

netsh interface show interface name="Local Area Connection" shows something like :

Local Area Connection
   Type:                 Dedicated
   Administrative state: Enabled
   Connect state:        Disconnected

You can check it's state with:

:loop
timeout 2
echo still waiting...
netsh interface show interface name="Local Area Connection" | find "Connect state" |find "Connected" >nul || goto :loop
echo Interface is now connected.

Same can be done with ...find "Administrative state" |find "Enabled" ... (depends on which adapter status you refer to)

NOTE: change Local Area Connection to the name of your adapter

Stephan
  • 53,940
  • 10
  • 58
  • 91