VirtualBox

Changeset 29754 in vbox for trunk/src/VBox


Ignore:
Timestamp:
May 24, 2010 5:45:13 AM (15 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
61946
Message:

VBoxShell: adds nic commands.

File:
1 edited

Legend:

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

    r29556 r29754  
    24482448            msg ='{0} nic{1} {2}: {3}'.format(mach.name, nicnum, func, r)
    24492449            print msg
     2450    return 0
     2451
     2452def nicSwitchOnOff(adapter, attr, args):
     2453    if len(args) == 1:
     2454        yesno = {0: 'off', 1: 'on'}
     2455        r = yesno[int(adapter.__getattr__(attr))]
     2456        return (0, r)
     2457    else:
     2458        yesno = {'off' : 0, 'on' : 1}
     2459        if args[1] not in yesno:
     2460            print '%s isn\'t acceptable, please choose %s' % (args[1], yesno.keys())
     2461            return (1, None)
     2462        adapter.__setattr__(attr, yesno[args[1]])
     2463    return (0, None)
     2464
     2465def nicTraceSubCmd(ctx, vm, nicnum, adapter, args):
     2466    '''
     2467    usage: nic <vm> <nicnum> trace [on|off [file]]
     2468    '''
     2469    (rc, r) = nicSwitchOnOff(adapter, 'traceEnabled', args)
     2470    if len(args) == 1 and rc == 0:
     2471        r = '%s file:%s' % (r, adapter.traceFile)
     2472        return (0, r)
     2473    elif len(args) == 3 and rc == 0:
     2474        adapter.traceFile = args[2]
     2475    return (0, None)
     2476
     2477def nicLineSpeedSubCmd(ctx, vm, nicnum, adapter, args):
     2478    if len(args) == 1:
     2479        r = '%d kbps'%(adapter.lineSpeed)
     2480        return (0, r)
     2481    else:
     2482        if not args[1].isdigit():
     2483            print '%s isn\'t a number'.format(args[1])
     2484            print (1, None)
     2485        adapter.lineSpeed = int(args[1])
     2486    return (0, None)
     2487
     2488def nicCableSubCmd(ctx, vm, nicnum, adapter, args):
     2489    '''
     2490    usage: nic <vm> <nicnum> cable [on|off]
     2491    '''
     2492    return nicSwitchOnOff(adapter, 'cableConnected', args)
     2493
     2494def nicEnableSubCmd(ctx, vm, nicnum, adapter, args):
     2495    '''
     2496    usage: nic <vm> <nicnum> enable [on|off]
     2497    '''
     2498    return nicSwitchOnOff(adapter, 'enabled', args)
     2499
     2500def nicTypeSubCmd(ctx, vm, nicnum, adapter, args):
     2501    '''
     2502    usage: nic <vm> <nicnum> type [Am79c970A|Am79c970A|I82540EM|I82545EM|I82543GC|Virtio]
     2503    '''
     2504    if len(args) == 1:
     2505        nictypes = ctx['ifaces'].all_values('NetworkAdapterType')
     2506        for n in nictypes.keys():
     2507            if str(adapter.adapterType) == str(nictypes[n]):
     2508                return (0, str(n))
     2509        return (1, None)
     2510    else:
     2511        nictypes = ctx['ifaces'].all_values('NetworkAdapterType')
     2512        if args[1] not in nictypes.keys():
     2513            print '%s not in acceptable values (%s)' % (args[1], nictypes.keys())
     2514            return (1, None)
     2515        adapter.adapterType = nictypes[args[1]]
     2516    return (0, None)
     2517
     2518def nicAttachmentSubCmd(ctx, vm, nicnum, adapter, args):
     2519    '''
     2520    usage: nic <vm> <nicnum> attachment [Null|NAT|Bridged <interface>|Internal <name>|HostOnly <interface>]
     2521    '''
     2522    if len(args) == 1:
     2523        nicAttachmentType = {
     2524            ctx['global'].constants.NetworkAttachmentType_Null: ('Null', ''),
     2525            ctx['global'].constants.NetworkAttachmentType_NAT: ('NAT', ''),
     2526            ctx['global'].constants.NetworkAttachmentType_Bridged: ('Bridged', adapter.hostInterface),
     2527            ctx['global'].constants.NetworkAttachmentType_Internal: ('Internal', adapter.internalNetwork),
     2528            ctx['global'].constants.NetworkAttachmentType_HostOnly: ('HostOnly', adapter.hostInterface),
     2529            #ctx['global'].constants.NetworkAttachmentType_VDE: ('VDE', adapter.VDENetwork)
     2530        }
     2531        import types
     2532        if type(adapter.attachmentType) != types.IntType:
     2533            t = str(adapter.attachmentType)
     2534        else:
     2535            t = adapter.attachmentType
     2536        (r, p) = nicAttachmentType[t]
     2537        return (0, 'attachment:{0}, name:{1}'.format(r, p))
     2538    else:
     2539        nicAttachmentType = {
     2540            'Null': {
     2541                'v': lambda: len(args) == 2,
     2542                'p': lambda: 'do nothing',
     2543                'f': lambda: adapter.detach()},
     2544            'NAT': {
     2545                'v': lambda: len(args) == 2,
     2546                'p': lambda: 'do nothing',
     2547                'f': lambda: adapter.attachToNAT()},
     2548            'Bridged': {
     2549                'v': lambda: len(args) == 3,
     2550                'p': lambda: adapter.__setattr__('hostInterface', args[2]),
     2551                'f': lambda: adapter.attachToBridgedInterface()},
     2552            'Internal': {
     2553                'v': lambda: len(args) == 3,
     2554                'p': lambda: adapter.__setattr__('internalNetwork', args[2]),
     2555                'f': lambda: adapter.attachToInternalNetwork()},
     2556            'HostOnly': {
     2557                'v': lambda: len(args) == 2,
     2558                'p': lambda: adapter.__setattr__('hostInterface', args[2]),
     2559                'f': lambda: adapter.attachToHostOnlyInterface()},
     2560            'VDE': {
     2561                'v': lambda: len(args) == 3,
     2562                'p': lambda: adapter.__setattr__('VDENetwork', args[2]),
     2563                'f': lambda: adapter.attachToVDE()}
     2564        }
     2565        if args[1] not in nicAttachmentType.keys():
     2566            print '{0} not in acceptable values ({1})'.format(args[1], nicAttachmentType.keys())
     2567            return (1, None)
     2568        if not nicAttachmentType[args[1]]['v']():
     2569            print nicAttachmentType.__doc__
     2570            return (1, None)
     2571        nicAttachmentType[args[1]]['p']()
     2572        nicAttachmentType[args[1]]['f']()
     2573    return (0, None)
     2574
     2575def nicCmd(ctx, args):
     2576    '''
     2577    This command to manage network adapters
     2578    usage: nic <vm> <nicnum> <cmd> <cmd-args>
     2579    where cmd : attachment, trace, linespeed, cable, enable, type
     2580    '''
     2581    # 'command name':{'runtime': is_callable_at_runtime, 'op': function_name}
     2582    niccomand = {
     2583        'attachment': nicAttachmentSubCmd,
     2584        'trace':  nicTraceSubCmd,
     2585        'linespeed': nicLineSpeedSubCmd,
     2586        'cable': nicCableSubCmd,
     2587        'enable': nicEnableSubCmd,
     2588        'type': nicTypeSubCmd
     2589    }
     2590    if     args[1] == 'help' \
     2591        or len(args) < 2 \
     2592        or (len(args) > 2 and args[3] not in niccomand):
     2593        if len(args) == 3 \
     2594           and args[2] in niccomand:
     2595            print niccomand[args[2]].__doc__
     2596        else:
     2597            print nicCmd.__doc__
     2598        return 0
     2599
     2600    vm = ctx['argsToMach'](args)
     2601    if vm is None:
     2602        print 'please specify vm'
     2603        return 0
     2604     
     2605    if    len(args) < 3 \
     2606       or not args[2].isdigit() \
     2607       or int(args[2]) not in range(0, ctx['vb'].systemProperties.networkAdapterCount):
     2608            print 'please specify adapter num %d isn\'t in range [0-%d]'%(args[2], ctx['vb'].systemProperties.networkAdapterCount)
     2609            return 0
     2610    nicnum = int(args[2])
     2611    cmdargs = args[3:]
     2612    func = args[3]
     2613    session = None
     2614    session = ctx['global'].openMachineSession(vm.id)
     2615    vm = session.machine
     2616    adapter = vm.getNetworkAdapter(nicnum)
     2617    (rc, report) = niccomand[func](ctx, vm, nicnum, adapter, cmdargs)
     2618    if rc == 0:
     2619            vm.saveSettings()
     2620    if report is not None:
     2621        print '%s nic %d %s: %s' % (vm.name, nicnum, args[3], report)
     2622    session.close()
    24502623    return 0
    24512624
     
    25262699            'snapshot':['VM snapshot manipulation, snapshot help for more info', snapshotCmd, 0],
    25272700            'nat':['NAT manipulation, nat help for more info', natCmd, 0],
     2701            'nic' : ['network adapter management', nicCmd, 0],
    25282702            }
    25292703
Note: See TracChangeset for help on using the changeset viewer.

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