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