How to detect the network interface is "initialized"

2k Views Asked by At

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%
1

There are 1 best solutions below

0
On

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