Page 1 of 1

How to enable network cap trace using API?

Posted: 21. Mar 2025, 16:19
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.

Re: How to enable network cap trace using API?

Posted: 21. Mar 2025, 19:02
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.

Re: How to enable network cap trace using API?

Posted: 24. Mar 2025, 03:39
by Bladeliu
Thanks very much!