Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Install Microsoft loopback adapter programmatically from VBS (or C#/VB)

I'd like to programmatically install the MS loopback adapter to automate tunneling of SMB over SSH.

All the code I found on the net uses the MS devcon utility, which is not redistributable (cf. http://support.microsoft.com/kb/311272/en-us). Example usage (more examples):

devcon -r install %WINDIR%\Inf\Netloop.inf *MSLOOP

Apart from the distributability issue, ideally i'd like to have some control over the resulting device name, though that could be fixed by enumerating the network adapters before and after and looking for the new MS loopback device. That's a bit racy, though I think I could live with it. My idea is to adapt some of this code.

I'm currently looking into the devcon source code from the WDK to add the loopback adapter via SetupAPI/CfgMgr32 as the MS KB article linked above suggests. Is there any easier/scriptable way?

If there is none, does anyone have some relatively simple sample code for that SetupAPI/CfgMgr32 route?

like image 288
Christian Aichinger Avatar asked Jun 17 '26 20:06

Christian Aichinger


1 Answers

I wanted to achieve the same thing without writing any new exe to do it and found it can be done with cscript and the devcon and netsh tools. creating the adapter seems to give you no control over what it will be called, so you have to enumerate using the WMI interface after you have created it. Unfortunately netsh behaviour depends on which version of windows you're on, but bung the following in a file called create-loopback.vbs and it will work on XP and 2008 server.

  Dim strLastLoopbackAdapterName, loopbackAdapterName 

  If wscript.arguments.count < 3 then
    WScript.Echo "usage: create-loopback.vbs loopbackAdapterName loopbackIpAddress loopbackSubNetMask "
    WScript.Quit
  end If
  loopbackAdapterName = wscript.arguments(0)
  loopbackIpAddress = wscript.arguments(1)
  loopbackSubNetMask = wscript.arguments(2)

  Wscript.Echo "Creating loopback called " &loopbackAdapterName &" on " &loopbackIpAddress &" with mask " &loopbackSubNetMask

  Set objShell = CreateObject("WScript.Shell") 
  Wscript.Echo "Installing loopback adapter..."

  objShell.Run "cmd /c devcon install %windir%\inf\netloop.inf *MSLOOP", 0, True 

  Wscript.Echo "Waiting for drivers to update..."
  Wscript.sleep 10000 'Allow 10s for install' 

  strComputer = "."
  Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
  Set colItems = objWMIService.ExecQuery("SELECT NetConnectionID FROM Win32_NetworkAdapter WHERE Name='Microsoft Loopback Adapter'", "WQL", 48)
  For Each objItem In colItems
     strLastLoopbackAdapterName = objItem.NetConnectionID
  Next

  Wscript.Echo "Last Loopback Connection is " & strLastLoopbackAdapterName  

  Wscript.Echo "Renaming new loopback..."
  objShell.Run "netsh interface set interface name = " &Chr(34) &strLastLoopbackAdapterName &Chr(34) &" newname = " &Chr(34) &loopbackAdapterName &Chr(34), 0, True 
  Wscript.Echo "Configuring loopback..."
  objShell.run "netsh interface ip set address name=" &Chr(34) &loopbackAdapterName &Chr(34) &" source=static " &loopbackIpAddress &" " &loopbackSubNetMask, 0, True 
  Wscript.Echo "Done"
  WScript.Quit(0)
like image 99
martski Avatar answered Jun 19 '26 14:06

martski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!