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

Hosts file

Not sure where to ask this question but we'll start here since it pertains to a VB .NET app that uses web services.

Is there a way to change which hosts file is used depending on which domain a user logs into?
0
thenrich
Asked:
thenrich
  • 5
  • 5
1 Solution
 
S-TwilleyCommented:
Can you give an example of what you mean... or what you're really trying to achieve
0
 
thenrichAuthor Commented:
I'll try.

I have a VB .NET application that uses web services for the back-end. It works great with 1 exception. There are certain users that use the application on both sides of our firewall. Logged into our network when sitting in there office and logged onto the local machine when on the road [if this isn't clear let me know cause it's critical to understanding the issue]

If the application runs behind our firewall I need to update the hosts file with the ip address of the web server hosting my services.

11.9.111.1  webservice.mycompany.com

Else I get an underlying connection was closed error.

If the user is on the road and logs into the local machine the previous IP address needs to be deleted for the application to work.

0
 
thenrichAuthor Commented:
S-Twilly do you have any ideas? Or should I have this question deleted.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
S-TwilleyCommented:
Sorry... forgot about this question.  My only idea would be two run two dummy hosts files side by side, and then just switch in the one you need... not really a good answer i know, but its all i can think of... or of course editting the host file itself... which means you are only working with one hosts file which is an advantage (especially if other apps are updating it)... but means having to read in and edit it each time u need to switch
0
 
thenrichAuthor Commented:
Do you know if there is a built in class in the .NET framework that allows you to work with such files other then the basic IO structure?

I was kinda thinking the same thing as you but was wondering if there was an easier way.
0
 
S-TwilleyCommented:
Im not sure of any... but there could well be something.

If i were to use the basic IO structure... i'd read in the whole file (or line by line... doesn't matter)... and test to see whether the first non-whitespace char is "#" or not... if it is, it's a comment (or commented out host matchup)... if it's not a #... then it's a matchup... in that case... read all chars up to the next whitespace char... and that should be your domain... then find the next non-whitespace char, read up to the end, that's your IP address

Alternatively, you could use regex to parse the line.

maybe something like  (some of the below is pseudo, im thinking off the top of my head.)
basically, unless its the specific line we want, we'll append the line to a string... if it is the line we want, we append the edited line... then output it  back to the hosts file


Dim regParse As New Regex("\s*(?<commented>(#|))\s*(?<server>\S*)\s+(?<ip>\S*)")

Dim rebuildFile As String = ""

FOR EACH LINE IN CODE  ' this is pseudo
   Dim matchThis As Match = regParse.Match(thisLine)
   if matchThis.Success Then
       Dim sComment = matchThis.Groups("commented").Value
       if sComment <> "#" Then
             Dim thisServer As String = matchThis.Groups("server").Value
             Dim thisIP As String = matchThis.Groups("ip").Value

             if thisServer = serverWeWant Then
                 rebuildFile &= thisServer & " " & SWITCHEDIP &  vbcrlf
             Else
                  rebuildFile &= thisLine & vbcrlf
             End If
       Else
           rebuildFile &= thisLine & vbcrlf
       End If
   Else
        rebuildFile &= thisLine & vbcrlf
   End If

   write rebuildFile over hosts file
0
 
thenrichAuthor Commented:
That's about what I had too. I spose for now that'll have to do.

I guess I thought it'd be a more common problem.
0
 
S-TwilleyCommented:
I know the feeling, I've come up with problems where I want to add values quickly to the hosts file or edit them... and there seems no easy solution.  I had hoped at one point to create an IE addon which blocks the current website (or one of the sites within the frames)... which would act as a good way to block add servers... but i never got around to it :P.  You probably know this but putting some adservers (or a better use would be sites which try to install malware) into the hosts files and pointing them to 127.0.0.1.. it'll prevent your computer from accessing the site, which in one case can protect you, but also sometimes stops popups (depending on how the popup was created)
0
 
thenrichAuthor Commented:
Yup familiar with the pointing to localhost. That IE addon seems interesting - I'd have a couple uses for that.
0
 
S-TwilleyCommented:
I don't like using popup blockers... I just turn javascript on the internet zone to prompt or disable... stops popups (i dont really venture around different sites so i put the common ones either in trusted or blocked).

but should i get common stray popups, I'd use my IE addon to block the site of that popup... or if im visiting a page i want, and it has things on it that are in the way, block those sites.

and the other application was to have the capability of adding a trusted site and it's ip match to the host file to speed up the host resolving (obviously has to be a static ip site)..

what were your ideas out of interest? :P
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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