Changeset 3267 in MondoRescue


Ignore:
Timestamp:
Apr 29, 2014, 11:52:55 AM (10 years ago)
Author:
Bruno Cornec
Message:
  • First fully working version of mr-net-get-config
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3.2/MondoRescue/bin/mr-net-get-config

    r3265 r3267  
    77# Get the complete network configuration of the system
    88#
    9 use strict;
     9use strict 'vars';
    1010use IO::Interface::Simple;
    1111use Net::IPv4Addr qw( :all );;
     12use Socket;
    1213use ProjectBuilder::Base;
     14use ProjectBuilder::Version;
     15use Data::Dumper;
     16use Getopt::Long qw(:config auto_abbrev no_ignore_case);
     17use English;
     18
    1319
    1420=pod
     
    2430=head1 SYNOPSIS
    2531
    26 mr-net-get-config server-ip:/export /path/to/file
     32mr-net-get-config [-v][-h][-f /path/to/file] proto://[user@]server/export|[user@]server:/export
    2733
    2834=head1 ARGUMENTS
     
    3036=over 4
    3137
    32 =item B<server-ip:/export>
     38=item B<proto://[user@]server/export|[user@]server:/export>
    3339
    34 This is the IP address of the network server, followed by a ':' and the exported directory
     40This 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.
    3541
    36 =item B</path/to/file>
     42=back
    3743
    38 This is the path of the configuration file into which we want to store the network configuration
     44=head1 OPTIONS
     45
     46=over 4
     47
     48=item B<-v|--verbose>
     49
     50Increase verbosity
     51
     52=item B<-h|--help>
     53
     54Print a brief help message and exits.
     55
     56=item B<--man>
     57
     58Prints the manual page and exits.
     59
     60=item B<-f /path/to/file>
     61
     62This is the path of the output file into which we want to store the network configuration.
     63By default the command prints on the standard output.
    3964
    4065=back
     
    5984=cut
    6085
    61 my $verbose = undef;
    62 if ((defined $ARGV[0]) && ($ARGV[0] eq "-v")) {
    63     $verbose = 1;
    64     shift @ARGV;
     86# Global variables
     87my ($mrver,$mrrev) = pb_version_init();
     88my $appname = "mr-net-get-config";
     89my %opts;                   # CLI Options
     90
     91# Initialize the syntax string
     92
     93pb_syntax_init("$appname Version $mrver-$mrrev\n");
     94
     95GetOptions("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
     101if (defined $opts{'h'}) {
     102    pb_syntax(0,1);
     103}
     104if (defined $opts{'man'}) {
     105    pb_syntax(0,2);
     106}
     107if (defined $opts{'v'}) {
     108    $pbdebug = $opts{'v'};
     109}
     110pb_log_init($pbdebug, $pbLOG);
     111
     112my $fd;
     113if (defined $opts{'f'}) {
     114    open(CFG, "> $opts{'f'}") || die "Unable to write into $opts{'f'}: $!";
     115    $fd=\*CFG;
     116} else {
     117    $fd=\*STDOUT;
    65118}
    66119
    67 if ((not defined $ARGV[0]) || (not defined $ARGV[1])) {
    68     die "Syntax: mr-net-get-config [proto:]server-ip:/export /path/to/file";
     120# Normalize URI
     121my $url = $ARGV[0];
     122if ($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;
    69128}
    70129
    71 open(CFG, ">> $ARGV[1]") || die "Unable to append into $ARGV[1]: $!";
     130# Analyze URI
     131my ($scheme, $account, $host, $port, $path) = pb_get_uri($url);
    72132
    73 my ($netfs_proto,$netfs_server_ip,$netfs_server_mount);
    74 # Protocol for sharing is NFS by default
    75 $netfs_proto = "nfs";
    76 
    77 my ($scheme, $account, $host, $port, $path) = pb_get_uri($ARGV[0]);
     133# If hot is a name transform into an IP
     134my $hostip = inet_ntoa(scalar gethostbyname($host || 'localhost'));
    78135
    79136# We need to loop on all if to see which one is on the same LAN as the server
     
    85142    my $netfs_broadcast = $if->broadcast;
    86143    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";
     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");
    110165        # 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";
     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-path $path\n";
     175        print $fd "netfs-server-ipaddr $hostip\n";
    121176        open(ROUTE, "/proc/net/route") || die "Unable to read /proc/net/route: $!";
    122177        while (<ROUTE>) {
     
    125180            next if ($dest !~ /^00000000$/);
    126181            $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";
     182            print $fd "netfs-client-defgw $gw\n";
    128183        }
    129184        close(ROUTE);
    130         last if (not defined $verbose);
     185        last if ($pbdebug > 1);
    131186    }
    132187}
    133 close(CFG);
     188close($fd);
Note: See TracChangeset for help on using the changeset viewer.