How to get and/or set IP address?

Hey, I'm using VB Express 2005 and I'm trying to write a small program to configure remote network devices. In order to do the initial configuration, I need to get the IP address of the local hard-wired ethernet adapter, or set it.

One way of doing this, I figured I could set the local hard-wired adapter to DHCP and connect it to the device. Windows would assign an automatic IP address, which I could simply obtain. I would much rather be able to assign a specific IP address, do what I need to, and the re-set the adapter to its previous settings. Finding the correct hard-wired adapter could be as simple and promting the user with a list of all the network cards and asking them to select the correct one.

Anyone have any advice I found the other post from January 06 on how to get the IP address, but I can't seem to make them work for my needs.



Answer this question

How to get and/or set IP address?

  • Brennon

    I have code that will do that in WMI... in passing... but I don't see a profile email address to send it to you in.



  • aagar_2003

    I tried your netsh command but, I realised that netsh always reports "Ok." even if there is a IP conflict on the network. I'm searching a way to avoid setting a conflicting address programmaticaly.

    Thank you,

    Francois.



  • Test 1

    That information definately gave me some insight as where I can go with some of the things I'm trying to do, but it didn't work for my current task.

    What I ended up doing is using the shell command netsh to manually set the local machine IP address and then return it to a DHCP setting once my program is configured.

    Thanks for the help! If you would like the code I used to set the local machine IP address, send me an email. Thanks.


  • sparrow2006

    By searching on the internet I found a trick that may work with ICMP turned off clients. Just by trying every address of the subnet with this WMI application. It is also possible to do a IP change with "netsh" but you don't know if there is someone on this ip programmaticaly. Only windows will report the conflict. WMI gives error messages for conflicts. I have not tried the procedure but that should solve the problem.

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

    For Each objNetAdapter in colNetAdapters
    errEnable = objNetAdapter.EnableStatic( _
    strIPAddress, strSubnetMask)
    errGateways = objNetAdapter.SetGateways(_
    strGateway, strGatewaymetric)
    Next

    Have a nice day,
    Francois.

  • Black6

    How can I get the Ip to incress by one, like inputing 10.10.10.10 and have the IP setting on the local area network changed to 10.10.10.11 Thanks.
  • BostonZZZ

    Here's a little background information on what I'm writing, and why I'm assigning an IP address.

    I install hardware on computer networks that manages physical access to the facility. To program and maintain this equipment, we connect to the local network. Our hardware is initially programmed by assigning an IP to the local computer doing the programming 1 IP away from what we're going to set the device to. Traditionally, we set this in Control Panels and then ARP the new IP address and MAC address statically. Then we're able to connect to our hardware and perform the permanent programming.

    Some of our techs do not posess the knowledge to perform this seemingly simple task, so I'm writing this program to do this. Essentially, my VB program does the following:

    1. Collect the information for the current network device to be programmed from the user.
    2. Temporarily set an IP address on the local machine.
    3. Set a static ARP table entry with the MAC and IP addresses previously obtained.
    4. 'Push' the new IP temporarily to the network device we're programming.
    5. Open a telnet window to the network device to allow the technician to finalize and verify the programming.
    6. Reset the local machine's network adapter to 'Automatically obtain an IP address.'

    I hope this helps you understand a little better why I'm doing what I am.


  • billinares

    What kind of adapter do you have and is that your interface to the internet itself

    Is it a router



  • Kishore Wani

  • Laserbeak43

    Here's the code that allows me to change the IP address:

    To change to a static (fixed) IP address:

    Dim varIPMask As String
    Dim varNetworkConnection As String
    Dim cmdChangeIP As String
    Dim varComputerIP As String
    cmdChangeIP =
    "netsh interface ip set address name=""" & varNetworkConnection & """ source=static addr=" & varComputerIP & " mask=" & varIPMask
    Shell(cmdChangeIP)

    Above, varIPMask is the Network Mask, varNetworkConnection is the name of the network connection, as shown in the Network Properties window of Control Panels, varComputerIP is the IP address you want to set the computer to, and cmdChangeIP is the complete command, once variables are assign values.

    To change to a dynamic (DHCP) or automatically assigned IP address:

    cmdChangeIP = "netsh interface ip set address name=""" & varNetworkConnection & """ source=dhcp"

    I hope this helps you all!

    Note that this solution does NOT require a reboot of the computer!  This method has only been tested on Windows XP Professional, Windows 2000 Professional.


  • norm74

    I didn't get anything yet...
  • Steve Stirrup

    It's sent.

    This was working code used with a router. It also has WMI code that interrogates a system for network adapters and that will be helpful.

    Unfortunately, I believe this version asked the router for the IP it saw,,, so that won't be helpful.



  • Byron

    I can't allow the adapter itself to be an issue. Ultimately, I think I'll need to enumerate all of the available network adapters on the given machine and allow the user to select the correct one. From there, I need to set the IP address, hopefully without a required reboot.

    Thanks.


  • ea1

    I'm terribly sorry. I just found that gmail will not take an extension of VB and it sat in my outbox for quite a while. I renamed the extension and sent it out.

    Renee



  • BLIS

    I don't understand what you mean by set the machine's IP address.

    Are you on a LAN where you have IPs to dispense Usually IPs are assigned by a provider. Who knows You my be the provider for these machines or managing their IP's. It's just that the way you are languaging what you are doing suggests a relationship to the machines that I'm not familiar with.



  • How to get and/or set IP address?