grabswitchconfig.in 3.75 KB
Newer Older
1 2
#!/usr/bin/perl
#
3
# Copyright (c) 2012-2016 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
23 24 25 26
#

use lib '@prefix@/lib';
use libdb;
27 28

# Optional alternate version of libraries.
29
use lib '@prefix@/lib/snmpit';
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
use snmpit_lib;
use snmpit_cisco;

# This could probably be a configure variable, but I don't see a reason to make
# it on yet
my $TFTPDIR = "/tftpboot";

use strict;
use English;

sub usage() {
    print "Usage: grapbswitchconfig <switch> <filename> [server]\n";
    print "    <switch> must be (for now) a Cisco switch in the testbed DB\n";
    print "    <filename> must be a file that does not exist in $TFTPDIR\n";
    print "    [server] is the name or IP of the TFTP server to dump to\n";
    print "    server defaults to the IP address on the interface for our\n";
    print "    first hop to switch\n";
    exit 1;
}

Robert Ricci's avatar
Robert Ricci committed
50
if ($UID && !TBAdmin($UID)) {
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    die "*** $0:\n" .
        "    Sorry, only admins get to run this script\n";
}

if (@ARGV != 2 && @ARGV != 3) {
    usage();
}

my ($switch, $filename, $server) = @ARGV;

#
# Make sure they gave us a Cisco
#
my $type = getDeviceType($switch);
if ($type !~ /cisco/ && $type !~ /catalyst/) {
    die "*** $0:\n" .
        "    Sorry, only Cisco switches are support right now (type $type)\n";
}

#
# Make sure the destintion file is TFTP-accessible
#
if ($filename !~ /^$TFTPDIR/) {
    die "*** $0:\n" .
        "    The file must reside in $TFTPDIR\n";
}

#
# Try to set up an SNMP connection to the switch
#
my $sess = new snmpit_cisco($switch);
if (!$sess) {
    die "*** $0:\n" .
        "    Failed to connect to $switch\n";
}

#
# Try to determine the server to use if not given - since we often talk to
# swtiches on 'private' interfaces, we can't just use 'hostname'.
#
if (!$server) {
    #
    # Use 'route get' to find out what interface we use to talk to this thing
    #
    my $iface;
    foreach my $line (`route get $switch`) {
        if ($line =~ /interface:\s+(\w+)/) {
            $iface = $1;
        }
    }
    if (!$iface) {
        die "*** $0\n" .
            "    Unable to determine interface to $switch\n";
    }

    #
    # Use ifconfig to get that interface's IP address
    #
    foreach my $line (`ifconfig $iface`) {
        if ($line =~ /inet\s+(\d+\.\d+\.\d+\.\d+)\s+/) {
            $server = $1;
        }
    }
    if (!$server) {
        die "*** $0\n" .
            "    Unable to determine IP address for $iface\n";
    }

}

#
# Try to make the file to dump config to
#
if (-e $filename) {
    die "*** $0:\n" .
        "    File $filename already exists\n";
}

if (!open(DUMPFILE,">$filename")) {
    die "*** $0:\n" .
        "    Failed to open $filename: $!\n";
}
close DUMPFILE;

if (system "chmod 777 $filename") {
    die "*** $0:\n" .
        "    Failed to chmod $filename: $!\n";
}


#
# Do the dump!
#
if (!$sess->writeConfigTFTP($server,$filename)) {
    die "*** $0:\n" .
        "    Dump to $filename on $server failed\n";
}

#
# Chmod the file back to something reasonable
#
if (system "chmod 640 $filename") {
    die "*** $0:\n" .
        "    Failed to chmod $filename: $!\n";
}