VirtualBox

Ignore:
Timestamp:
Dec 20, 2010 6:57:06 PM (14 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
69112
Message:

VBoxShell: guest pipe cmd

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VBoxShell/vboxshell.py

    r35262 r35264  
    10611061    return 0
    10621062
    1063 def execInGuest(ctx,console,args,env,user,passwd,tmo):
     1063def execInGuest(ctx,console,args,env,user,passwd,tmo,inputPipe=None,outputPipe=None):
    10641064    if len(args) < 1:
    10651065        print "exec in guest needs at least program name"
     
    10691069    gargs = args
    10701070    print "executing %s with args %s as %s" %(args[0], gargs, user)
    1071     (progress, pid) = guest.executeProcess(args[0], 0, gargs, env, user, passwd, tmo)
     1071    flags = 0
     1072    if inputPipe is not None:
     1073        flags = 1 # set WaitForProcessStartOnly
     1074    print args[0]
     1075    (progress, pid) = guest.executeProcess(args[0], flags, gargs, env, user, passwd, tmo)
    10721076    print "executed with pid %d" %(pid)
    10731077    if pid != 0:
    10741078        try:
    10751079            while True:
     1080                if inputPipe is not None:
     1081                    indata = inputPipe(ctx)
     1082                    if indata is not None:
     1083                        write = len(indata)
     1084                        off = 0
     1085                        while write > 0:
     1086                            w = guest.setProcessInput(pid, 0, 10*1000, indata[off:])
     1087                            off = off + w
     1088                            write = write - w
     1089                    else:
     1090                        # EOF
     1091                        try:
     1092                            guest.setProcessInput(pid, 1, 10*1000, " ")
     1093                        except:
     1094                            pass
    10761095                data = guest.getProcessOutput(pid, 0, 10000, 4096)
    10771096                if data and len(data) > 0:
     
    10821101                data = guest.getProcessOutput(pid, 0, 0, 4096)
    10831102                if data and len(data) > 0:
    1084                     sys.stdout.write(data)
     1103                    if outputPipe is not None:
     1104                        outputPipe(ctx,data)
     1105                    else:
     1106                        sys.stdout.write(data)
    10851107                    continue
    10861108                if progress.completed:
     
    11551177    return 0
    11561178
    1157 def gcatCmd(ctx,args):
    1158     if (len(args) < 2):
    1159         print "usage: gcat [vmname|uuid] local_file | guestProgram, such as gcat linux /home/nike/.bashrc | sh -c 'cat >'"
     1179def readCmdPipe(ctx,hcmd):
     1180    try:
     1181        return ctx['process'].communicate()[0]
     1182    except:
     1183        return None
     1184
     1185def gpipeCmd(ctx,args):
     1186    if (len(args) < 4):
     1187        print "usage: gpipe [vmname|uuid] hostProgram guestProgram, such as gpipe linux  '/bin/uname -a' '/bin/sh -c \"/usr/bin/tee; /bin/uname -a\"'"
    11601188        return 0
    11611189    mach = argsToMach(ctx,args)
    11621190    if mach == None:
    11631191        return 0
    1164     gargs = args[2:]
     1192    hcmd = args[2]
     1193    gcmd = args[3]
     1194    (user,passwd) = getCred(ctx)
     1195    import subprocess
     1196    ctx['process'] = subprocess.Popen(split_no_quotes(hcmd), stdout=subprocess.PIPE)
     1197    gargs = split_no_quotes(gcmd)
    11651198    env = []
    1166     (user,passwd) = getCred(ctx)
    1167     gargs.insert(0, lambda ctx,mach,console,args: execInGuest(ctx,console,args,env, user, passwd, 0))
     1199    gargs.insert(0, lambda ctx,mach,console,args: execInGuest(ctx,console,args,env,user,passwd, 10000,lambda ctx:readCmdPipe(ctx, hcmd)))
    11681200    cmdExistingVm(ctx, mach, 'guestlambda', gargs)
     1201    try:
     1202        ctx['process'].terminate()
     1203    except:
     1204        pass
     1205    ctx['process'] = None
    11691206    return 0
    11701207
     
    30643101            'gexec':['Executes program in the guest', gexecCmd, 0],
    30653102            'gcopy':['Copy file to the guest', gcopyCmd, 0],
     3103            'gpipe':['Pipe between host and guest', gpipeCmd, 0],
    30663104            'alias':['Control aliases', aliasCmd, 0],
    30673105            'verbose':['Toggle verbosity', verboseCmd, 0],
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette