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!
potpourrigroupAsked:
Who is Participating?
 
Fernando SotoRetiredCommented:
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
 
Fernando SotoRetiredCommented:
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
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
Fernando SotoRetiredCommented:
Is line 2 always called SERVER2 or is the IP address always the same as above, 192.168.1.2 ?
0
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.