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 | #
|
---|
9 | use strict 'vars';
|
---|
10 | use IO::Interface::Simple;
|
---|
11 | use Net::IPv4Addr qw( :all );;
|
---|
12 | use Socket;
|
---|
13 | use ProjectBuilder::Base;
|
---|
14 | use ProjectBuilder::Version;
|
---|
15 | use Data::Dumper;
|
---|
16 | use Getopt::Long qw(:config auto_abbrev no_ignore_case);
|
---|
17 | use English;
|
---|
18 |
|
---|
19 |
|
---|
20 | =pod
|
---|
21 |
|
---|
22 | =head1 NAME
|
---|
23 |
|
---|
24 | mr-net-get-config keeps track of the network configuration of the system studied
|
---|
25 |
|
---|
26 | =head1 DESCRIPTION
|
---|
27 |
|
---|
28 | mr-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 |
|
---|
32 | mr-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 |
|
---|
40 | This 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 |
|
---|
50 | Increase verbosity
|
---|
51 |
|
---|
52 | =item B<-h|--help>
|
---|
53 |
|
---|
54 | Print a brief help message and exits.
|
---|
55 |
|
---|
56 | =item B<--man>
|
---|
57 |
|
---|
58 | Prints the manual page and exits.
|
---|
59 |
|
---|
60 | =item B<-f /path/to/file>
|
---|
61 |
|
---|
62 | This is the path of the output file into which we want to store the network configuration.
|
---|
63 | By default the command prints on the standard output.
|
---|
64 |
|
---|
65 | =back
|
---|
66 |
|
---|
67 | =head1 WEB SITES
|
---|
68 |
|
---|
69 | The 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 |
|
---|
73 | For community exchanges around MondoRescue please use the list L<http://sourceforge.net/mailarchive/forum.php?forum_name=mondo-devel>
|
---|
74 |
|
---|
75 | =head1 AUTHORS
|
---|
76 |
|
---|
77 | The MondoRescue team lead by Bruno Cornec L<mailto:bruno@mondorescue.org>.
|
---|
78 |
|
---|
79 | =head1 COPYRIGHT
|
---|
80 |
|
---|
81 | MondoRescue is distributed under the GPL v2.0 license or later,
|
---|
82 | described in the file C<COPYING> included with the distribution.
|
---|
83 |
|
---|
84 | =cut
|
---|
85 |
|
---|
86 | # Global variables
|
---|
87 | my ($mrver,$mrrev) = pb_version_init();
|
---|
88 | my $appname = "mr-net-get-config";
|
---|
89 | my %opts; # CLI Options
|
---|
90 |
|
---|
91 | # Initialize the syntax string
|
---|
92 |
|
---|
93 | pb_syntax_init("$appname Version $mrver-$mrrev\n");
|
---|
94 |
|
---|
95 | GetOptions("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 |
|
---|
101 | if (defined $opts{'h'}) {
|
---|
102 | pb_syntax(0,1);
|
---|
103 | }
|
---|
104 | if (defined $opts{'man'}) {
|
---|
105 | pb_syntax(0,2);
|
---|
106 | }
|
---|
107 | if (defined $opts{'v'}) {
|
---|
108 | $pbdebug = $opts{'v'};
|
---|
109 | }
|
---|
110 | pb_log_init($pbdebug, $pbLOG);
|
---|
111 |
|
---|
112 | my $fd;
|
---|
113 | if (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
|
---|
121 | my $url = $ARGV[0];
|
---|
122 | if ($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
|
---|
131 | my ($scheme, $account, $host, $port, $path) = pb_get_uri($url);
|
---|
132 |
|
---|
133 | # If hot is a name transform into an IP
|
---|
134 | my $hostip = inet_ntoa(scalar gethostbyname($host || 'localhost'));
|
---|
135 |
|
---|
136 | # We need to loop on all if to see which one is on the same LAN as the server
|
---|
137 | for my $if (IO::Interface::Simple->interfaces) {
|
---|
138 | # Skip fake interfaces
|
---|
139 | next if (not defined $if->address);
|
---|
140 | my $netfs_ipaddr = $if->address;
|
---|
141 | my $netfs_netmask = $if->netmask;
|
---|
142 | my $netfs_broadcast = $if->broadcast;
|
---|
143 | my $netfs_hwaddr = $if->hwaddr;
|
---|
144 | pb_log(2, "interface = $if\n");
|
---|
145 | pb_log(2, "--\n");
|
---|
146 | pb_log(2, "addr = $netfs_ipaddr\n",
|
---|
147 | "broadcast = $netfs_broadcast\n",
|
---|
148 | "netmask = $netfs_netmask\n",
|
---|
149 | "dstaddr = ",$if->dstaddr,"\n",
|
---|
150 | "hwaddr = $netfs_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 (ipv4_in_network($if->address, $if->netmask, $hostip)) {
|
---|
164 | pb_log(1, "Netfs server $host is in network $netfs_ipaddr/$netfs_netmask\n");
|
---|
165 | # So generate conf file
|
---|
166 | print $fd "netfs-dev $if\n";
|
---|
167 | print $fd "netfs-proto $scheme\n";
|
---|
168 | print $fd "netfs-client-ipaddr $netfs_ipaddr\n";
|
---|
169 | print $fd "netfs-client-netmask $netfs_netmask\n";
|
---|
170 | print $fd "netfs-client-broadcast $netfs_broadcast\n";
|
---|
171 | print $fd "netfs-client-hwaddr $netfs_hwaddr\n";
|
---|
172 | print $fd "netfs-server-mount $ARGV[0]\n";
|
---|
173 | print $fd "netfs-server-user $account\n";
|
---|
174 | print $fd "netfs-server-ipaddr $hostip\n";
|
---|
175 | open(ROUTE, "/proc/net/route") || die "Unable to read /proc/net/route: $!";
|
---|
176 | while (<ROUTE>) {
|
---|
177 | my ($iface,$dest,$gw,$foo) = split(/\s+/,$_);
|
---|
178 | # Keep only default route
|
---|
179 | next if ($dest !~ /^00000000$/);
|
---|
180 | $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;
|
---|
181 | print $fd "netfs-client-defgw $gw\n";
|
---|
182 | }
|
---|
183 | close(ROUTE);
|
---|
184 | last if ($pbdebug > 1);
|
---|
185 | }
|
---|
186 | }
|
---|
187 | close($fd);
|
---|