Getrtr

From NippAero

Jump to: navigation, search

PHP Version

I re-wrote this again using PHP. I needed it in PHP for another project I was working on.

<?php

//
// Get Router PHP Version
// Mike Nipp 2007
// Using the CISCO-CONFIG-COPY-MIB

$rand = rand(1, 500);
$host = $argv[1];
$community = "private";
$tftpserver = "10.2.8.6";

touch("/tftpboot/$host");
chmod("/tftpboot/$host", 0777);

//#Set the copy protocol to tftp using "ccCopyProtocol" OID
snmpset($host, $community, ".1.3.6.1.4.1.9.9.96.1.1.1.1.2.$rand", "i", 1);

//Set the type of file to copy from using "ccCopySourceFileType" OID
//3 = startup-config, 4 = running-config
snmpset($host, $community, ".1.3.6.1.4.1.9.9.96.1.1.1.1.3.$rand", "i", 4);

//Set the type of file to copy to using "ccCopyDestFileType" OID
snmpset($host, $community, ".1.3.6.1.4.1.9.9.96.1.1.1.1.4.$rand", "i", 1);

//Set the IP address of the TFTP Server using "ccCopyServerAddress" OID
snmpset($host, $community, ".1.3.6.1.4.1.9.9.96.1.1.1.1.5.$rand", "a", $tftpserver);

//Set the name of the file to write to using "ccCopyFileName" OID
snmpset($host, $community, ".1.3.6.1.4.1.9.9.96.1.1.1.1.6.$rand", "s", $host);

//Set the status of the table to actice using "ccCopyEntryRowStatus" OID
snmpset($host, $community, ".1.3.6.1.4.1.9.9.96.1.1.1.1.14.$rand", "i", 1);

// Wait for copy operation to complete
$status = "NO DATA";
while ($status != "INTEGER: 3") {
$status = snmpget($host, $community, ".1.3.6.1.4.1.9.9.96.1.1.1.1.10.$rand");
//echo "$status\n";

}

$config = "/tftpboot/$host";

if (!file_exists($config)) {
$size = "NO DATA";
exit();
}

// Open the file pointer to the config file
$handle = fopen($config, 'r');

// Read the config file contents
$contents = fread($handle, filesize($config));
$crap = explode("\n", $contents);
        foreach ($crap as $line)
print "$line\n";

// Clean up and delete file
fclose($handle);
unlink($config);

?>


New Shell Version

I re-wrote the getrtr script to use the CISCO-CONFIG-COPY-MIB.

#!/bin/sh
## SNMP COPY RUNNING-CONFIGURATION TO TFTP SERVER
## CISCO-CONFIG-COPY-MIB
##


#myhostname=`hostname`
#myip=`host $myhostname |awk '{print $4}'`

tftpserver="10.2.4.9"
randomnumber="$RANDOM"

for duh in $*
do
#echo Getting $duh.... #For troubleshooting
#echo Random Number is $randomnumber #For troubleshooting
touch /tftpboot/$duh
chmod 777 /tftpboot/$duh

#Set the copy protocol to tftp using "ccCopyProtocol" OID
snmpset -c private -v 1 $duh .1.3.6.1.4.1.9.9.96.1.1.1.1.2.$randomnumber integer 1 > /dev/null

#Set the type of file to copy from using "ccCopySourceFileType" OID
#3 = startup-config, 4 = running-config
snmpset -c private -v 1 $duh  .1.3.6.1.4.1.9.9.96.1.1.1.1.3.$randomnumber integer 4 > /dev/null

#Set the type of file to copy to using "ccCopyDestFileType" OID
snmpset -c private -v 1 $duh  .1.3.6.1.4.1.9.9.96.1.1.1.1.4.$randomnumber integer 1 > /dev/null

#Set the IP address of the TFTP Server using "ccCopyServerAddress" OID
snmpset -c private -v 1 $duh .1.3.6.1.4.1.9.9.96.1.1.1.1.5.$randomnumber a $tftpserver > /dev/null

#Set the name of the file to write to using "ccCopyFileName" OID
snmpset -c private -v 1 $duh .1.3.6.1.4.1.9.9.96.1.1.1.1.6.$randomnumber string $duh > /dev/null

#Set the status of the table to actice using "ccCopyEntryRowStatus" OID
snmpset -c private -v 1 $duh .1.3.6.1.4.1.9.9.96.1.1.1.1.14.$randomnumber integer 1 > /dev/null
sleep 6

cat /tftpboot/$duh
rm /tftpboot/$duh

done

Original Version

This is the original script by Leon that I changed for dynamic host assignment.

#!/bin/sh
myhostname=`hostname`
myip=`host $myhostname |awk '{print $4}'`

for duh in $*
do
touch /tftpboot/$duh
chmod 777 /tftpboot/$duh
snmpset -v 1 -c private $duh  .1.3.6.1.4.1.9.2.1.55.$myip s $duh
cat /tftpboot/$duh
#rm /tftpboot/$duh
done
Personal tools