source: branches/3.3/MondoRescue/bin/mr-net-get-config @ 3707

Last change on this file since 3707 was 3707, checked in by bruno, 13 months ago

Update pbtest to latest version with SLES support

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