直接上代码,这次pyhton写的,python2哦:

#!/usr/bin/env python

import socket
import thread
import time
import sys
import os


HOST = '127.0.0.1'
USER = 'springhack'
PORT = 10022


BUFFSIZE = 1024
ADDR = (HOST, PORT)


LOGO = '''
         #####
        #######
        ##O#O##            Powered by SpringHack
        #######            Email: springhack@live.cn
      ###########
     #############          
    ###############         
    ################        
   #################        
 #####################      
 #####################      
   #################        
'''

print LOGO


class ProgressBar:
    def __init__(self, count = 0, total = 0, width = 50):
        self.count = count
        self.total = total
        self.width = width
    def move(self):
        self.count += 1
    def log(self, s):
        sys.stdout.write(' ' * (self.width + 9) + '\r')
        sys.stdout.flush()
        print s
        progress = self.width * self.count / self.total
        sys.stdout.write('{0:3}/{1:3}: '.format(self.count, self.total))
        sys.stdout.write('#' * progress + '-' * (self.width - progress) + '\r')
        if progress == self.width:
            sys.stdout.write('\n')
        sys.stdout.flush()



def showBar(count, timeout, cb):
    bar = ProgressBar(total = count)
    for i in range(count):
        bar.log('Trying ...')
        ret = cb(bar)
        if ret:
            return True
        bar.move()
        bar.log('Tried to connect vm %d time(s) ...' % (i + 1))
        time.sleep(timeout)
    return False



def getList():
    list = os.popen(r"VBoxManage list vms | awk -F \" '{print $2}'").read().split('\n')
    while '' in list:
        list.remove('')
    return list


def startVM(vm):
    print 'Starting vm ...'
    os.system('VBoxHeadless -startvm "%s" >/dev/null 2>&1 &' % vm)
    time.sleep(3)
    print 'Started !'


def killConn(conn):
    time.sleep(2)
    conn.close()


def SSHCheck(bar):
    try:
        conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        conn.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
        conn.connect(ADDR)
        conn.setblocking(0)
        conn.send('WTF Test')
        bar.log('Sending test data ...')
        time.sleep(2)
        bar.log('Trying to get banner ...')
        rec = conn.recv(BUFFSIZE)
        if rec.find('OpenSSH'):
            return True
        else:
            return False
    except Exception,e:
        return False


def main():
    print 'List all environment: '
    list = getList()
    for i in range(len(list)):
        print '\t%d. %s' % (i + 1, list[i])
    ch = int(input('Choice a env: '))
    if ch >= len(list):
        print 'No such env, exit !'
        return
    vm = list[ch - 1]
    startVM(vm)
    ret = showBar(20, 3, SSHCheck)
    if ret:
        print '\n\nOK, link start !\n'
        time.sleep(2)
        os.system('ssh %s@%s -p %d' % (USER, HOST, PORT))
        print 'Exiting ...'
        os.system('VBoxManage controlvm "%s" poweroff' % vm)
        os.system('pkill VBox*')
        print 'We done !'
    else:
        print 'Unknown error, try again ?'
        os.system('VBoxManage controlvm "%s" poweroff' % vm)
        os.system('pkill VBox*')
        print 'Exited !'



if __name__ == '__main__':
    main()