grabswitchconfig.in 2.99 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#!/usr/bin/perl
#
# EMULAB-COPYRIGHT
# Copyright (c) 2005 University of Utah and the Flux Group.
# All rights reserved.
#

use lib '@prefix@/lib';
use libdb;
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
30
if ($UID && !TBAdmin($UID)) {
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 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
    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";
}