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