3 # Copyright (C) 2009 Red Hat Inc.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 use Sys::Guestfs::Lib qw(open_guest get_partitions resolve_windows_path
23 inspect_all_partitions inspect_partition
24 inspect_operating_systems mount_operating_system inspect_in_detail);
27 use Locale::TextDomain 'virt-tools';
33 virt-ifconfig - Display IP address of a virtual machine
37 virt-ifconfig [--options] domname
39 virt-ifconfig [--options]
43 C<virt-ifconfig domname> displays the IP address of a virtual machine
44 named C<domname>, where C<domname> is the libvirt name. If no
45 C<domname> is given, this lists out the IP addresses of all running
46 virtual machines known to libvirt.
48 For Linux guests, this only works if the C<virt-tools-guest> package
49 is installed in the guest.
69 Display version number and exit.
75 =item B<--connect URI> | B<-c URI>
77 If using libvirt, connect to the given I<URI>. If omitted, then we
78 connect to the default libvirt hypervisor.
86 Write out the results in CSV format (comma-separated values).
87 This format can be imported easily into databases and spreadsheets.
93 GetOptions ("help|?" => \$help,
94 "version" => \$version,
95 "connect|c=s" => \$uri,
98 pod2usage (1) if $help;
100 print "@PACKAGE_STRING@\n";
104 # Open the guest handle.
110 $conn = Sys::Virt->new (readonly => 1, address => $uri);
112 $conn = Sys::Virt->new (readonly => 1);
115 # Ignore inactive domains - who cares about their IP address?
116 my @doms = $conn->list_domains ();
118 my @domnames = map { $_->get_name () } @doms;
122 foreach (@domnames) {
133 print_row (__"Guest", __"Iface", __"Family", __"IP address");
144 printf ("%-20s %-7s %-7s %s\n", $domname, $iface, $family, $ip);
146 printf ("%s,%s,%s,%s\n", $domname, $iface, $family, $ip);
156 $g = open_guest (\@args, address => $uri);
158 $g = open_guest (\@args);
163 # Don't care about mountpoints. Instead, just look for a
164 # directory with one of a selection of names on one of the
165 # partitions that we found.
166 my @partitions = get_partitions ($g);
169 foreach my $partition (@partitions) {
171 $g->mount_ro ($partition, "/");
173 my @dirs = ("/var/run/virt-tools", "/run/virt-tools");
174 foreach $dir (@dirs) {
175 if ($g->is_dir ($dir)) {
176 my @names = $g->ls ($dir);
177 @names = grep { /^ip-/ } @names;
179 foreach $name (@names) {
183 my @lines = $g->read_lines ("$dir/$name");
185 if (m{^\s*inet (\S+)/\d+ }) {
186 $ips{$iface}{inet} = []
187 unless exists $ips{$iface}{inet};
188 push @{$ips{$iface}{inet}}, $1;
189 } elsif (m{^\s*inet6 (\S+)/\d+ }) {
190 $ips{$iface}{inet6} = []
191 unless exists $ips{$iface}{inet6};
192 push @{$ips{$iface}{inet6}}, $1;
203 my @ifaces = sort keys %ips;
206 foreach $iface (@ifaces) {
207 my @families = sort keys %{$ips{$iface}};
209 foreach $family (@families) {
210 foreach (@{$ips{$iface}{$family}}) {
211 print_row ($args[0], $iface, $family, $_);
217 __x("{n}: no IP address found (is guest running virt-tools?)\n",
229 L<Sys::Guestfs::Lib(3)>,
231 L<http://libguestfs.org/>.
235 Richard W.M. Jones L<http://et.redhat.com/~rjones/>
239 Copyright (C) 2009 Red Hat Inc.
241 This program is free software; you can redistribute it and/or modify
242 it under the terms of the GNU General Public License as published by
243 the Free Software Foundation; either version 2 of the License, or
244 (at your option) any later version.
246 This program is distributed in the hope that it will be useful,
247 but WITHOUT ANY WARRANTY; without even the implied warranty of
248 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
249 GNU General Public License for more details.
251 You should have received a copy of the GNU General Public License
252 along with this program; if not, write to the Free Software
253 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.