Results 1 to 16 of 16

Thread: Set my IP address from a file?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2009
    Posts
    140

    Set my IP address from a file?

    Hi, Forum-folks!
    It's been awhile since I darkened these halls, but I'm back with another question.....
    The systems we're building comprise several PCs (24-36 of them), each of which needs to have his IP address set.... Since I'm already using a "configuration file" approach for several other variables, it occurred to me that it'd be nice not to have to set the IP address manually, but just specify what I want it to be in the config file, and have the code take care of setting it.....
    I went a-hunting for some code to crib, and found something that looked kind of like this:
    Code:
       Dim regKey As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine
            Dim subKey As Microsoft.Win32.RegistryKey = regKey.OpenSubKey("SYSTEM\\CurrentControlSet\\services\\Tcpip\\Parameters\\Interfaces\\{30871C26-5B03-4E9C-A3F9-2277765c265F}", True)
                   subKey.SetValue("IPAddress", My_IP)
                   subKey.Close()
    Note that the example I found had a different string of characters between the curly-braces, and did NOT work, so I used Regedit to run the chain of keys down to \\Interfaces, and found that the key below that that contained the IPAddress subkey looked like the one in the code above, so... I copied that info from my registry and replaced what was between the curly braces and it worked like a charm (after a reboot, that is)! However.....

    I can't help feeling this nagging sense of dread that that string is unique to this particular PC, and will be different on others..... If so, this approach isn't gonna cut it.....

    Can anyone out here in Forumville either confirm my suspicions, or calm my worries? Maybe even suggest a better method? I like this approach, because it's so compact, and doesn't require a boatload of setup to implement, but if it's not going to work across all the PCs, then it's really not that useful ....

    Thanks loads for any help/hope you guys/gals can offer!

    Joe

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Set my IP address from a file?

    That's "string of characters between the curly-braces" is a GUID and is going to be different on every machine. In fact it is going to be different for every interface on each machine. You can have more than one - I have at least three... Wired, Wireless, work VPN, and at least one, possible 2 client VPNs too. Each one is going to have its own IP address. If you can guarantee that you're only dealing with one, then it'll probably work, but you're going to run into the issue with the GUID changing on every machine. For that, I got nothing.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2009
    Posts
    140

    Re: Set my IP address from a file?

    Thanks, Techgnome, for the quick reply!
    The motherboards we're using have 2 Ethernet ports, but only 1 will ever be used, so that "multiple interfaces" concern doesn't bother me too much.... The "changing GUID" on the other hand.... sounds like that will shoot this approach squarely between the eyes.... Anybody got another approach that's comparably simple & compact?

    Hopefully,
    Joe

  4. #4
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,393

  5. #5
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,757

    Re: Set my IP address from a file?

    I've used netsh in the past. I think it's referred to in jdc's second link.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Feb 2009
    Posts
    140

    Re: Set my IP address from a file?

    Hi, kebo! Thanks for the reply! I've looked at jdc's second link, and tried to (more or less) duplicate what's there in Solution 2.... I'm assuming that rather than "System.Diagonistic.Process.Start", I should use "System.Diagnostics.Process.Start", followed by the info I need to set it... Somewhat like this:?
    Code:
    Private Function set_IP(ByRef My_IP As String)
            Dim NETSH_STR1 As String = "netsh interface ip set address name=Ethernet source=static addr="
            Dim netsh_str2 As String = " mask=255.255.255.0 gateway=192.168.42.1"
            Dim NETSH_STR As String = NETSH_STR1 & My_IP & netsh_str2
            MsgBox(NETSH_STR)
            'System.Diagnostics.Process.Start(NETSH_STR)
    
            Return 0
        End Function
    The msgbox echoes what I'd expect to see after having read the desired IP address from the file and passing it to the function.... However, when I un-comment the line that actually executes the string, I get a "file not found" message... Code compiles fine, but I get nasty-grams at run-time.... Can anybody point me at what I might be doing wrong here?

    Thanks!

    Joe

  7. #7
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Set my IP address from a file?

    I think you need to shell out to the command line first to run a netsh command... to do that, it's like this:
    Code:
            Dim NETSH_STR1 As String = "netsh interface ip set address name=Ethernet source=static addr="
            Dim netsh_str2 As String = " mask=255.255.255.0 gateway=192.168.42.1"
            Dim NETSH_STR As String = NETSH_STR1 & My_IP & netsh_str2
            MsgBox(NETSH_STR)
            Dim psi As New ProcessStartInfo
            psi.FileName = "cmd.exe"
            psi.Arguments = NETSH_STR
            System.Diagnostics.Process.Start(psi)
    So this should shell out to the commandline and run your netsh command...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Feb 2009
    Posts
    140

    Re: Set my IP address from a file?

    I'll give that a shot, Techgnome! Thanks!!

    {EDIT}
    Well, it seems to have had an effect, but not the one I'd hoped for.... Now I see a command-prompt window open, but nothing more seems to have happened... tried hitting the up arrow, in case the netsh command was left in the buffer, but no command appeared....
    here's the code as it stands at the moment:
    Code:
    Private Function set_IP(ByRef My_IP As String)
            Dim NETSH_STR1 As String = "netsh interface ip set address name=Ethernet source=static addr="
            Dim netsh_str2 As String = " mask=255.255.255.0 gateway=192.168.42.1"
            Dim NETSH_STR As String = NETSH_STR1 & My_IP & netsh_str2
            Dim psi As New ProcessStartInfo
            psi.FileName = "cmd.exe"
            psi.Arguments = NETSH_STR
            System.Diagnostics.Process.Start(psi)
            MsgBox(NETSH_STR)
            Return 0
        End Function
    Am I missing something here?

    {another edit}
    BTW, I checked the IP address via the control panel, after the command-prompt window appeared , and it hasn't changed......
    Last edited by oldmainframehack; Oct 3rd, 2017 at 10:11 AM. Reason: add info

  9. #9
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Set my IP address from a file?

    Try adding a command argument to your cmd application.
    Dim NETSH_STR1 As String = " /c netsh interface ip set address name=Ethernet source=static addr="

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Feb 2009
    Posts
    140

    Re: Set my IP address from a file?

    Welll..... that didn't seem to improve matters much.....it appears that caused the system to reboot..... not what I was after.....

    {edit} What was the /c supposed to do?
    Last edited by oldmainframehack; Oct 3rd, 2017 at 12:22 PM. Reason: requesting clarification....

  11. #11
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Set my IP address from a file?

    Maybe netsh should be the filename and the rest set as the Arguments (to be honest, I'm just stumbling in the dark here, well severely dimly lit at any rate - I know how to use Process.Start, just not with this current context and have no way of trying it).

    If that doesn't work, the worst case would be to write the netsh commands out to a batch file, then execute that... at least then you can include a "Pause" command at the end and hold the command window open long enough to see if there's any errors.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  12. #12
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Set my IP address from a file?

    I just did a quick test and the following code worked fine for me. Didn't require specifying a cmd exe.
    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e aAs System.EventArgs) Handles Button1.Click 
     
      System.Diagnostics.Process.Start("netsh", "interface ip set address ""Local Area Connection"" static 192.168.200.10 255.255.255.0")
    
    End Sub
    So, changing your latest code to this seems like it should work
    Code:
    Private Function set_IP(ByRef My_IP As String)
            Dim NETSH_STR1 As String = "interface ip set address name=Ethernet source=static addr="
            Dim netsh_str2 As String = " mask=255.255.255.0 gateway=192.168.42.1"
            Dim NETSH_STR As String = NETSH_STR1 & My_IP & netsh_str2
            Dim psi As New ProcessStartInfo
            psi.FileName = "netsh"
            psi.Arguments = NETSH_STR
            System.Diagnostics.Process.Start(psi)
            MsgBox(NETSH_STR)
            Return 0
        End Function
    Is the Ethernet Adapter you want to change named "Ethernet"? The default names for a two port configuration are usually "Local Area Connection" and "Local Area Connection 2".
    I usually change the names at work when we have multiple connections to give them more meaning full names, e.g. "Simulation", "Avionics", "Offboard", etc...
    Makes them easier to choose in Wireshark as well since I don't have to figure which generic name is associated with which network.
    Last edited by passel; Oct 3rd, 2017 at 12:51 PM.

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Feb 2009
    Posts
    140

    Re: Set my IP address from a file?

    Yep - there's 2 Ethernet holes on this guy, labeled "Ethernet" and "Ethernet 2" ... I'm going to try removing the "name=", "source=", etc. so it looks more like what you had in the first code-box, and see what happens....

    Wish me luck!

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Feb 2009
    Posts
    140

    Re: Set my IP address from a file?

    No joy.... Still reboots when the "netsh.exe" runs.....Not sure it's really worth all this straining to get the IP address set from a file.... FWIW, this is in a Windows 10 Enterprise environment.... Is netsh supposed to behave differently there?

    Additional info:
    This application normally runs with up to 6 DisplayLink monitors, and I wasn't seeing anything but the reboot.... Just now I disconnected the DisplayLink monitors and connected an HDMI monitor.... A message appeared on-screen before rebooting that said something like "your PC experienced trouble and had to restart - we'll restart it for you".... Down at the bottom of that page was a message that said "Page fault in non-paged area"..... Mean anything to you guys/gals?

    More info: this last time it rebooted the message said "UNEXPECTED KERNEL MODE TRAP".... curiouser and curiouser.......
    Last edited by oldmainframehack; Oct 3rd, 2017 at 01:55 PM.

  15. #15
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Set my IP address from a file?

    Well, I was assuming you had Admin privileges.
    But I tested on a Win7 pro machine, not a Win10 machine, so don't know if Win10 has decided this is something that shouldn't be allowed.
    I guess I have a Win10 (not enterprise) laptop that I have the necessary access to give it a try.
    I'll update this post once I've given it a try.

    Ok, I logged in as a user who is admin capable.
    Running the code didn't cause my system to reboot but it didn't change the address either.
    Running the line in cmd window revealed that the cmd window needed to be run with elevated rights for the netsh to work.
    So I changed the code to add the "runas" verb so the process launched will have elevated privileges. Of course it opens a dialog asking for allowance to run elevated, and once granted it works.
    I didn't try to see if there was a way to get around the dialog box.
    Code:
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim psi As New ProcessStartInfo
            psi.FileName = "netsh"
            psi.Arguments = "interface ip set address ""Ethernet"" static 192.168.200.20 255.255.255.0"
            psi.Verb = "runas"
            System.Diagnostics.Process.Start(psi)
        End Sub
    Last edited by passel; Oct 3rd, 2017 at 04:19 PM.

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Feb 2009
    Posts
    140

    Re: Set my IP address from a file?

    Thanks, Passel, for all your help!
    As the "runas" causes a prompt, which must be responded to, I think I'm better off just setting the silly thing manually....Hey, I'd have to edit the config file for each one anyway, to get the proper IP address in there... So how much more trouble is it to just go ahead and set the silly address myself.... Looks like this is really more trouble than it's worth.... Thanks anyway, for your time and efforts!

    Joe

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width