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). This
87 format can be imported easily into databases and spreadsheets, but
88 read L</NOTE ABOUT CSV FORMAT> below.
94 GetOptions ("help|?" => \$help,
95 "version" => \$version,
96 "connect|c=s" => \$uri,
99 pod2usage (1) if $help;
101 print "@PACKAGE_STRING@\n";
105 # Open the guest handle.
111 $conn = Sys::Virt->new (readonly => 1, address => $uri);
113 $conn = Sys::Virt->new (readonly => 1);
116 # Ignore inactive domains - who cares about their IP address?
117 my @doms = $conn->list_domains ();
119 my @domnames = map { $_->get_name () } @doms;
123 foreach (@domnames) {
134 print_row (__"Guest", __"Iface", __"Family", __"IP address");
145 printf ("%-20s %-7s %-7s %s\n", $domname, $iface, $family, $ip);
147 printf ("%s,%s,%s,%s\n", $domname, $iface, $family, $ip);
157 $g = open_guest (\@args, address => $uri);
159 $g = open_guest (\@args);
164 # Don't care about mountpoints. Instead, just look for a
165 # directory with one of a selection of names on one of the
166 # partitions that we found.
167 my @partitions = get_partitions ($g);
170 foreach my $partition (@partitions) {
172 $g->mount_ro ($partition, "/");
174 my @dirs = ("/var/run/virt-tools", "/run/virt-tools");
175 foreach $dir (@dirs) {
176 if ($g->is_dir ($dir)) {
177 my @names = $g->ls ($dir);
178 @names = grep { /^ip-/ } @names;
180 foreach $name (@names) {
184 my @lines = $g->read_lines ("$dir/$name");
186 if (m{^\s*inet (\S+)/\d+ }) {
187 $ips{$iface}{inet} = []
188 unless exists $ips{$iface}{inet};
189 push @{$ips{$iface}{inet}}, $1;
190 } elsif (m{^\s*inet6 (\S+)/\d+ }) {
191 $ips{$iface}{inet6} = []
192 unless exists $ips{$iface}{inet6};
193 push @{$ips{$iface}{inet6}}, $1;
204 my @ifaces = sort keys %ips;
207 foreach $iface (@ifaces) {
208 my @families = sort keys %{$ips{$iface}};
210 foreach $family (@families) {
211 foreach (@{$ips{$iface}{$family}}) {
212 print_row ($args[0], $iface, $family, $_);
218 __x("{n}: no IP address found (is it running virt-tools-guest?)\n",
225 =head1 NOTE ABOUT CSV FORMAT
227 Comma-separated values (CSV) is a deceptive format. It I<seems> like
228 it should be easy to parse, but it is definitely not easy to parse.
230 Myth: Just split fields at commas. Reality: This does I<not> work
231 reliably. This example has two columns:
235 Myth: Read the file one line at a time. Reality: This does I<not>
236 work reliably. This example has one row:
241 For shell scripts, use C<csvtool> (L<http://merjis.com/developers/csv>
242 also packaged in major Linux distributions).
244 For other languages, use a CSV processing library (eg. C<Text::CSV>
245 for Perl or Python's built-in csv library).
247 Most spreadsheets and databases can import CSV directly.
255 L<Sys::Guestfs::Lib(3)>,
257 L<http://libguestfs.org/>.
261 Richard W.M. Jones L<http://et.redhat.com/~rjones/>
265 Copyright (C) 2009 Red Hat Inc.
267 This program is free software; you can redistribute it and/or modify
268 it under the terms of the GNU General Public License as published by
269 the Free Software Foundation; either version 2 of the License, or
270 (at your option) any later version.
272 This program is distributed in the hope that it will be useful,
273 but WITHOUT ANY WARRANTY; without even the implied warranty of
274 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
275 GNU General Public License for more details.
277 You should have received a copy of the GNU General Public License
278 along with this program; if not, write to the Free Software
279 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.