parameters: (ctx, mach, nicnum, nat, args)
return_statement: return (0, None)
docstring: This command shows/manages port-forwarding settings
      usage:
          nat <vm> <nicnum> <pf> [ simple tcp|udp <hostport> <guestport>]
              |[no_name tcp|udp <hostip> <hostport> <guestip> <guestport>]
              |[ex tcp|udp <pf-name> <hostip> <hostport> <guestip> <guestport>]
              |[delete <pf-name>]
docstring_summary: This command shows/manages port-forwarding settings
      usage:
          nat <vm> <nicnum> <pf> [ simple tcp|udp <hostport> <guestport>]
              |[no_name tcp|udp <hostip> <hostport> <guestip> <guestport>]
              |[ex tcp|udp <pf-name> <hostip> <hostport> <guestip> <guestport>]
              |[delete <pf-name>]
docstring_tokens: This command shows / manages port - forwarding settings usage : nat <vm > <nicnum > <pf > [ simple tcp|udp <hostport > <guestport > ] | [ no_name tcp|udp <hostip > <hostport > <guestip > <guestport > ] | [ ex tcp|udp <pf - name > <hostip > <hostport > <guestip > <guestport > ] | [ delete <pf - name > ]
function: def natPortForwarding(ctx, mach, nicnum, nat, args):
      """This command shows/manages port-forwarding settings
      usage:
          nat <vm> <nicnum> <pf> [ simple tcp|udp <hostport> <guestport>]
              |[no_name tcp|udp <hostip> <hostport> <guestip> <guestport>]
              |[ex tcp|udp <pf-name> <hostip> <hostport> <guestip> <guestport>]
              |[delete <pf-name>]
      """
      if len(args) == 1:
          # note: keys/values are swapped in defining part of the function
          proto = {0: 'udp', 1: 'tcp'}
          msg = []
          pfs = ctx['global'].getArray(nat, 'redirects')
          for pf in pfs:
              (pfnme, pfp, pfhip, pfhp, pfgip, pfgp) = str(pf).split(', ')
              msg.append('%s: %s %s:%s => %s:%s' % (pfnme, proto[int(pfp)], pfhip, pfhp, pfgip, pfgp))
          return (0, msg) # msg is array
      else:
          proto = {'udp': 0, 'tcp': 1}
          pfcmd = {
              'simple': {
                  'validate': lambda: args[1] in list(pfcmd.keys()) and args[2] in list(proto.keys()) and len(args) == 5,
                  'func':lambda: nat.addRedirect('', proto[args[2]], '', int(args[3]), '', int(args[4]))
              },
              'no_name': {
                  'validate': lambda: args[1] in list(pfcmd.keys()) and args[2] in list(proto.keys()) and len(args) == 7,
                  'func': lambda: nat.addRedirect('', proto[args[2]], args[3], int(args[4]), args[5], int(args[6]))
              },
              'ex': {
                  'validate': lambda: args[1] in list(pfcmd.keys()) and args[2] in list(proto.keys()) and len(args) == 8,
                  'func': lambda: nat.addRedirect(args[3], proto[args[2]], args[4], int(args[5]), args[6], int(args[7]))
              },
              'delete': {
                  'validate': lambda: len(args) == 3,
                  'func': lambda: nat.removeRedirect(args[2])
              }
          }
  
          if not pfcmd[args[1]]['validate']():
              print('invalid port-forwarding or args of sub command ', args[1])
              print(natPortForwarding.__doc__)
              return (1, None)
  
          a = pfcmd[args[1]]['func']()
      return (0, None)
function_tokens: def natPortForwarding ( ctx , mach , nicnum , nat , args ) : if len ( args ) == 1 : # note: keys/values are swapped in defining part of the function proto = { 0 : 'udp' , 1 : 'tcp' } msg = [ ] pfs = ctx [ 'global' ] . getArray ( nat , 'redirects' ) for pf in pfs : ( pfnme , pfp , pfhip , pfhp , pfgip , pfgp ) = str ( pf ) . split ( ', ' ) msg . append ( '%s: %s %s:%s => %s:%s' % ( pfnme , proto [ int ( pfp ) ] , pfhip , pfhp , pfgip , pfgp ) ) return ( 0 , msg ) # msg is array else : proto = { 'udp' : 0 , 'tcp' : 1 } pfcmd = { 'simple' : { 'validate' : lambda : args [ 1 ] in list ( pfcmd . keys ( ) ) and args [ 2 ] in list ( proto . keys ( ) ) and len ( args ) == 5 , 'func' : lambda : nat . addRedirect ( '' , proto [ args [ 2 ] ] , '' , int ( args [ 3 ] ) , '' , int ( args [ 4 ] ) ) } , 'no_name' : { 'validate' : lambda : args [ 1 ] in list ( pfcmd . keys ( ) ) and args [ 2 ] in list ( proto . keys ( ) ) and len ( args ) == 7 , 'func' : lambda : nat . addRedirect ( '' , proto [ args [ 2 ] ] , args [ 3 ] , int ( args [ 4 ] ) , args [ 5 ] , int ( args [ 6 ] ) ) } , 'ex' : { 'validate' : lambda : args [ 1 ] in list ( pfcmd . keys ( ) ) and args [ 2 ] in list ( proto . keys ( ) ) and len ( args ) == 8 , 'func' : lambda : nat . addRedirect ( args [ 3 ] , proto [ args [ 2 ] ] , args [ 4 ] , int ( args [ 5 ] ) , args [ 6 ] , int ( args [ 7 ] ) ) } , 'delete' : { 'validate' : lambda : len ( args ) == 3 , 'func' : lambda : nat . removeRedirect ( args [ 2 ] ) } } if not pfcmd [ args [ 1 ] ] [ 'validate' ] ( ) : print ( 'invalid port-forwarding or args of sub command ' , args [ 1 ] ) print ( natPortForwarding . __doc__ ) return ( 1 , None ) a = pfcmd [ args [ 1 ] ] [ 'func' ] ( ) return ( 0 , None )