#!/usr/bin/python import commands import os import sys import time def find_free_slots(vpstype, server): if (vpstype == "vps1"): slots = range(101, 121) else: slots = range(101, 113) used = {} cmd = "ssh -l root %s vzlist -a |egrep -v -e 'TEMPLATE|example\.com' | egrep -e '1..\\b' | awk {'print $1'}" %server for x in commands.getoutput(cmd).split(): used[x] = "" free = [] for k in slots: if not (used.has_key(str(k))): free.append(k) print "Used slots: " print sorted(used) return free def main(): hserver = raw_input("What is the parent server name? ") vpstype = raw_input("VPS type? (vps1/vps2) ") free = find_free_slots(vpstype, hserver) if ( len(free) == 0 ): print "There are no free spots on this server." sys.exit() else: uslot = str(free[0]) print "I will use this slot: %s\n" %uslot name = raw_input("Host name for this account? ") ips = [] ips.append(raw_input("Main IP? ")) rootpass = raw_input("Root password? ") EMAIL = raw_input("Contact e-mail address? ") octets = ips[0].split(".") for x in range(1, 4): last = int(octets[3]) + x print "IP: " print "%s.%s.%s.%s" %(octets[0], octets[1], octets[2], str(last)) ips.append("%s.%s.%s.%s" %(octets[0], octets[1], octets[2], str(last))) cmds = [] cmds.append("vzctl set %s --hostname %s --save" %(uslot, name)) for ip in ips: cmds.append("vzctl set %s --ipadd %s --save" %(uslot, ip)) cmds.append("vzctl start %s" %uslot) #Wait for client to start cmds.append("sleep 4") cmds.append("vzctl set %s --userpasswd root:%s" %(uslot, rootpass)) print "These are the setup commands: " print cmds print "\n" for cmd in cmds: os.system("ssh -l root %s %s" %(hserver, cmd)) #client setup starts here print "Client commands: " print "ssh -l root %s vzctl exec %s wget -P /root http://layer3.example.com/scripts/vps/vps-client-install.sh" %(hserver, uslot) print "ssh -l root %s vzctl exec %s bash /root/vps-client-install.sh %s %s %s\n" %(hserver, uslot, ips[0], rootpass, EMAIL) os.system("ssh -l root %s vzctl exec %s wget -nv -P /root http://layer3.example.com/scripts/vps/vps-client-install.sh" %(hserver, uslot)) os.system("ssh -l root %s vzctl exec %s chmod +x /root/vps-client-install.sh" %(hserver, uslot)) os.system("ssh -l root %s vzctl exec %s /root/vps-client-install.sh %s %s %s %s" %(hserver, uslot, name, ips[0], rootpass, EMAIL)) print "\n" print "Account setup done, login to WHM and double check things.\n" main()