python 脚本编写vm系统操作

来源:互联网 发布:淘宝店铺自然流量 编辑:程序博客网 时间:2024/06/06 07:02
import subprocess
import time

class VMHelper:
    def __init__(self,vmRun,vmxPath,snapShot,user,passwd):
        self.vmrun = vmRun
        self.vmxPath = vmxPath
        self.snapShot =snapShot
        self.user = user
        self.passwd = passwd

    def RevertToSnapshot(self):
        subprocess.call([self.vmrun,"-T","ws","revertToSnapshot",self.vmxPath,self.snapShot])

    def StartVM(self):
        subprocess.call([self.vmrun,"-T","ws","start",self.vmxPath])
        time.sleep(10)

    def StopVM(self):
        subprocess.call([self.vmrun,"-T","ws","stop",self.vmxPath])
        time.sleep(10)

    def CreateSnapshot(self,newsanpshot):
        subprocess.call([self.vmrun,"-T","ws","snapshot",self.vmxPath,newsanpshot])

    def DeleteSnapshot(self):
        subprocess.call([self.vmrun,"-T","ws","deleteSnapshot",self.vmxPath,self.snapShot])

    def CopyFileFromHostToGuest(self,hostfile,vmfile):
        #subprocess.call("%s -T ws -gu %s -gp %s CopyFileFromHostToGuest %s %s %s" % (self.vmrun,user,passwd,self.vmxPath,hostFile,vmFile))
        subprocess.call([self.vmrun,"-gu",self.user,"-gp",self.passwd,"CopyFileFromHostToGuest",self.vmxPath,hostfile,vmfile])

    def CopyFileFromGuestToHost(self,vmfile,hostfile):
        #subprocess.call("%s -T ws -gu %s -gp %s CopyFileFromGuestToHost %s %s %s" % (self.vmrun,user,passwd,self.vmxPath,vmFile,hostFile))
        subprocess.call([self.vmrun,"-gu",self.user,"-gp",self.passwd,"CopyFileFromGuestToHost",self.vmxPath,vmfile,hostfile])

    def runScriptInGuest(self,ScriptPath):
        subprocess.call("%s -gu \"%s\" -gp \"%s\" runScriptInGuest \"%s\" -interactive \"\" \"cmd.exe /k \"%s\" \" " %(self.vmrun,self.user,self.passwd,self.vmxPath,ScriptPath))

    def runProgramInGuest(self,programName,parameter):
        subprocess.call("%s -gu \"%s\" -gp \"%s\" runProgramInGuest \"%s\" -interactive \"%s\" \"%s\"" %(self.vmrun,self.user,self.passwd,self.vmxPath,programName,parameter))
        #command = ("vmrun -gu \"administrator\" -gp \"weirenminfuwu\" runProgramInGuest \"{0}\" -interactive \"C:\\virus.exe\"",programName);


if __name__ == "__main__":
    print "ok"
0 0
原创粉丝点击