1 | #!/usr/bin/python
|
---|
2 |
|
---|
3 | """
|
---|
4 | Copyright (C) 2009-2020 Oracle Corporation
|
---|
5 |
|
---|
6 | This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
7 | available from http://www.virtualbox.org. This file is free software;
|
---|
8 | you can redistribute it and/or modify it under the terms of the GNU
|
---|
9 | General Public License (GPL) as published by the Free Software
|
---|
10 | Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
11 | VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
12 | hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
13 | """
|
---|
14 |
|
---|
15 | #################################################################################
|
---|
16 | # This program is a port-forwarding configurator supposed to simplify
|
---|
17 | # port-forwarding for NAT users
|
---|
18 | # > python VBoxPortForwarding.py --vm winXP -a 1 -p TCP -l 8080 -g 80 -P www
|
---|
19 | # generates sequence of API calls, equivalent to:
|
---|
20 | # > VBoxManage setextradata "winXP"
|
---|
21 | # "VBoxInternal/Devices/pcnet/0/LUN#0/Config/www/Protocol" TCP
|
---|
22 | # > VBoxManage setextradata "winXP"
|
---|
23 | # "VBoxInternal/Devices/pcnet/0/LUN#0/Config/www/GuestPort" 80
|
---|
24 | # > VBoxManage setextradata "winXP"
|
---|
25 | # "VBoxInternal/Devices/pcnet/0/LUN#0/Config/www/HostPort" 8080
|
---|
26 | ################################################################################
|
---|
27 |
|
---|
28 | import os,sys
|
---|
29 | from vboxapi import VirtualBoxManager
|
---|
30 | import optparse
|
---|
31 |
|
---|
32 | class OptionParser (optparse.OptionParser):
|
---|
33 | def check_required(self, opt):
|
---|
34 | option = self.get_option(opt)
|
---|
35 | if option.type == "string" and getattr(self.values, option.dest) != None:
|
---|
36 | return True
|
---|
37 | if option.type == "int" and getattr(self.values, option.dest) != -1:
|
---|
38 | return True
|
---|
39 | return False
|
---|
40 |
|
---|
41 | def generate_profile_name(proto, host_port, guest_port):
|
---|
42 | return proto + '_' + str(host_port) + '_' + str(guest_port)
|
---|
43 |
|
---|
44 | def main(argv):
|
---|
45 |
|
---|
46 | usage = "usage: %prog --vm winXP -a 1 -p TCP -l 8080 -g 80 -P www"
|
---|
47 | parser = OptionParser(usage=usage)
|
---|
48 | parser.add_option("-V", "--vm", action="store", dest="vmname", type="string",
|
---|
49 | help="Name or UID of VM to operate on", default=None)
|
---|
50 | parser.add_option("-P", "--profile", dest="profile", type="string",
|
---|
51 | default=None)
|
---|
52 | parser.add_option("-p", "--ip-proto", dest="proto", type="string",
|
---|
53 | default=None)
|
---|
54 | parser.add_option("-l", "--host-port", dest="host_port", type="int",
|
---|
55 | default = -1)
|
---|
56 | parser.add_option("-g", "--guest-port", dest="guest_port", type="int",
|
---|
57 | default = -1)
|
---|
58 | parser.add_option("-a", "--adapter", dest="adapter", type="int",
|
---|
59 | default=-1)
|
---|
60 | (options,args) = parser.parse_args(argv)
|
---|
61 |
|
---|
62 | if (not (parser.check_required("-V") or parser.check_required("-G"))):
|
---|
63 | parser.error("please define --vm or --guid option")
|
---|
64 | if (not parser.check_required("-p")):
|
---|
65 | parser.error("please define -p or --ip-proto option")
|
---|
66 | if (not parser.check_required("-l")):
|
---|
67 | parser.error("please define -l or --host_port option")
|
---|
68 | if (not parser.check_required("-g")):
|
---|
69 | parser.error("please define -g or --guest_port option")
|
---|
70 | if (not parser.check_required("-a")):
|
---|
71 | parser.error("please define -a or --adapter option")
|
---|
72 |
|
---|
73 | man = VirtualBoxManager(None, None)
|
---|
74 | vb = man.getVirtualBox()
|
---|
75 | print "VirtualBox version: %s" % vb.version,
|
---|
76 | print "r%s" % vb.revision
|
---|
77 |
|
---|
78 | vm = None
|
---|
79 | try:
|
---|
80 | if options.vmname != None:
|
---|
81 | vm = vb.findMachine(options.vmname)
|
---|
82 | elif options.vmname != None:
|
---|
83 | vm = vb.getMachine(options.vmname)
|
---|
84 | except:
|
---|
85 | print "can't find VM by name or UID:",options.vmname
|
---|
86 | del man
|
---|
87 | return
|
---|
88 |
|
---|
89 | print "vm found: %s [%s]" % (vm.name, vm.id)
|
---|
90 |
|
---|
91 | session = man.openMachineSession(vm.id)
|
---|
92 | vm = session.machine
|
---|
93 |
|
---|
94 | adapter = vm.getNetworkAdapter(options.adapter)
|
---|
95 |
|
---|
96 | if adapter.enabled == False:
|
---|
97 | print "adapter(%d) is disabled" % adapter.slot
|
---|
98 | del man
|
---|
99 | return
|
---|
100 |
|
---|
101 | name = None
|
---|
102 | if (adapter.adapterType == man.constants.NetworkAdapterType_Null):
|
---|
103 | print "none adapter type detected"
|
---|
104 | return -1
|
---|
105 | elif (adapter.adapterType == man.constants.NetworkAdapterType_Am79C970A):
|
---|
106 | name = "pcnet"
|
---|
107 | elif (adapter.adapterType == man.constants.NetworkAdapterType_Am79C973):
|
---|
108 | name = "pcnet"
|
---|
109 | elif (adapter.adapterType == man.constants.NetworkAdapterType_I82540EM):
|
---|
110 | name = "e1000"
|
---|
111 | elif (adapter.adapterType == man.constants.NetworkAdapterType_I82545EM):
|
---|
112 | name = "e1000"
|
---|
113 | elif (adapter.adapterType == man.constants.NetworkAdapterType_I82543GC):
|
---|
114 | name = "e1000"
|
---|
115 | print "adapter of '%s' type has been detected" % name
|
---|
116 |
|
---|
117 | profile_name = options.profile
|
---|
118 | if profile_name == None:
|
---|
119 | profile_name = generate_profile_name(options.proto.upper(),
|
---|
120 | options.host_port,
|
---|
121 | options.guest_port)
|
---|
122 | config = "VBoxInternal/Devices/" + name + "/"
|
---|
123 | config = config + str(adapter.slot) +"/LUN#0/Config/" + profile_name
|
---|
124 | proto = config + "/Protocol"
|
---|
125 | host_port = config + "/HostPort"
|
---|
126 | guest_port = config + "/GuestPort"
|
---|
127 |
|
---|
128 | vm.setExtraData(proto, options.proto.upper())
|
---|
129 | vm.setExtraData(host_port, str(options.host_port))
|
---|
130 | vm.setExtraData(guest_port, str(options.guest_port))
|
---|
131 |
|
---|
132 |
|
---|
133 | vm.saveSettings()
|
---|
134 | man.closeMachineSession(session)
|
---|
135 |
|
---|
136 | del man
|
---|
137 |
|
---|
138 | if __name__ == "__main__":
|
---|
139 | main(sys.argv)
|
---|