| 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 host is a name transform into an IP
|
|---|
| 134 | my $hostip = inet_ntoa(scalar gethostbyname($host || 'localhost'));
|
|---|
| 135 |
|
|---|
| 136 | my $found = 0;
|
|---|
| 137 |
|
|---|
| 138 | # We need to loop on all if to see which one is on the same LAN as the server
|
|---|
| 139 | for 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 | }
|
|---|
| 170 | if ($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 |
|
|---|
| 177 | close($fd);
|
|---|
| 178 |
|
|---|
| 179 | sub mr_net_get_def_gw {
|
|---|
| 180 |
|
|---|
| 181 | my ($iface,$dest,$gw,$foo);
|
|---|
| 182 |
|
|---|
| 183 | open(ROUTE, "/proc/net/route") || die "Unable to read /proc/net/route: $!";
|
|---|
| 184 | while (<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 | }
|
|---|
| 191 | close(ROUTE);
|
|---|
| 192 | return($iface,$gw);
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | sub mr_net_print_conf {
|
|---|
| 196 |
|
|---|
| 197 | my ($fd, $iface, $scheme, $netfs_ipaddr, $netfs_netmask, $netfs_broadcast, $netfs_hwaddr, $netfs_server_mount, $account, $hostip, $gw) = @_;
|
|---|
| 198 |
|
|---|
| 199 | print $fd "netfs-dev $iface\n";
|
|---|
| 200 | print $fd "netfs-proto $scheme\n";
|
|---|
| 201 | print $fd "netfs-client-ipaddr $netfs_ipaddr\n";
|
|---|
| 202 | print $fd "netfs-client-netmask $netfs_netmask\n";
|
|---|
| 203 | print $fd "netfs-client-broadcast $netfs_broadcast\n";
|
|---|
| 204 | print $fd "netfs-client-hwaddr $netfs_hwaddr\n";
|
|---|
| 205 | print $fd "netfs-server-mount $ARGV[0]\n";
|
|---|
| 206 | print $fd "netfs-server-user $account\n";
|
|---|
| 207 | print $fd "netfs-server-ipaddr $hostip\n";
|
|---|
| 208 | print $fd "netfs-client-defgw $gw\n";
|
|---|
| 209 | }
|
|---|