[libvirt] Host IP Address

Hi, I was trying to figure out a way to get the IP Address of the host, in which a particular VM is running using the LibvirtAPI. For Eg.; If I have a VM object, then I would like to fetch the IP Address of the host in which this VM is running, programatically. Is there a way to do this using the API? Please guide me, if there is some other way to do this also. Thanks and Regards Sagar Barve

Anno domini 2010 IKI-サガル バルウェ scripsit: Hi!
I was trying to figure out a way to get the IP Address of the host, in which a particular VM is running using the LibvirtAPI. For Eg.; If I have a VM object, then I would like to fetch the IP Address of the host in which this VM is running, programatically. Is there a way to do this using the API?
Please guide me, if there is some other way to do this also.
Did I get you right, that you have a 'virDomainPtr' as basis and want a mapping from that to the IP? I guess one way could be to get the virConnectionPtr conn_ptr = virDomainGetConnect (domain_ptr); And then query the hostname virConnectGetHostname (conn_ptr); or the uri virConnectGetURI (conn_ptr); and resolve the hostname or extract the IP/host from the URI and resolve that. HTH Ciao Max -- Gib Dein Bestes. Dann übertriff Dich selbst!

Thanks Max, Thanks for such a quick reply.
Did I get you right, that you have a 'virDomainPtr' as basis and want a mapping from that to the IP? Yes. Thats what I want to do.
I guess one way could be to get the virConnectionPtr conn_ptr = virDomainGetConnect (domain_ptr);
And then query the hostname virConnectGetHostname (conn_ptr); or the uri virConnectGetURI (conn_ptr);
and resolve the hostname or extract the IP/host from the URI and resolve that.
Yes, I thought of this too. But I was hoping that the LibvirtAPI has some way of giving me the host IP Address directly, instead of resolving it myself. But then again, how would I resolve the IP Address from the hostname? Can you please guide me on this. Thanks and Regards Sagar Barve ----- Original Message ----- From: "Maximilian Wilhelm" <max@rfc2324.org> To: <libvir-list@redhat.com> Sent: Wednesday, June 23, 2010 3:26 PM Subject: Re: [libvirt] Host IP Address
Anno domini 2010 IKI-サガル バルウェ scripsit:
Hi!
I was trying to figure out a way to get the IP Address of the host, in which a particular VM is running using the LibvirtAPI. For Eg.; If I have a VM object, then I would like to fetch the IP Address of the host in which this VM is running, programatically. Is there a way to do this using the API?
Please guide me, if there is some other way to do this also.
Did I get you right, that you have a 'virDomainPtr' as basis and want a mapping from that to the IP?
I guess one way could be to get the virConnectionPtr conn_ptr = virDomainGetConnect (domain_ptr);
And then query the hostname virConnectGetHostname (conn_ptr); or the uri virConnectGetURI (conn_ptr);
and resolve the hostname or extract the IP/host from the URI and resolve that.
HTH Ciao Max -- Gib Dein Bestes. Dann übertriff Dich selbst!
-- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list

Anno domini 2010 IKI-サガル バルウェ scripsit: Hi!
Thanks for such a quick reply.
No problem :)
Did I get you right, that you have a 'virDomainPtr' as basis and want a mapping from that to the IP? Yes. Thats what I want to do.
I guess one way could be to get the virConnectionPtr conn_ptr = virDomainGetConnect (domain_ptr);
And then query the hostname virConnectGetHostname (conn_ptr); or the uri virConnectGetURI (conn_ptr);
and resolve the hostname or extract the IP/host from the URI and resolve that.
Yes, I thought of this too. But I was hoping that the LibvirtAPI has some way of giving me the host IP Address directly, instead of resolving it myself.
I don't know of any and I wouldn't know which IP to choose if there were multiple.
But then again, how would I resolve the IP Address from the hostname? Can you please guide me on this.
In short: struct hostent *hostent; hostent = gethostbyname (char *hostname); if (hostent) { for (i = 0; hostent->h_addr_list[i]; i++) printf ("%s, ", hostent->h_addr_list[i]); printf ("\n"); } HTH Ciao Max -- "Ich habe eher spontan mitbestellt, ohne genau zu wissen, was ich da gerade kaufe. Immer noch besser, als Schuhe zu kaufen." -- uschebit zum Thema SheevaPlug

