Skip to content

Instantly share code, notes, and snippets.

@ivarvong
Created September 7, 2010 23:48
Show Gist options
  • Save ivarvong/569350 to your computer and use it in GitHub Desktop.
Save ivarvong/569350 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/python
import re
import os
import subprocess
def createJail(jail = '', ip = ''):
#Prompts user for Jail parameters and builds Jail filesystem
conf_file = '/etc/rc.conf'
jail = raw_input('Jail name (whatever.uoregon.edu): ')
ip = raw_input('Jail IP (10.0.0.xxx): ')
confirm = '\nPlease review your selections\nJail: "' + jail + '"\nIP: "' + ip + '"\nType "Yes/No" to continue.\n'
conf = raw_input(confirm)
if conf.lower() == 'yes':
print 'Bulding Jail %s...' % (jail)
#Copies base jail to new jail dir
subprocess.call('cpdup /var/jails/www-base/ /var/jails/' + jail, shell=True)
#dict = {}
#tuple = ()
li = []
#Appends ifconfig alias to rc.conf
fileHandle = open(conf_file)
fileList = fileHandle.readlines()
for fileLine in fileList:
#print '>>', fileLine
a = re.compile('^ifconfig_em0_alias([0-9]+)=.*$')
m = a.search(fileLine)
if m:
li.append(m.group(1))
fileHandle.close()
li.sort(key=int, reverse=True)
new_alias = 'ifconfig_em0_alias' + str(int(li[0]) + 1) + '="' + ip + '/32"\n'
fileList.append(new_alias)
fileHandle = open(conf_file + '.new', 'w')
fileHandle.writelines(fileList)
fileHandle.close()
os.rename(conf_file, conf_file + '.old')
os.rename(conf_file + '.new', conf_file)
#Instantiates new alias
subprocess.call('ifconfig em0 alias ' + ip + '/32', shell=True)
#Runs ezjail-admin to setup jail
subprocess.call('ezjail-admin create -x ' + jail + ' ' + ip, shell=True)
#Runs ezjail.sh to startup new jail
subprocess.call('/usr/local/etc/rc.d/ezjail.sh start ' + jail, shell=True)
#Finish
print 'Done'
else:
createJail()
createJail()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment