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);
25 use Locale::TextDomain 'virt-tools';
31 virt-ifconfig - Display IP address of a virtual machine
35 virt-ifconfig [--options] domname
37 virt-ifconfig [--options]
41 C<virt-ifconfig domname> displays the IP address of a virtual machine
42 named C<domname>, where C<domname> is the libvirt name. If no
43 C<domname> is given, this lists out the IP addresses of all running
44 virtual machines known to libvirt.
46 For Linux guests, this only works if the C<virt-tools-guest> package
47 is installed in the guest.
67 Display version number and exit.
73 =item B<--connect URI> | B<-c URI>
75 If using libvirt, connect to the given I<URI>. If omitted, then we
76 connect to the default libvirt hypervisor.
84 Write out the results in CSV format (comma-separated values). This
85 format can be imported easily into databases and spreadsheets, but
86 read L</NOTE ABOUT CSV FORMAT> below.
92 GetOptions ("help|?" => \$help,
93 "version" => \$version,
94 "connect|c=s" => \$uri,
97 pod2usage (1) if $help;
99 print "@PACKAGE_STRING@\n";
103 # Open the guest handle.
109 $conn = Sys::Virt->new (readonly => 1, address => $uri);
111 $conn = Sys::Virt->new (readonly => 1);
114 # Ignore inactive domains - who cares about their IP address?
115 my @doms = $conn->list_domains ();
117 my @domnames = map { $_->get_name () } @doms;
121 foreach (@domnames) {
132 print_row (__"Guest", __"Iface", __"Family", __"IP address");
143 printf ("%-20s %-7s %-7s %s\n", $domname, $iface, $family, $ip);
145 printf ("%s,%s,%s,%s\n", $domname, $iface, $family, $ip);
155 $g = open_guest (\@args, address => $uri);
157 $g = open_guest (\@args);
162 # Don't care about mountpoints. Instead, just look for a
163 # directory with one of a selection of names on one of the
164 # partitions that we found.
165 my @partitions = get_partitions ($g);
168 foreach my $partition (@partitions) {
170 $g->mount_ro ($partition, "/");
172 my @dirs = ("/var/lib/virt-tools", "/lib/virt-tools");
173 foreach $dir (@dirs) {
174 if ($g->is_dir ($dir)) {
175 my @names = $g->ls ($dir);
176 @names = grep { /^ip-/ } @names;
178 foreach $name (@names) {
182 my @lines = $g->read_lines ("$dir/$name");
184 if (m{^\s*inet (\S+)/\d+ }) {
185 $ips{$iface}{inet} = []
186 unless exists $ips{$iface}{inet};
187 push @{$ips{$iface}{inet}}, $1;
188 } elsif (m{^\s*inet6 (\S+)/\d+ }) {
189 $ips{$iface}{inet6} = []
190 unless exists $ips{$iface}{inet6};
191 push @{$ips{$iface}{inet6}}, $1;
202 my @ifaces = sort keys %ips;
205 foreach $iface (@ifaces) {
206 my @families = sort keys %{$ips{$iface}};
208 foreach $family (@families) {
209 foreach (@{$ips{$iface}{$family}}) {
210 print_row ($args[0], $iface, $family, $_);
216 __x("{n}: no IP address found (is it running virt-tools-guest?)\n",
223 =head1 NOTE ABOUT CSV FORMAT
225 Comma-separated values (CSV) is a deceptive format. It I<seems> like
226 it should be easy to parse, but it is definitely not easy to parse.
228 Myth: Just split fields at commas. Reality: This does I<not> work
229 reliably. This example has two columns:
233 Myth: Read the file one line at a time. Reality: This does I<not>
234 work reliably. This example has one row:
239 For shell scripts, use C<csvtool> (L<http://merjis.com/developers/csv>
240 also packaged in major Linux distributions).
242 For other languages, use a CSV processing library (eg. C<Text::CSV>
243 for Perl or Python's built-in csv library).
245 Most spreadsheets and databases can import CSV directly.
253 L<Sys::Guestfs::Lib(3)>,
255 L<http://libguestfs.org/>.
259 Richard W.M. Jones L<http://et.redhat.com/~rjones/>
263 Copyright (C) 2009 Red Hat Inc.
265 This program is free software; you can redistribute it and/or modify
266 it under the terms of the GNU General Public License as published by
267 the Free Software Foundation; either version 2 of the License, or
268 (at your option) any later version.
270 This program is distributed in the hope that it will be useful,
271 but WITHOUT ANY WARRANTY; without even the implied warranty of
272 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
273 GNU General Public License for more details.
275 You should have received a copy of the GNU General Public License
276 along with this program; if not, write to the Free Software
277 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.