source: MondoRescue/branches/3.2/MondoRescue/bin/mr-net-get-config@ 3522

Last change on this file since 3522 was 3512, checked in by Bruno Cornec, 8 years ago
  • Fix #785 by returning conf values when NFS server not on the same LAN as the client
  • Property svn:executable set to *
File size: 5.8 KB
Line 
1#!/usr/bin/perl -w
2#
3# $Id$
4# Copyright B. Cornec 2005-2014
5# Provided under the GPL v2
6#
7# Get the complete network configuration of the system
8#
9use strict 'vars';
10use IO::Interface::Simple;
11use Net::IPv4Addr qw( :all );;
12use Socket;
13use ProjectBuilder::Base;
14use ProjectBuilder::Version;
15use Data::Dumper;
16use Getopt::Long qw(:config auto_abbrev no_ignore_case);
17use English;
18
19
20=pod
21
22=head1 NAME
23
24mr-net-get-config keeps track of the network configuration of the system studied
25
26=head1 DESCRIPTION
27
28mr-net-get-config keeps track of the network configuration of the system studied wrt a network share passed as first parameter and store the result in the file given as second parameter (typically /tmp/mondorestore.cfg)
29
30=head1 SYNOPSIS
31
32mr-net-get-config [-v][-h][-f /path/to/file] proto://[user@]server/export|[user@]server:/export
33
34=head1 ARGUMENTS
35
36=over 4
37
38=item B<proto://[user@]server/export|[user@]server:/export>
39
40This is the name of the network server, followed by a ':' and the exported directory if nfs or you have to specify the protocol used (nfs, sshfs or smbfs) followed by the network server and the exported directory (without ':'). An optional user name may be provided in case it's needed.
41
42=back
43
44=head1 OPTIONS
45
46=over 4
47
48=item B<-v|--verbose>
49
50Increase verbosity
51
52=item B<-h|--help>
53
54Print a brief help message and exits.
55
56=item B<--man>
57
58Prints the manual page and exits.
59
60=item B<-f /path/to/file>
61
62This is the path of the output file into which we want to store the network configuration.
63By default the command prints on the standard output.
64
65=back
66
67=head1 WEB SITES
68
69The main Web site of the project is available at L<http://www.mondorescue.org>. Bug reports should be filled using the trac instance of the project at L<http://trac.mondorescue.org/>.
70
71=head1 USER MAILING LIST
72
73For community exchanges around MondoRescue please use the list L<http://sourceforge.net/mailarchive/forum.php?forum_name=mondo-devel>
74
75=head1 AUTHORS
76
77The MondoRescue team lead by Bruno Cornec L<mailto:bruno@mondorescue.org>.
78
79=head1 COPYRIGHT
80
81MondoRescue is distributed under the GPL v2.0 license or later,
82described in the file C<COPYING> included with the distribution.
83
84=cut
85
86# Global variables
87my ($mrver,$mrrev) = pb_version_init();
88my $appname = "mr-net-get-config";
89my %opts; # CLI Options
90
91# Initialize the syntax string
92
93pb_syntax_init("$appname Version $mrver-$mrrev\n");
94
95GetOptions("help|?|h" => \$opts{'h'},
96 "man" => \$opts{'man'},
97 "verbose|v+" => \$opts{'v'},
98 "file|f=s" => \$opts{'f'},
99) || pb_syntax(-1,0);
100
101if (defined $opts{'h'}) {
102 pb_syntax(0,1);
103}
104if (defined $opts{'man'}) {
105 pb_syntax(0,2);
106}
107if (defined $opts{'v'}) {
108 $pbdebug = $opts{'v'};
109}
110pb_log_init($pbdebug, $pbLOG);
111
112my $fd;
113if (defined $opts{'f'}) {
114 open(CFG, "> $opts{'f'}") || die "Unable to write into $opts{'f'}: $!";
115 $fd=\*CFG;
116} else {
117 $fd=\*STDOUT;
118}
119
120# Normalize URI
121my $url = $ARGV[0];
122if ($url !~ /:\/\//) {
123 # No Protocol given
124 # Protocol for sharing is NFS by default
125 # Format is without ':' then
126 $url =~ s/://;
127 $url = "nfs://".$url;
128}
129
130# Analyze URI
131my ($scheme, $account, $host, $port, $path) = pb_get_uri($url);
132
133# If host is a name transform into an IP
134my $hostip = inet_ntoa(scalar gethostbyname($host || 'localhost'));
135
136my $found = 0;
137
138# We need to loop on all if to see which one is on the same LAN as the server
139for my $if (IO::Interface::Simple->interfaces) {
140 # Skip fake interfaces
141 next if (not defined $if->address);
142 pb_log(2, "interface = $if\n");
143 pb_log(2, "--\n");
144 pb_log(2, "addr = $if->address\n",
145 "broadcast = $if->broadcast\n",
146 "netmask = $if->netmask\n",
147 "dstaddr = ",$if->dstaddr,"\n",
148 "hwaddr = $if->hwaddr\n",
149 "mtu = ",$if->mtu,"\n",
150 "metric = ",$if->metric,"\n",
151 "index = ",$if->index,"\n");
152 pb_log(2, "is running\n") if $if->is_running;
153 pb_log(2, "is broadcast\n") if $if->is_broadcast;
154 pb_log(2, "is p-to-p\n") if $if->is_pt2pt;
155 pb_log(2, "is loopback\n") if $if->is_loopback;
156 pb_log(2, "is promiscuous\n") if $if->is_promiscuous;
157 pb_log(2, "is multicast\n") if $if->is_multicast;
158 pb_log(2, "is notrailers\n") if $if->is_notrailers;
159 pb_log(2, "is noarp\n") if $if->is_noarp;
160 pb_log(2, "--\n");
161 if (ipv4_in_network($if->address, $if->netmask, $hostip)) {
162 pb_log(1, "Netfs server $host is in network $if->address/$if->netmask\n");
163 $found = 1;
164 # So generate conf file
165 my ($iface,$gw) = mr_net_get_def_gw();
166 mr_net_print_conf($fd, $if, $scheme, $if->address, $if->netmask, $if->broadcast, $if->hwaddr, $ARGV[0], $account, $hostip, $gw);
167 last if ($pbdebug > 1);
168 }
169}
170if ($found == 0) {
171 # Not found yet, so use the default GW to reach the server
172 my ($iface,$gw) = mr_net_get_def_gw();
173 my $if = IO::Interface::Simple->new($iface);
174 mr_net_print_conf($fd, $if, $scheme, $if->address, $if->netmask, $if->broadcast, $if->hwaddr, $ARGV[0], $account, $hostip, $gw);
175 }
176
177close($fd);
178
179sub mr_net_get_def_gw {
180
181my ($iface,$dest,$gw,$foo);
182
183open(ROUTE, "/proc/net/route") || die "Unable to read /proc/net/route: $!";
184while (<ROUTE>) {
185 ($iface,$dest,$gw,$foo) = split(/\s+/,$_);
186 # Keep only default route
187 next if ($dest !~ /^00000000$/);
188 $gw =~ s/([A-z0-9]{2})([A-z0-9]{2})([A-z0-9]{2})([A-z0-9]{2})/hex($4).".".hex($3).".".hex($2).".".hex($1)/eg;
189 last;
190}
191close(ROUTE);
192return($iface,$gw);
193}
194
195sub mr_net_print_conf {
196
197my ($fd, $iface, $scheme, $netfs_ipaddr, $netfs_netmask, $netfs_broadcast, $netfs_hwaddr, $netfs_server_mount, $account, $hostip, $gw) = @_;
198
199print $fd "netfs-dev $iface\n";
200print $fd "netfs-proto $scheme\n";
201print $fd "netfs-client-ipaddr $netfs_ipaddr\n";
202print $fd "netfs-client-netmask $netfs_netmask\n";
203print $fd "netfs-client-broadcast $netfs_broadcast\n";
204print $fd "netfs-client-hwaddr $netfs_hwaddr\n";
205print $fd "netfs-server-mount $ARGV[0]\n";
206print $fd "netfs-server-user $account\n";
207print $fd "netfs-server-ipaddr $hostip\n";
208print $fd "netfs-client-defgw $gw\n";
209}
Note: See TracBrowser for help on using the repository browser.