Thanks Max,
But then again, how would I resolve the IP Address from the hostname? Can you please guide me on this.
In short:
struct hostent *hostent; hostent = gethostbyname (char *hostname); if (hostent) { for (i = 0; hostent->h_addr_list[i]; i++) printf ("%s, ", hostent->h_addr_list[i]); printf ("\n"); }
I got the idea of what to do. Thanks. Thanks and Regards Sagar Barve ----- Original Message ----- From: "Maximilian Wilhelm" <max@rfc2324.org> To: <libvir-list@redhat.com> Sent: Wednesday, June 23, 2010 4:23 PM Subject: Re: [libvirt] Host IP Address
Anno domini 2010 IKI-サガル バルウェ scripsit:
Hi!
Thanks for such a quick reply.
No problem :)
Did I get you right, that you have a 'virDomainPtr' as basis and want a mapping from that to the IP? Yes. Thats what I want to do.
I guess one way could be to get the virConnectionPtr conn_ptr = virDomainGetConnect (domain_ptr);
And then query the hostname virConnectGetHostname (conn_ptr); or the uri virConnectGetURI (conn_ptr);
and resolve the hostname or extract the IP/host from the URI and resolve that.
Yes, I thought of this too. But I was hoping that the LibvirtAPI has some way of giving me the host IP Address directly, instead of resolving it myself.
I don't know of any and I wouldn't know which IP to choose if there were multiple.
But then again, how would I resolve the IP Address from the hostname? Can you please guide me on this.
In short:
struct hostent *hostent; hostent = gethostbyname (char *hostname); if (hostent) { for (i = 0; hostent->h_addr_list[i]; i++) printf ("%s, ", hostent->h_addr_list[i]); printf ("\n"); }
HTH Ciao Max -- "Ich habe eher spontan mitbestellt, ohne genau zu wissen, was ich da gerade kaufe. Immer noch besser, als Schuhe zu kaufen." -- uschebit zum Thema SheevaPlug
-- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list

On Wed, Jun 23, 2010 at 09:23:36AM +0200, Maximilian Wilhelm wrote:
Anno domini 2010 IKI-サガル バルウェ scripsit:
Hi!
Thanks for such a quick reply.
No problem :)
Did I get you right, that you have a 'virDomainPtr' as basis and want a mapping from that to the IP? Yes. Thats what I want to do.
I guess one way could be to get the virConnectionPtr conn_ptr = virDomainGetConnect (domain_ptr);
And then query the hostname virConnectGetHostname (conn_ptr); or the uri virConnectGetURI (conn_ptr);
and resolve the hostname or extract the IP/host from the URI and resolve that.
Yes, I thought of this too. But I was hoping that the LibvirtAPI has some way of giving me the host IP Address directly, instead of resolving it myself.
I don't know of any and I wouldn't know which IP to choose if there were multiple.
But then again, how would I resolve the IP Address from the hostname? Can you please guide me on this.
In short:
struct hostent *hostent; hostent = gethostbyname (char *hostname); if (hostent) { for (i = 0; hostent->h_addr_list[i]; i++) printf ("%s, ", hostent->h_addr_list[i]); printf ("\n"); }
Never use gethostbyname in any new code. It is long since obsolete, not thread safe and is hardcoded to only work with IPv4. There is a good exmaple of how to use the modern replacemenbt, getaddrinfo(), to resolve hostnames here: http://people.redhat.com/drepper/userapi-ipv6.html Daniel -- |: Red Hat, Engineering, London -o- http://people.redhat.com/berrange/ :| |: http://libvirt.org -o- http://virt-manager.org -o- http://deltacloud.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: GnuPG: 7D3B9505 -o- F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|

Anno domini 2010 Daniel P. Berrange scripsit:
Never use gethostbyname in any new code. It is long since obsolete, not thread safe and is hardcoded to only work with IPv4.
There is a good exmaple of how to use the modern replacemenbt, getaddrinfo(), to resolve hostnames here:
Oh, sorry, I wasn't aware of that. Thank you for the hint. Ciao Max -- Alles sollte so einfach wie möglich gemacht sein. Aber nicht einfacher. (Einstein)

Never use gethostbyname in any new code. It is long since obsolete, not thread safe and is hardcoded to only work with IPv4.
There is a good exmaple of how to use the modern replacemenbt, getaddrinfo(), to resolve hostnames here:
Oh, sorry, I wasn't aware of that. Thank you for the hint.
Ciao Max
Thanks Daniel and Max for all the help. Thanks and Regards Sagar Barve ----- Original Message ----- From: "Maximilian Wilhelm" <max@rfc2324.org> To: <libvir-list@redhat.com> Sent: Friday, June 25, 2010 12:55 AM Subject: Re: [libvirt] Host IP Address
Anno domini 2010 Daniel P. Berrange scripsit:
Never use gethostbyname in any new code. It is long since obsolete, not thread safe and is hardcoded to only work with IPv4.
There is a good exmaple of how to use the modern replacemenbt, getaddrinfo(), to resolve hostnames here:
Oh, sorry, I wasn't aware of that. Thank you for the hint.
Ciao Max -- Alles sollte so einfach wie möglich gemacht sein. Aber nicht einfacher. (Einstein)
-- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list
participants (3)
-
Daniel P. Berrange
-
IKI-サガル バルウェ
-
Maximilian Wilhelm