Skip to content

Instantly share code, notes, and snippets.

@gilangvperdana
Last active February 23, 2024 08:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gilangvperdana/b36030688b747b42d5d04487a0f8fd0b to your computer and use it in GitHub Desktop.
Save gilangvperdana/b36030688b747b42d5d04487a0f8fd0b to your computer and use it in GitHub Desktop.
Libvirt Client

General

If you want to see KVM/QEMU on VMHost with Libvirt client you can use libvirt-clients

Install Libvirt Clients

apt install -y libvirt-clients

Remote Command

Assume UUIDVM result from virsh list & 192.168.2.13 are Server HOST. for Remote Virsh make sure TCP port 16509 are open.

  • List VM
virsh list
  • Remote List VM
virsh -c qemu+tcp://192.168.2.13/system list
  • Exec Command
virsh -c qemu:///system qemu-agent-command UUIDVM '{"execute": "guest-exec", "arguments": { "path": "apt", "arg": [ "autoremove","-y" ], "capture-output": true }}'  --pretty
  • Remote Exec Command (Linux)
virsh -c qemu+tcp://192.168.2.13/system qemu-agent-command UUIDVM '{"execute": "guest-exec", "arguments": { "path": "systemctl", "apt": [ "autoremove","-y" ], "capture-output": true }}'  --pretty
  • Remote Exec Command (Windows)
virsh -c qemu+tcp://192.168.2.13/system qemu-agent-command UUIDVM '{"execute":"guest-exec", "arguments":{"path":"cmd.exe","arg":["/c","echo 123 > C:\\123.txt"]}}'  --pretty

## Example to Set IP Address
virsh -c qemu+tcp://192.168.2.13/system qemu-agent-command UUIDVM '{"execute":"guest-exec", "arguments":{"path":"cmd.exe","arg":["/c","netsh interface ipv4 set address name=\"Ethernet 2\" static 172.16.0.15 255.255.0.0 172.16.0.1"]}}' --pretty

## Example to Set Hostname
virsh -c qemu+tcp://192.168.2.13/system qemu-agent-command UUIDVM '{"execute":"guest-exec", "arguments":{"path":"powershell.exe","arg":["Rename-Computer -NewName \"WinHK05-POC\""]}}' --pretty

## Example to Reboot Guest OS
virsh -c qemu+tcp://192.168.1.16/system qemu-agent-command 1be52c2df60c491bb8f46fb129585786 '{"execute":"guest-exec", "arguments":{"path":"shutdown.exe","arg":["/r","/t","0"]}}' --pretty

Verbose Exec Command

virsh -c qemu+tcp://192.168.2.13/system qemu-agent-command UUIDVM '{"execute": "guest-exec-status", "arguments": { "pid": 1127307 }}' --pretty
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment