Created
December 6, 2012 15:24
-
-
Save jandahl/4225291 to your computer and use it in GitHub Desktop.
router.db controls for rancid: Remove existing line or add new, using snmp to fetch hostname if not presented by user
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
## | |
## updaterancid.sh v.2012-12-06 | |
## | |
## jd@nianet.dk | |
## | |
if [[ $(id -un) != "rancid" ]]; then | |
echo "Must be rancid user to run this script." | |
exit 1 | |
fi | |
date=$(date +%Y-%m-%d-kl-%H-%m) | |
ip=$1 | |
function aboutMe { | |
echo -e "Rancid Updater v. 2012-12-06" | |
echo -e "\n\tOpdater linje i Rancid baseret paa \033[1;31mIP-adresse\033[1;0m" | |
echo -e "\n\tEksempel:\n\t\t`basename $0` \033[1;31mIP-adresse\033[1;0m\n" | |
echo -e "\n\tCPEens \033[1;31mhostname\033[0m hentes efter behov automatisk vha. snmp" | |
echo -e "\n\tHvis \033[1;31mhostname\033[0m skal forceres eller CPE er nede skriver du" | |
echo -e "\n\t\t`basename $0` \033[1;31mIP-adresse hostname\033[0m\n\n" | |
exit 0 | |
} | |
function wantToDestroyLine { | |
removedline=`sed -n "/^$ip:/p" /usr/local/var/rancid/rancid-cpe/router.db | sed -e "s/:cisco:up:/ /"` | |
echo -e "\n\tFound \033[1;31m$removedline\033[1;0m\n" | |
read -n 1 -p " Would you like me to erase that line (y/j/n)? " yno | |
case $yno in | |
[yYjJ] ) | |
cp /usr/local/var/rancid/rancid-cpe/router.db /usr/local/var/rancid/rancid-cpe/router.db.$date | |
sed -e "/^$ip:/d" /usr/local/var/rancid/rancid-cpe/router.db.$date > /usr/local/var/rancid/rancid-cpe/router.db | |
echo -e "\n\n\t\033[1;31m$removedline\033[1;0m sent to the farm.\n" | |
exit 0 | |
;; | |
[nN] ) | |
echo -e "\n\tFine! Have it your way!\n\tDoing nothing.\n" | |
exit 0 | |
;; | |
*) | |
echo -e "\n\tWhatchutalkinboutwillis?\n\n\tInvalid input detected.\n\n\t\033[1;31mDoing nothing.\033[1;0m\n" | |
exit 1 | |
;; | |
esac | |
} | |
function wantToCreateLine { | |
string="$ip:cisco:up:$hostname" | |
cp /usr/local/var/rancid/rancid-cpe/router.db /usr/local/var/rancid/rancid-cpe/router.db.$date | |
echo $string >> /usr/local/var/rancid/rancid-cpe/router.db | |
echo -e "\n\tAdded \033[1;31m$string\033[0m\n" | |
exit 0 | |
} | |
function checkForLine { | |
if `grep -q "^$ip\:" /usr/local/var/rancid/rancid-cpe/router.db` | |
then | |
wantToDestroyLine | |
fi | |
wantToCreateLine | |
} | |
function getHostNameViaSNMP { | |
echo -e "\nTrying to fetch hostname from the CPE...\n" | |
if [ ! `ping -c 1 $ip > /dev/null` ] | |
then | |
echo -e "\n\tUnfortunately, \033[1;31m$ip\033[0m does not seem to be responsive at the moment," | |
echo -e "\ttherefore I cannot ascertain \033[1;31mhostname\033[0m via SNMP.\n" | |
echo -e "\n\tYou can still add the entry using the following syntax:" | |
echo -e "\n\t\t`basename $0` \033[1;31mIP-adresse hostname\033[0m\n\n" | |
exit 1 | |
fi | |
hostname=$(snmpwalk -v 2c -c nianetcpemgmtread $ip .1.3.6.1.4.1.9.2.1.3 | awk -F\" '{ print $2 }') | |
} | |
case $# in | |
0) | |
aboutMe | |
;; | |
1) | |
getHostNameViaSNMP | |
checkForLine | |
;; | |
2) | |
cpehostname=$2 | |
checkForLine | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bloody hell, måske skal jeg lave et simplere script uden $# in 1 og så lave en wrapper på ny-cm1 der evt. laver snmpwalk og så giver det videre til cm1