Skip to content

Instantly share code, notes, and snippets.

@rafamaciel
Last active August 29, 2015 14:20
Show Gist options
  • Save rafamaciel/baec07fffedf7d6e3a04 to your computer and use it in GitHub Desktop.
Save rafamaciel/baec07fffedf7d6e3a04 to your computer and use it in GitHub Desktop.
Manger hosts file
#!/usr/bin/env python
import sys
import re
import os
def add_host():
if len(sys.argv) != 4:
print 'Usage: add [IP_ADDRESS] [HOSTNAME]'
return
ip_addr = sys.argv[2]
hostname = sys.argv[3]
if is_valid_ip(ip_addr) is False:
print 'Enter a valid IP'
return
with open('/etc/hosts', 'rt') as f:
s = f.read() + '\n' + ip_addr + '\t' + hostname
with open('/tmp/etc_hosts.tmp', 'wt') as outf:
outf.write(s)
os.system('sudo mv /tmp/etc_hosts.tmp /etc/hosts')
print 'Pointing hostname '+hostname+' for the ip '+ip_addr
def disable():
if len(sys.argv) != 3:
print 'Usage: enable [HOSTNAME]'
return
hostname = sys.argv[2]
f = open("/etc/hosts", "r")
lines = f.readlines()
f.close()
f = open("/tmp/etc_hosts.tmp", "wt")
for line in lines:
if line.__contains__(hostname):
line = '# ' + line
f.write(line)
else:
f.write(line)
f.close()
os.system('sudo mv /tmp/etc_hosts.tmp /etc/hosts')
def list_hosts():
f = open("/etc/hosts", "r")
lines = f.readlines()
for line in lines:
if line.__contains__('#'):
line = '[ DISABLED ]\t' + line.replace('# ', '')
else:
line = '[ ENABLED ]\t' + line
print line
f.close()
def enable():
if len(sys.argv) != 3:
print 'Usage: enable [HOSTNAME]'
return
hostname = sys.argv[2]
f = open("/etc/hosts", "r")
lines = f.readlines()
f.close()
f = open("/tmp/etc_hosts.tmp", "wt")
for line in lines:
if line.__contains__(hostname):
line = line.replace('# ', '')
f.write(line)
else:
f.write(line)
f.close()
os.system('sudo mv /tmp/etc_hosts.tmp /etc/hosts')
def remove():
if len(sys.argv) != 3:
print 'Usage: enable [HOSTNAME]'
return
hostname = sys.argv[2]
f = open("/etc/hosts", "r")
lines = f.readlines()
f.close()
f = open("/tmp/etc_hosts.tmp", "wt")
for line in lines:
if line.__contains__(hostname) is False:
f.write(line)
os.system('sudo mv /tmp/etc_hosts.tmp /etc/hosts')
def is_valid_ip(ip):
"""Validates IP addresses.
"""
return is_valid_ipv4(ip) or is_valid_ipv6(ip)
def is_valid_ipv4(ip):
"""Validates IPv4 addresses.
"""
pattern = re.compile(r"""
^
(?:
# Dotted variants:
(?:
# Decimal 1-255 (no leading 0's)
[3-9]\d?|2(?:5[0-5]|[0-4]?\d)?|1\d{0,2}
|
0x0*[0-9a-f]{1,2} # Hexadecimal 0x0 - 0xFF (possible leading 0's)
|
0+[1-3]?[0-7]{0,2} # Octal 0 - 0377 (possible leading 0's)
)
(?: # Repeat 0-3 times, separated by a dot
\.
(?:
[3-9]\d?|2(?:5[0-5]|[0-4]?\d)?|1\d{0,2}
|
0x0*[0-9a-f]{1,2}
|
0+[1-3]?[0-7]{0,2}
)
){0,3}
|
0x0*[0-9a-f]{1,8} # Hexadecimal notation, 0x0 - 0xffffffff
|
0+[0-3]?[0-7]{0,10} # Octal notation, 0 - 037777777777
|
# Decimal notation, 1-4294967295:
429496729[0-5]|42949672[0-8]\d|4294967[01]\d\d|429496[0-6]\d{3}|
42949[0-5]\d{4}|4294[0-8]\d{5}|429[0-3]\d{6}|42[0-8]\d{7}|
4[01]\d{8}|[1-3]\d{0,9}|[4-9]\d{0,8}
)
$
""", re.VERBOSE | re.IGNORECASE)
return pattern.match(ip) is not None
def is_valid_ipv6(ip):
"""Validates IPv6 addresses.
"""
pattern = re.compile(r"""
^
\s* # Leading whitespace
(?!.*::.*::) # Only a single whildcard allowed
(?:(?!:)|:(?=:)) # Colon iff it would be part of a wildcard
(?: # Repeat 6 times:
[0-9a-f]{0,4} # A group of at most four hexadecimal digits
(?:(?<=::)|(?<!::):) # Colon unless preceeded by wildcard
){6} #
(?: # Either
[0-9a-f]{0,4} # Another group
(?:(?<=::)|(?<!::):) # Colon unless preceeded by wildcard
[0-9a-f]{0,4} # Last group
(?: (?<=::) # Colon iff preceeded by exacly one colon
| (?<!:) #
| (?<=:) (?<!::) : #
) # OR
| # A v4 address with NO leading zeros
(?:25[0-4]|2[0-4]\d|1\d\d|[1-9]?\d)
(?: \.
(?:25[0-4]|2[0-4]\d|1\d\d|[1-9]?\d)
){3}
)
\s* # Trailing whitespace
$
""", re.VERBOSE | re.IGNORECASE | re.DOTALL)
return pattern.match(ip) is not None
for option in sys.argv:
if option == 'add':
add_host()
if option == 'remove':
remove()
if option == 'enable':
enable()
if option == 'disable':
disable()
if option == 'list':
list_hosts()
if len(sys.argv) == 1:
print 'Use a option: add, enable, disable, remove, list'

Install:

git clone [email protected]:/baec07fffedf7d6e3a04.git hosts  
sudo mv hosts/host_manager.py /usr/bin/hosts
rm -rf hosts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment