コード例 #1
0
print "------"
print exampleVm.sshCommand(["ls nonexistent ; echo `hostname`"]).output
print "------"
# these scp commands here are just a demo
exampleDir = os.path.join(tempfile.gettempdir(), Timestamp.microsecondTimestamp())
os.mkdir(exampleDir, 0755)
try:
    sendDir = os.path.join(exampleDir, "send")
    os.mkdir(sendDir, 0755)
    exampleFile1 = os.path.join(sendDir, "example1.txt")
    with open(exampleFile1, "w") as outputFile:
        outputFile.write("this is an example\n" * 1000000)
    scpExample1 = exampleVm.scpPutCommand(fromHostPath=exampleFile1, toGuestPath="~/example1.txt")
    print "returncode=" + str(scpExample1.returncode)
    print "output=" + scpExample1.output
    scpExample2 = exampleVm.scpGetCommand(fromGuestPath="/etc/hosts", toHostPath=exampleFile1)
    print "returncode=" + str(scpExample2.returncode)
    print "output=" + scpExample2.output
    with open(exampleFile1, "r") as inputFile:
        exampleFile1Content = inputFile.read()
    print "content=\n" + exampleFile1Content
finally:
    shutil.rmtree(exampleDir)

# a good way to shut down the virtual machine,
# wait longer because apparently Ubuntu (with GUI) the first time does something important that must be
# allowed to finish before shutdown, lest the machine could be in an unusable state
exampleVm.shutdownCommand(extraSleepSeconds=60)
VMwareHypervisor.local.sleepUntilNotRunning(exampleVm.vmxFilePath, ticker=True)

# a possible modification pointed out