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

Last change on this file since 3265 was 3265, checked in by Bruno Cornec, 10 years ago
  • Add a new binary mr-net-get-config to coput the network configuration and generate the appropriate portion of the configuration file
  • Property svn:executable set to *
File size: 4.1 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;
10use IO::Interface::Simple;
11use Net::IPv4Addr qw( :all );;
12use ProjectBuilder::Base;
13
14=pod
15
16=head1 NAME
17
18mr-net-get-config keeps track of the network configuration of the system studied
19
20=head1 DESCRIPTION
21
22mr-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)
23
24=head1 SYNOPSIS
25
26mr-net-get-config server-ip:/export /path/to/file
27
28=head1 ARGUMENTS
29
30=over 4
31
32=item B<server-ip:/export>
33
34This is the IP address of the network server, followed by a ':' and the exported directory
35
36=item B</path/to/file>
37
38This is the path of the configuration file into which we want to store the network configuration
39
40=back
41
42=head1 WEB SITES
43
44The 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/>.
45
46=head1 USER MAILING LIST
47
48For community exchanges around MondoRescue please use the list L<http://sourceforge.net/mailarchive/forum.php?forum_name=mondo-devel>
49
50=head1 AUTHORS
51
52The MondoRescue team lead by Bruno Cornec L<mailto:bruno@mondorescue.org>.
53
54=head1 COPYRIGHT
55
56MondoRescue is distributed under the GPL v2.0 license or later,
57described in the file C<COPYING> included with the distribution.
58
59=cut
60
61my $verbose = undef;
62if ((defined $ARGV[0]) && ($ARGV[0] eq "-v")) {
63 $verbose = 1;
64 shift @ARGV;
65}
66
67if ((not defined $ARGV[0]) || (not defined $ARGV[1])) {
68 die "Syntax: mr-net-get-config [proto:]server-ip:/export /path/to/file";
69}
70
71open(CFG, ">> $ARGV[1]") || die "Unable to append into $ARGV[1]: $!";
72
73my ($netfs_proto,$netfs_server_ip,$netfs_server_mount);
74# Protocol for sharing is NFS by default
75$netfs_proto = "nfs";
76
77my ($scheme, $account, $host, $port, $path) = pb_get_uri($ARGV[0]);
78
79# We need to loop on all if to see which one is on the same LAN as the server
80for my $if (IO::Interface::Simple->interfaces) {
81 # Skip fake interfaces
82 next if (not defined $if->address);
83 my $netfs_ipaddr = $if->address;
84 my $netfs_netmask = $if->netmask;
85 my $netfs_broadcast = $if->broadcast;
86 my $netfs_hwaddr = $if->hwaddr;
87 if (defined $verbose) {
88 print "interface = $if\n";
89 print "--\n";
90 print "addr = ",$netfs_ipaddr,"\n",
91 "broadcast = ",$netfs_broadcast,"\n",
92 "netmask = ",$netfs_netmask,"\n",
93 "dstaddr = ",$if->dstaddr,"\n",
94 "hwaddr = ",$netfs_hwaddr,"\n",
95 "mtu = ",$if->mtu,"\n",
96 "metric = ",$if->metric,"\n",
97 "index = ",$if->index,"\n";
98 print "is running\n" if $if->is_running;
99 print "is broadcast\n" if $if->is_broadcast;
100 print "is p-to-p\n" if $if->is_pt2pt;
101 print "is loopback\n" if $if->is_loopback;
102 print "is promiscuous\n" if $if->is_promiscuous;
103 print "is multicast\n" if $if->is_multicast;
104 print "is notrailers\n" if $if->is_notrailers;
105 print "is noarp\n" if $if->is_noarp;
106 print "--\n";
107 }
108 if (ipv4_in_network($if->address, $if->netmask, $host)) {
109 print "Netfs server $host is in network $netfs_ipaddr/$netfs_netmask\n";
110 # So generate conf file
111 print CFG "netfs-dev $if\n";
112 print CFG "netfs-proto $scheme\n";
113 print CFG "netfs-client-ipaddr $netfs_ipaddr\n";
114 print CFG "netfs-client-netmask $netfs_netmask\n";
115 print CFG "netfs-client-broadcast $netfs_broadcast\n";
116 print CFG "netfs-client-hwaddr $netfs_hwaddr\n";
117 print CFG "netfs-server-mount $ARGV[0]\n";
118 print CFG "netfs-server-user $account\n";
119 print CFG "netfs-server-path $path\n";
120 print CFG "netfs-server-ipaddr $host\n";
121 open(ROUTE, "/proc/net/route") || die "Unable to read /proc/net/route: $!";
122 while (<ROUTE>) {
123 my ($iface,$dest,$gw,$foo) = split(/\s+/,$_);
124 # Keep only default route
125 next if ($dest !~ /^00000000$/);
126 $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;
127 print CFG "netfs-client-defgw $gw\n";
128 }
129 close(ROUTE);
130 last if (not defined $verbose);
131 }
132}
133close(CFG);
Note: See TracBrowser for help on using the repository browser.