• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3149
  • Last Modified:

VB.NET | Modify line in HOSTS file

Hello! Say I have the following HOSTS file:

192.168.1.1     SERVER1
192.168.1.2     SERVER2
192.168.1.3     SERVER3

Is there a way to change the IP address of line two? Or, if it's easier, remove the line all together and add a new line with the correct IP?

Not sure on the difficulty so I will adjust the points if neccessary.

Thanks Experts!
0
potpourrigroup
Asked:
potpourrigroup
  • 4
  • 3
1 Solution
 
Fernando SotoCommented:
Hi  potpourrigroup;

The HOSTS file contains the mappings of IP addresses to host names for TCP/IP.

If the second line is in error just open the file for edit correct the IP address and save the file, then reboot for the changes to take effect.

Fernando
0
 
potpourrigroupAuthor Commented:
Would love to but we have 200 computers we need changed. Looking to script it to save time.
0
 
Fernando SotoCommented:
Is line 2 always called SERVER2 or is the IP address always the same as above, 192.168.1.2 ?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
potpourrigroupAuthor Commented:
The name is the same on all machies...
0
 
potpourrigroupAuthor Commented:
So the line...

192.168.5.1      SERVER2

This is on all computers but has the incorrect IP address. Need to change the IP or replace the line.

Thanks for your response!
0
 
potpourrigroupAuthor Commented:
We do run internal DNS but one of our software vendors runs a crappy app that is HOSTS file dependent. That's how all this came about.
0
 
Fernando SotoCommented:
Hi  potpourrigroup;

The following code will open the hosts file on the system and make the change you want done. In the sample code it will look for the IP address 192.168.5.1 and replace it with 192.168.1.2. In your case you may have to replace one or both of the IP address in the Regular Expression. To do that the only place you need to modify in the code below is the following line.
        ' If the IP address in the next line is not what is in the file now, the change it to what it should be leaving all
        ' other characters as is.
        Dim output As String = Regex.Replace(input, "(192.168.5.1)(\s+SERVER2)", _
            ' I the IP address in the next line is not what you want to change it to in the file then change it and leaving the
            ' the $2 at the end alone.
            "192.168.1.2$2", RegexOptions.IgnoreCase)

Sample code follows:

Imports System.IO
Imports System.Text.RegularExpressions

        Dim Hosts As String = Environment.SystemDirectory & "\drivers\etc\hosts"
        Dim sr As New StreamReader(Hosts)
        Dim input As String = sr.ReadToEnd()
        sr.Close()
        Dim output As String = Regex.Replace(input, "(192.168.5.1)(\s+SERVER2)", _
            "192.168.1.2$2", RegexOptions.IgnoreCase)
        Dim sw As New StreamWriter(Hosts)
        sw.Write(output)
        sw.Close()


Fernando
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now