Changeset 3267 in MondoRescue
- Timestamp:
- Apr 29, 2014, 11:52:55 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3.2/MondoRescue/bin/mr-net-get-config
r3265 r3267 7 7 # Get the complete network configuration of the system 8 8 # 9 use strict ;9 use strict 'vars'; 10 10 use IO::Interface::Simple; 11 11 use Net::IPv4Addr qw( :all );; 12 use Socket; 12 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 13 19 14 20 =pod … … 24 30 =head1 SYNOPSIS 25 31 26 mr-net-get-config server-ip:/export /path/to/file32 mr-net-get-config [-v][-h][-f /path/to/file] proto://[user@]server/export|[user@]server:/export 27 33 28 34 =head1 ARGUMENTS … … 30 36 =over 4 31 37 32 =item B< server-ip:/export>38 =item B<proto://[user@]server/export|[user@]server:/export> 33 39 34 This is the IP address of the network server, followed by a ':' and the exported directory40 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. 35 41 36 = item B</path/to/file>42 =back 37 43 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 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. 39 64 40 65 =back … … 59 84 =cut 60 85 61 my $verbose = undef; 62 if ((defined $ARGV[0]) && ($ARGV[0] eq "-v")) { 63 $verbose = 1; 64 shift @ARGV; 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; 65 118 } 66 119 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 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; 69 128 } 70 129 71 open(CFG, ">> $ARGV[1]") || die "Unable to append into $ARGV[1]: $!"; 130 # Analyze URI 131 my ($scheme, $account, $host, $port, $path) = pb_get_uri($url); 72 132 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 134 my $hostip = inet_ntoa(scalar gethostbyname($host || 'localhost')); 78 135 79 136 # We need to loop on all if to see which one is on the same LAN as the server … … 85 142 my $netfs_broadcast = $if->broadcast; 86 143 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"); 110 165 # 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"; 121 176 open(ROUTE, "/proc/net/route") || die "Unable to read /proc/net/route: $!"; 122 177 while (<ROUTE>) { … … 125 180 next if ($dest !~ /^00000000$/); 126 181 $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"; 128 183 } 129 184 close(ROUTE); 130 last if ( not defined $verbose);185 last if ($pbdebug > 1); 131 186 } 132 187 } 133 close( CFG);188 close($fd);
Note:
See TracChangeset
for help on using the changeset viewer.