Port Forwarding python

Discussion about using the VirtualBox API, Tutorials, Samples.
Post Reply
dh233
Posts: 2
Joined: 4. Nov 2014, 08:09

Port Forwarding python

Post by dh233 »

Hi,

I am trying to configure port forwarding with NAT via python.

The API documentation contains the method "void INATNetwork::addPortForwardRule". My questions is how to tell which interface of which virtual machine this port forwarding should be configured for. So if I do the following, how can I specify the virtual machine name and the interface?:

Code: Select all

vb = virtualbox.library.INATNetwork()
vb.add_port_forward_rule(False, "DH", virtualbox.library.NATProtocol(1), "", 2222, "1.2.3.4", 22)
Thanks, dh233
noteirak
Site Moderator
Posts: 5229
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7
Contact:

Re: Port Forwarding python

Post by noteirak »

INATNetwork::addPortForwardRule take the guest IP as its 6th argument. You cannot give an interface name, only an IP.
This is because NAT rules are part of the TCP/IP realm, at layer 3. It only deals with IP and TCP/UDP ports and that's how it was implemented here.
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
dh233
Posts: 2
Joined: 4. Nov 2014, 08:09

Re: Port Forwarding python

Post by dh233 »

Thanks for your answer. This works, but what I am actually looking for is a method to configure a port forwarding rule like in the GUI.

In the GUI it is possible to assign a port forwarding rule to an interface of a VM which is configured as NAT with the following parameters:

Name: xyz
Protocol: TCP
Host IP: <blank>
Host Port: 2222
Guest IP: <blank>
Guest Port: 22

So if the above mentioned port forwarding has been configured for a machine "Ubuntu" the port forwarding works for this machine without specifying Guest IP/Host IP.
As the machine will receive its IP via DHCP I do not know the Guest IP in advance, which makes it impossible to specify it in the python method.

Actually I have written a short method which will use VBoxMange via 'commands' to configure the port forwarding now:

Code: Select all

def addPortForward(VmName, IfNumber, PfName, Proto, HostPort, GuestPort):
    commandString = "VBoxManage modifyvm " + VmName + " --natpf" + str(IfNumber) + " \"'" + str(PfName) + "'," + str(Proto) + ",," + str(HostPort) + ",," + str(GuestPort)+'"'
    print commandString
    status, output = commands.getstatusoutput(commandString)
    print status
    print output
I was wondering if the same can be done directly via python API, but it does not seem like that. I also experimented with 'setextradata' but this did not work too well for me.
noteirak
Site Moderator
Posts: 5229
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7
Contact:

Re: Port Forwarding python

Post by noteirak »

NAT and NAT network are two different things. When in NAT, there is only a single machine, therefore the IP can be guessed or known.
With NAT network, you can have several machines, so you can't just guess the IP you'll use. Nothing stops you to gather the IP set on a specific interface using the guest additions tho!
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
Post Reply