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

Last change on this file was 3797, checked in by Bruno Cornec, 2 months ago

Remove usage of obsolete Net::IPv4Addr

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