0

I have to check website performance through various webproxies from my Fedora 19 box, and I want to do that by setting a local resolvable name "proxy" to the DNS resolved names for our proxies in my /etc/hosts file. So, contents of /etc/hosts file should look something like this:

proxy.us.company.com    proxy
#proxy.eu.company.com    proxy
#proxy.sa.company.com    proxy

Then when I need to test a different proxy, I can just edit the /etc/hosts file and uncomment a different proxy DNS name, and all my logins (and everyone else using my machine) will be checking against the new proxy. And most of the above DNS names are round-robin entries which is also fine and required for my testing. (Truth be told, I also need this for actual work, because every once in a while proxy.us gets bogged down and one of the other proxies ends up being faster than proxy.us.)

How do I go about doing this? I did consider writing a script to change the http_proxy env variable, but with that method I would need to add an additional step to every process to align all my logins' variables together. I want to change it in just once place. /etc/hosts seems the most logical place to make a system-wide name resolution change like this.

3 Answers 3

4

/etc/hosts only maps names to IP addresses.. You cannot point a name at another name.

You could still use /etc/hosts, but you'll have to script up resolving the name to an IP and modifying /etc/hosts with that IP or do the work ahead of time, creating multiple hosts files and have your script simply move the file into place for each proxy at the proper point of the check.

0

This cannot be done. Best solution to do what I want is to install a name resolver (bind, named) and update its local tables with the FQDN's of the proxies.

0

Here's a python script that does what you want:

#!/usr/bin/env python
from dns.resolver import Resolver
from re import sub
hostsfile='/etc/hosts'
proxies = [
    'proxy.us.company.com',
    'proxy.eu.company.com',
    'proxy.sa.company.com'
]
name = 'proxy'

def main():
    proxy = menu('Select proxy:', proxies)
    ip = Resolver().query(proxy,'A')[0].to_text()
    if len(ip):
        with open(hostsfile, 'r') as h:
            hosts = h.read()
        with open(hostsfile, 'w+') as h:
            hosts = sub('((\n|(?<!\n)\.)(1?\d?\d|2[0-4]\d|25[0-5])){4} +'+name+'(?= *\n)', '\n'+ip+' '+name, hosts)
            h.write(hosts)

def getInt(question, min, max):
    min,max = [int(max),int(min)] if min>max else [int(min),int(max)]
    while True:
        try:
            answer = int(raw_input('{0}: '.format(question)))
            if min <= answer <= max:
                return answer
            print('Must be a number from {0} to {1}'.format(min,max))
        except ValueError:
            print('Not a valid number')

def menu(title, items, index=False):
    print(title)
    for i, item in enumerate(items):
        print('{0}. {1}'.format(i+1, item))
    answer = getInt('', 1, len(items)) - 1
    return answer if index else items[answer]

main()
4
  • 1
    What does this do? Jul 15, 2016 at 5:03
  • Asks you to choose a proxy hostname from a list, resolves the one you chose to an IP address, and replaces the existing hosts entry with the new IP.
    – Knowbody
    Jul 15, 2016 at 5:16
  • Also, the regex is designed to check for a valid IP address at the start of the line.
    – Knowbody
    Jul 15, 2016 at 5:25
  • 1
    @Knowbody the point that Michael Hampton was making was that you should really include an explanation of what your code does in your answer.
    – BE77Y
    Jul 15, 2016 at 7:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .