def scp(self, src, dst): vm = Vmrun(self.vmx) ip = vm.ip() user = self.user if ip: src_is_host = src.startswith(":") dst_is_host = dst.startswith(":") if src_is_host and dst_is_host: puts(colored.red("Both src and host are host destinations")) exit() if dst_is_host: dst = dst[1:] puts("Sending {src} to {user}@{ip}:{dst}".format( user=colored.green(user), ip=colored.green(ip), src=src, dst=dst, )) os.system('scp {} {}@{}:{}'.format(src, user, ip, dst)) else: src = src[1:] puts("Getting {user}@{ip}:{src} and saving in {dst}".format( user=colored.green(user), ip=colored.green(ip), src=src, dst=dst, )) os.system('scp {}@{}:{} {}'.format(user, ip, src, dst)) else: puts(colored.red("IP not found")) return
def ssh(self): vm = Vmrun(self.vmx) ip = vm.ip() if ip: puts("Connecting to {}".format(colored.green(ip))) os.system('ssh {}@{}'.format(self.user, ip)) else: puts(colored.red("IP not found"))
def ip(self): vm = Vmrun(self.vmx) print self.vmx ip = vm.ip() if ip: puts(colored.green(ip)) else: puts(colored.red("IP not found")) return ip
def do_use(self, args): """use specified vmx file, and initialize vmrun. Usage: use vmx-name """ VmxUI.do_use(self, args) if ( self.vmx is not None and self.vmx_admin is not None and self.vmx_pass is not None ): self.vmrun = Vmrun( self.vmx, self.vmx_admin, self.vmx_pass, debug=self.debug )
def start(self): vm = Vmrun(self.vmx) if self.gui: vm.start(gui=True) else: vm.start() puts(colored.yellow("Getting IP address...")) ip = vm.ip() puts(colored.green("VM started on {}".format(ip))) puts(colored.yellow("Sharing current folder...")) vm.enableSharedFolders() vm.addSharedFolder('mech', os.getcwd()) puts(colored.green("VM started on {}".format(ip)))
def get_vmrun(self, vmx_path): '''Returns an Vmrun objects using a vmx file path''' attributes = self.get_attributes() return Vmrun(user=attributes.guest_username, password=attributes.guest_password, vmx=vmx_path, debug=True, vmrun=VMRUN)
def stop(self): vm = Vmrun(self.vmx) if vm.check_tools() is True: vm.stop() else: vm.stop(mode='hard') puts(colored.green("Stopped", vm))
def mech_start(filename=False, gui=False): if filename: vm = Vmrun(filename) else: vm = get_vm() if gui: vm.start(gui=True) else: vm.start() ip = vm.ip() puts(colored.green("VM started on {}".format(ip)))
def suspend(self): vm = Vmrun(self.vmx) vm.suspend() puts(colored.green("Suspended", vm))
def ip(self): vm = Vmrun(self.vmx) print self.vmx ip = vm.ip() puts(colored.green(ip)) return ip
def status(self): vm = Vmrun('') puts("".join(vm.list()))
class ConsoleUI(VmxUI): """ """ vmrun = None def __init__(self, prompt, intro, config='.config', debug=False): """ Arguments: - `prompt`: - `intro`: """ VmxUI.__init__(self, config) self.prompt = self.make_prompt(prompt) self.intro = intro self.doc_header = "...oooOOO iConsole Command OOOooo..." \ "\n (for help, type: help <command>)" self.undoc_header = "" self.misc_header = "" self.ruler = " " self.debug = debug # FIXME awkward assert... def assert_vmrun(self): """Assert vmrun is properly initialized. Arguments: - `self`: """ if self.vmrun is None: print "type 'use' first..." return 0 else: return 1 def do_use(self, args): """use specified vmx file, and initialize vmrun. Usage: use vmx-name """ VmxUI.do_use(self, args) if ( self.vmx is not None and self.vmx_admin is not None and self.vmx_pass is not None ): self.vmrun = Vmrun( self.vmx, self.vmx_admin, self.vmx_pass, debug=self.debug ) def do_vmstart(self, args): """ Start vm Usage: vmstart """ return self.assert_vmrun() and "".join( self.vmrun.start() ) do_start = do_vmstart def do_vmsuspend(self, args): """ Suspend vm Usage: vmsuspend """ return self.assert_vmrun() and "".join( self.vmrun.suspend( "hard" ) ) do_suspend = do_vmsuspend def do_vmstop(self, args): """ Suspend vm Usage: vmstop """ return self.assert_vmrun() and "".join( self.vmrun.stop() ) do_stop = do_vmstop def do_vmcopy(self, args): """ Copy file to vm Usage: vmcopy from to """ argv = self.checkargs(args, 2) if argv == None: return # TODO change path return self.assert_vmrun() and \ "".join( self.vmrun.copyFileFromHostToGuest( argv[0], "\"%s\\%s\"" % (self.cwd_guest, argv[1]) ) ) do_cp = do_vmcopy def do_vmget(self, args): """ Get file from vm Usage: vmget file_of_vm as_file_host """ argv = self.checkargs(args, 2) if argv == None: return return self.assert_vmrun() and \ "".join( self.vmrun.copyFileFromGuestToHost( "\"%s\\%s\"" % (self.cwd_guest, argv[0]), "%s%s%s" % (self.cwd_host, os.sep, argv[1]) ) ) do_get = do_vmget def do_vmsnap(self, args): """Snapshot related commands Usage: [vm]snap list/create/delete/revert snap-name """ def vmsnap(argv): """ TODO - fix issue of quoted arguments Arguments: - `*argv`: """ if ( argv[0] == "list" or argv[0] == "l" ): print "".join( self.vmrun.listSnapshots() ) elif ( argv[0] == "create" or argv[0] == "c" ) and argv[1] != "": print "".join( self.vmrun.snapshot( argv[1] ) ) elif ( argv[0] == "delete" or argv[0] == "d" or argv[0] == "del" ) and argv[1] != "": print "".join( self.vmrun.deleteSnapshot( argv[1] ) ) elif ( argv[0] == "revert" or argv[0] == "r" ) and argv[1] != "": print "".join( self.vmrun.revertToSnapshot( argv[1] ) ) argv = self.checkargs(args) if argv == []: print "type 'help [vm]snap' ..."; return return self.assert_vmrun() and vmsnap(argv) do_snap = do_vmsnap def do_vmexec(self, args): """Execute program in the guest Arguments: - `self`: - `args`: """ def vmexec(argv): """ Arguments: - `argv`: """ try: pname = argv.pop(0) param = " ".join(argv) # FIXME # TODO n/a/i print "".join( self.vmrun.runProgramInGuest( "\"%s\"" % self.cfg.get( self.section, pname ), "n", "\"%s\"" % param) ) except Exception, e: print "[-] error : %s" % str(e) argv = self.checkargs(args) if argv == []: print "type 'help [vm]exec' ..."; return return self.assert_vmrun() and vmexec(argv)
def ip(self): vm = Vmrun(self.vmx) return vm.ip()
def stop(self): vm = Vmrun(self.vmx) vm.stop() puts(colored.green("Stopped", vm))
def pause(self): vm = Vmrun(self.vmx) vm.pause() puts(colored.yellow("Paused", vm))
def mech_status(): vm = Vmrun("") puts("".join(vm.list()))