Location: PHPKode > scripts > Cisco Config Copy > cisco-config-copy/ConfigCopy.class.php
<?PHP

/*
    Name        : ConfigCopy.class.php
    Author      : Max Schmid (max hide@address.com schmid-live .dot. de)
    Version     : 0.1a
    Date        : 13.04.2004
    Copyright (C) 2004

    This program is free software; you can redistribute it and/or modify it under the terms of
    the GNU General Public License as published by the Free Software Foundation; either version
    2 of the License, or (at your option) any later version.

    This program 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 General Public License for more details. (Found in the LICENSE file.)

    You should have received a copy of the GNU General Public License along with this program;
    if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
    MA 02111-1307 USA

    Description :
        This is a class to build a utility to backup cisco configuration files to a TFTP server using SNMP.
        You should call this class from some sort of utility that supplies the required input fields (ie. database query).

    Usage :

        $CC->function do_config_backup(<hostname>, <snmp_rw_community> ,<timeout>, <tftp_host_address>, [<directory>], [<counter>], [<debug>]);

        hostname                - The router/switch hostname or ip
        snmp_rw_community       - SNMP RW Community name
        timeout                 - SNMP Timeout in seconds
        tftp_host_address       - Hostname or IP of the TFTP Server
        directory               - Optional Directory under /tftpboot to create files in (ie. config-backup/)
        counter                 - Optional Counter to number the result lines
        debug                   - Optional set to True for debuging info

    Requirements :
        PHP 4.2.2 or greater
        UCD-snmp version: 4.2.5 or greater
        A running TFTP server

    TODO :
        Check status of copy opperations (complete, busy or failed).
        Write config to router / switch.

*/

class CiscoConfigCopy {

function do_config_backup($hostname, $community, $timeout, $tftphost, $dirname='', $cnt='', $debug=''){

        /* Array with SNMP OIDs */
        $snmpmapOID = array(    "cicsoMgmt"             => ".1.3.6.1.4.1.9.9.96",
                                "ccCopySourceFileType"  => ".1.3.6.1.4.1.9.9.96.1.1.1.1.3",
                                "ccCopyDestFileType"    => ".1.3.6.1.4.1.9.9.96.1.1.1.1.4",
                                "ccCopyServerAddress"   => ".1.3.6.1.4.1.9.9.96.1.1.1.1.5",
                                "ccCopyFileName"        => ".1.3.6.1.4.1.9.9.96.1.1.1.1.6",
                                "ccCopyEntryRowStatus"  => ".1.3.6.1.4.1.9.9.96.1.1.1.1.14",
                                "ccCopyState"           => ".1.3.6.1.4.1.9.9.96.1.1.1.1.10",
                                "IOSVersion"            => ".1.3.6.1.4.1.9.2.1.73.0");

        $now_date = date(ymdHi);                // Get date to ad to filename

        echo $cnt," - Processing host : ",$hostname;

        $str=exec("ping -c 1 -w 2 $hostname",$a,$a1);   // Check that host is pingable before we continue
        if(strlen($str)<2){
                echo " Host not reachable.\n";
                return;
        }

        $hostoptions = " -Ovq -t ".$timeout." ".$hostname." -c ".$community." ";        // Options to get IOS Version
        $sethostoptions = " "." -c ".$community." ".$hostname;                          // Option for SNMP Set commands


        $oid = $snmpmapOID['IOSVersion'];                                               // Read IOS Version to check SNMP connectivity
        $stdinput = shell_exec("snmpget ".$hostoptions." ".$oid);
        if($stdinput) {
                echo " ".$stdinput;
        } else {
                echo "Error: $stdinput\n";
                return;
        }

        $randid = rand(1, 999);                                                         // Ger random number for CC Requests

        // Build Config Copy Rows
        $oid = $snmpmapOID['ccCopyEntryRowStatus'].".".$randid;
        if($debug) {
                echo "Setting Config Copy : $oid to $hostname ...\n"."snmpset ".$sethostoptions." ".$oid."\n";
        }
        $stdinput = shell_exec("snmpset ".$sethostoptions." ".$oid." int 5");
        if($stdinput) {
                if($debug) {
                        echo "OK Response . ",$stdinput,"\n";
                }
        } else {
                echo "\n\nError: ccCopyEntryRowStatus : $stdinput \n";
                return;
        }

        // Set source file type running to running-config (4)
        $oid = $snmpmapOID['ccCopySourceFileType'].".".$randid;
        if($debug) {
                echo "Setting Source file type : $oid to $hostname ...\n"."snmpset ".$sethostoptions." ".$oid."\n";
        }
        $stdinput = shell_exec("snmpset ".$sethostoptions." ".$oid." int 4");
        if($stdinput) {
                if($debug) {
                        echo "OK Response . ",$stdinput,"\n";
                }
        } else {
                echo "Error: ccCopySourceFileType : $stdinput \n";
                return;
        }

        // Setting destination file type to Network (1)
        $oid = $snmpmapOID['ccCopyDestFileType'].".".$randid;
        if($debug) {
                echo "Setting destination file type to Network (1) : $oid to $hostname ...\n"."snmpset ".$sethostoptions." ".$oid."\n";
        }
        $stdinput = shell_exec("snmpset ".$sethostoptions." ".$oid." int 1");
        if($stdinput) {
                if($debug) {
                        echo "OK Response . ",$stdinput,"\n";
                }
        } else {
                echo "Error: ccCopyDestFileType : $stdinput \n";
                return;
        }

        // Set destination ip address (TFTP Server)
        $oid = $snmpmapOID['ccCopyServerAddress'].".".$randid;
        if($debug) {
                echo "Setting destination ipaddress : $oid to $hostname ...\n"."snmpset ".$sethostoptions." ".$oid." -a ".$tftphost."\n";
        }
        $stdinput = shell_exec("snmpset ".$sethostoptions." ".$oid." address ".$tftphost);
        if($stdinput) {
                if($debug) {
                        echo "OK Response . ",$stdinput,"\n";
                }
        } else {
                echo "Error: ccCopyServerAddress : $stdinput \n";
                return;
        }

        // Set config file name
        $oid = $snmpmapOID['ccCopyFileName'].".".$randid;
        if($debug) {
                echo "Setting destination filename : $oid to $hostname ...\n"."snmpset ".$sethostoptions." ".$oid."\n";
        }
        $stdinput = shell_exec("snmpset ".$sethostoptions." ".$oid." string ".$dirname.$hostname."-".$now_date.".cfg");
        if($stdinput) {
                if($debug) {
                        echo "OK Response . ",$stdinput,"\n";
                }
        } else {
                echo "Error: ccCopyFileName : $stdinput \n";
                return;
        }

        // Send request to start copy operation
        $oid = $snmpmapOID['ccCopyEntryRowStatus'].".".$randid;
        if($debug) {
                echo "Sending request to start copy operation : $oid to $hostname ...\n"."snmpset ".$sethostoptions." ".$oid."\n";
        }
        $stdinput = shell_exec("snmpset ".$sethostoptions." ".$oid." int 1 ");
        if($stdinput) {
                if($debug) {
                        echo "OK Response . ",$stdinput,"\n";
                }
        } else {
                echo "Error: ccCopyEntryRowStatus : $stdinput \n";
                return;
        }
}
}
?>
Return current item: Cisco Config Copy