What do you need to know before configuring Loopback Adapter?

  • do not set the gateway because you can screw you internet connection
  • do not set DNS because you really don’t need it
  • do not set 127.0.0.1 or 192.168.0.1 or addresses from the same class as your internet connection address

The addresses that I’m using most of the time are 10.10.10.10 and 192.168.100.100, both with 255.255.255.0 as mask.

Let’s see how can we set the IP address from the command line.

Solution 1
=======

Netsh.exe is a tool installed with Windows 2000, XP and 2003 Server. The name comes from Network Shell I guess…
In order to set an IP address for the adapter called “Local Area Connection 4” you need to run this command:

netsh interface ip set address name="Local Area Connection 4" static 192.168.100.100 255.255.255.0 1

This solution has a big disadvantage: it is not fully automated. You need first to check the name of the connection and then run netsh with that name. Depending on how many adapter you have installed, the last digit differs. So we need to another solution that will query the adapters and selects the loopback.

Solution 2
=======

A better solution is to create a script that query all the network adapters and selects the loopback adapter. I assume that there is no reason to have more than 1 loopback adapter installed.
The script is the following:

strIPAddress = "192.168.100.100"
strSubnetMask = "255.255.255.0"
Set objNetwork = CreateObject("WScript.Network")
strAlias = objNetwork.ComputerName

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery _
                ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
strGatewayMetric = Array(1)
For Each objNetAdapter in colNetAdapters

        if objNetAdapter.Description = "Microsoft Loopback Adapter" then
                errEnable = objNetAdapter.EnableStatic(array(strIPAddress), array(strSubnetMask))
                If errEnable = 0 Then
                        WScript.Echo "The IP address has been changed."
                Else
                        WScript.Echo "The IP address could not be changed."
                End If

        end if
Next

You can download the script here

Solution 3 – RECOMMENDED
====================

My preferred solution is to use an interactive script, the same like Solution 2, but the IP Address and mask are entered interactive by the user. The nice thing here is that we can set default values, so if the user is undecided, he can choose the default values.
The script is the following:

strIPAddress = inputbox("Set the IP Address", "IP Address?", "192.168.100.100")
strSubnetMask = array(inputbox("Set mask", "Mask?", "255.255.255.0"))

Set objNetwork = CreateObject("WScript.Network")

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery _
                ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
strGatewayMetric = Array(1)

For Each objNetAdapter in colNetAdapters

        if objNetAdapter.Description = "Microsoft Loopback Adapter" then
                errEnable = objNetAdapter.EnableStatic(array(strIPAddress), strSubnetMask)
                If errEnable = 0 Then
                        WScript.Echo "The IP address has been changed."
                Else
                        WScript.Echo "The IP address could not be changed."
                End If
        end if
Next

When you will run the script it will open the following screen:
set IP address
set mask
You can download the script here

If you have any issues, comments, suggestions, feel free to post a comment.

Read also

Tags: , ,
7 Responses to “Microsoft Loopback Adapter automatic configuration”
  1. Steven ChalkNo Gravatar says:

    Script for loopback adaptor is missing the value for strComputer and this is not commented in your script. Good work though as your simple netsh line works great.

    thanks

  2. StanislavNo Gravatar says:

    Hi,
    value strComputer is not presented, but you can use:

    Set WshNetwork = WScript.CreateObject(“WScript.Network”)
    Set objWMIService = GetObject(“winmgmts:\\” & WshNetwork.ComputerName & “\root\cimv2”)

    Thank you!

  3. mikeNo Gravatar says:

    Hi there I have tried this script with the replacements noted but I get an error when running it:

    SWbemObjectex(18,3): type mismatch

    The line in question is: errEnable = objNetAdapter.EnableStatic(array(strIPAddress), strSubnetMask) I think.

    Can you help me with this please?

  4. micro informatique serviceNo Gravatar says:

    Thanks for the useful info. It’s so interesting!

  5. ScherzNo Gravatar says:

    Hi,
    Sorry that i dident put my real mail but i want to know if its posible to do a READ (“c/c++”) on the loopback adapter
    thnanks a loot

  6. andreiashNo Gravatar says:

    I don’t know what methods you can access from C/C++. But as long they are available to VBScript, you should be able to do this. Unfortunately I don’t know more on this.

    Good luck!

  7. ScherzNo Gravatar says:

    how would be the funktion for a read in VBS???

Leave a Reply