Skip to content

Instantly share code, notes, and snippets.

@jak
Created March 3, 2012 21:51
Show Gist options
  • Save jak/1968473 to your computer and use it in GitHub Desktop.
Save jak/1968473 to your computer and use it in GitHub Desktop.
Setup script for dnsmasq on sandbox
#!/usr/bin/python
#
# Sandbox dnsmasq setup script
#
# Jak Spalding <jak.spalding@bbc.co.uk>
# 2012-03-03
#
# for Python 2.4
import re
import subprocess
import os.path
import shutil
import sys
def main():
checkifroot()
print '************************'
print '* dnsmasq setup script *'
print '************************'
ip = raw_input('What is the IP address you want to use (e.g. 192.168.1.7)?\n');
print 'Checking IP address format...',
checkip(ip)
print 'Checking that dnsmasq is installed...',
checkdnsmasq()
print 'Adding dnsmasq hosts file (using %s)...' % ip,
addhostsfile(ip)
print 'Adding dnsmasq configuration...',
addconfig()
print 'Attempting to add dnsmasq to system startup...',
addstartup()
print 'Starting dnsmasq service...'
startdnsmasq()
print '************************'
print '* all done! *'
print '************************'
print 'You can now point your mobile device to use DNS server of', ip
def checkip(ip):
ret = re.match('(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)', ip)
if ret == None:
print "ERROR: That's not a valid IP address!"
sys.exit(1)
print 'OK'
def checkifroot():
try:
user = subprocess.Popen(['whoami'], stdout=subprocess.PIPE).communicate()[0]
except OSError:
print 'ERROR: Unable to run whoami to check if user is running as root'
sys.exit(1)
user = user.strip()
#print 'current user = %s' % user
if user != 'root':
print 'ERROR: You have to run this script as root.'
sys.exit(1)
def checkdnsmasq():
ret = 1
try:
ret = subprocess.call(['dnsmasq','-v'], stderr=subprocess.STDOUT, stdout=subprocess.PIPE);
except OSError:
ret = 1
if ret != 0:
print "ERROR: Please install dnsmasq (e.g. yum install dnsmasq)!"
sys.exit(1)
print 'OK'
def addhostsfile(ip):
fp = '/etc/dnsmasq.hosts'
contents = '%s pal.sandbox.dev.bbc.co.uk static.sandbox.dev.bbc.co.uk' % ip
if os.path.isfile(fp):
print 'File %s exists.. overwriting' % fp
f = open(fp, 'w')
try:
f.write(contents)
finally:
f.close()
print 'OK'
def addconfig():
fp = '/etc/dnsmasq.conf'
contents = "no-hosts\naddn-hosts=/etc/dnsmasq.hosts"
if os.path.isfile(fp):
print 'Making a backup of', fp
shutil.move(fp, fp + '.bak')
f = open(fp, 'w')
try:
f.write(contents)
finally:
f.close()
print 'OK'
def addstartup():
ret = 1
try:
ret = subprocess.call(['chkconfig','--level','3','dnsmasq','on'], stderr=subprocess.STDOUT, stdout=subprocess.PIPE);
except OSError:
ret = 1
if ret != 0:
print "ERROR: Failed to add dnsmasq to system startup! (Return value was %d)" % ret
sys.exit(1)
print 'OK'
def startdnsmasq():
ret = 1
try:
ret = subprocess.call(['service','dnsmasq','restart']);
except OSError:
ret = 1
if ret != 0:
print "ERROR: Failed to start dnsmasq... you'd better fix that!"
sys.exit(1)
try:
main()
except KeyboardInterrupt:
print 'Bye!'
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment