How to enable network cap trace using API?

Discussion about using the VirtualBox API, Tutorials, Samples.
Post Reply
Bladeliu
Posts: 3
Joined: 21. Mar 2025, 16:12

How to enable network cap trace using API?

Post by Bladeliu »

Hello,

I'm using VirtualBox API to manage VMs. It's possible to enable network trace using API, just as command line? Invoking command line with API captures invalid traffic.

Command line:
VBoxManage modifyvm myvm --nictrace2 on --nictracefile2 ~/test.pcap

Example code

Code: Select all

vb = virtualbox.VirtualBox()
vm = vb.find_machine("myvm")          
snap = vm.find_snapshot("snapshot1")
session1 = virtualbox.Session()        

vm.create_session(session=session1)           
restoring = session1.machine.restore_snapshot(snap) 
restoring.wait_for_completion(300000)       

session1.unlock_machine()

# add network  trace code using API here
# If invoking command line "VBoxManage modifyvm myvm --nictrace2 on --nictracefile2 ~/test.pcap" , the pcap generated is invalid - Wireshark shows nothing.

... laugh_vm_process()
Thanks a lot.
paulson
Oracle Corporation
Posts: 31
Joined: 6. Jun 2019, 20:16

Re: How to enable network cap trace using API?

Post by paulson »

> I'm using VirtualBox API to manage VMs. It's possible to enable network
> trace using API, just as command line? Invoking command line with API
> captures invalid traffic.
Yes, it is possible to enable network tracing for a virtual network
card using the VirtualBox API. In Python it would look like the following:

Code: Select all

from vboxapi import VirtualBoxManager
virtualBoxManager = VirtualBoxManager("XPCOM", '');
vbox = virtualBoxManager.getVirtualBox();
machine = vbox.findMachine('vm-name');
session = virtualBoxManager.getSessionObject(vbox);
machine.lockMachine(session, 2);
sessionMachine = session.machine;
nic = sessionMachine.getNetworkAdapter(2);
nic.traceEnabled = True
nic.traceFile = '~/test.pcap'
sessionMachine.saveSettings()
session.unlockMachine()
This is the equivalent of the 'VBoxManage modifyvm' command you
referenced. I tried this out with the latest VirtualBox 7.1 release and
the test.pcap file is readable using snoop or wireshark for me.
Bladeliu
Posts: 3
Joined: 21. Mar 2025, 16:12

Re: How to enable network cap trace using API?

Post by Bladeliu »

Thanks very much!
Post Reply