View dynamic IP address allocation with virsh
Hello
Is there any way to view the dynamic IP addresses allocated to guests using virsh or any other command? Thanks in advance! |
Not sure if you are looking for this, but you can view the dynamic IP range for a network:
Code:
virsh # net-list --all |
Thanks for the reply druuna but that isn't what I'm looking for.
Let me elaborate what I'm dealing with. I have a number of virtual machines which I use for testing and the default setup is to get an IP address from a DHCP server. When I fire up a virtual machine, I want to make an SSH connection to it but need to know what IP address it was allocated. Code:
virsh list --all Yes I know I can configure a static IP address but when I need another instance of SLES I will use a template and it will get its IP dynamically allocated again. Hope this helps making my question better understood! |
I'm not aware of a virsh command that can do this, maybe arp can help you:
Code:
$ arp Code:
virsh # net-dumpxml default |
Excellent suggestion!
What worked for me was: Code:
arp -i virbr0 If you don't mind, I'm keeping this open to see what other suggestions come in but I'm pleased with yours, thank you. |
The arp -b was a typo, it should be arp -n. Already fixed it in my previous post.
I don't mind if you keep this open, I'm also interested in other methods (if any). |
I wrote a little shell script to get what I wanted.
It assumes the network virbr0 but you can naturally modify it to pass the network as the first argument. It produces the output in the form IP_Address Name_of_VM Code:
#!/bin/bash |
Quote:
Although I'm not able to test your script at the moment I do have some (positive) criticism: You seem to be using grep a lot to get what you want/need. Most of the time this can be replaced by a simpler solution. Here's an examples Code:
MAC=$(grep '^[[:digit:]]' /proc/net/arp | grep virbr0 | awk '{print $4}') Code:
MAC=$( awk '/virbr0/ { print $4 }' /proc/net/arp ) |
libvirt does not hand IPs out, so you cannot query it for IPs it has given out. The proper solution for finding out about a VM configuration is to have an agent in the VM, that will report whatever you want it to, back to the host, through the hyperchannel. ovirt-guest-agent or qemu-ga are two existing options I am aware of, or you can write your own.
using arp doesn't scale - what if the VM migrated away recently (for example)? |
Quite right, dyasny. But fortunately this isn't a production system. I just need to save a few minutes a day figuring out which VM (which will ultimately be trashed once I've broken it) has which IP address.
druuna, I like that you mentioned my unnecessary use of grep so would you mind criticising this? Code:
#!/bin/bash -x |
Quote:
|
Quote:
I do believe that the virsh list command has a header that is 2 lines followed by info about the VM: Code:
No + Name + State Code:
virsh list | awk '!/Name/ {print $2}' Code:
virsh list | awk '/running/ { print $2 }' This seems to elaborate: awk '/virbr0/ { gsub(/'$MAC'/,"'$VM'"); print $1,$4}' /proc/net/arp (it is clever though): Code:
awk '/virbr0/ { printf $1 }' /proc/net/arp Code:
#!/bin/bash |
Thanks druuna!
|
All times are GMT -5. The time now is 01:57 PM. |