Difference between revisions of "Con"

From Initech Technical Wiki
Jump to: navigation, search
 
Line 15: Line 15:
 
Either way selecting a device and pressing enter will login to that device by automatically figuring out the correct login binary to use.
 
Either way selecting a device and pressing enter will login to that device by automatically figuring out the correct login binary to use.
  
The code below is the bash script itself
+
The code below is the bash script itself, it requires the 'dialog' package to be installed (yum install dialog)
 
<pre>
 
<pre>
 
$ cat /usr/local/bin/con  
 
$ cat /usr/local/bin/con  

Latest revision as of 21:48, 9 May 2016

If you initialise con with no arguments it gives you a full list of all devices registered in rancid.

con

Con.png


If you initialise it with a keyword it gives you a list of the devices which name or description match the keyword

con corp

Con-corp.png


Either way selecting a device and pressing enter will login to that device by automatically figuring out the correct login binary to use.

The code below is the bash script itself, it requires the 'dialog' package to be installed (yum install dialog)

$ cat /usr/local/bin/con 
#!/bin/bash
if [ `whoami` != "rancid" ]; then
	echo "You must be the rancid user to run this, exiting"
	exit
fi
if [ ! -z "$1" ] && [ -z `echo $1 | sed -r -e "s/((1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])//g"` ]; then
	if egrep -q "\s$1\s" ~rancid/.cloginrc; then
		IP=$1
	else
		dialog --yesno "That IP address doesn't seem to exist in the .cloginrc file.  Would you like to try connecting anyway?" 0 0
                if [ "$?" != "0" ]; then
                        clear
                        exit
		else
			IP=$1
                fi

	fi
elif [ ! -z "$1" ] && egrep -q "\s$1\$" /etc/hosts ; then
        if egrep -q "\s$1\s" ~rancid/.cloginrc; then
                IP=$1
        else
                dialog --yesno "That hostname, while existing in the /etc/hosts file, doesn't seem to exist in the .cloginrc file.  Would you like to try connecting anyway?" 0 0
                if [ "$?" != "0" ]; then
                        clear
                        exit
		else
			IP=$1
                fi

        fi
else
	if [ -z "$1" ] ; then
		IP_LIST=`cat ~rancid/.cloginrc | egrep "^add\s" |awk '{print $3}' | sort | uniq | grep -v "\*"`
	else
		IP_LIST=`egrep -i -A 1 $1 ~rancid/.cloginrc | egrep "^add\s" |awk '{print $3}' | sort | uniq | grep -v "\*"`
	fi
	if [ -z "$IP_LIST" ]; then
		dialog --yesno "Couldn't find anything that matched that term, would you like a full list instead?" 0 0
		if [ "$?" = "0" ]; then
                       	$0 
		fi
		clear
		exit
	fi
	declare -a MENU_OPTIONS
	COUNT=0
	for IP in $IP_LIST
	do
		DESC=`egrep -B 1 "\s$IP\s" ~rancid/.cloginrc | egrep "^(\s*)?#" | sed -r -e "s/(\s*)?#(\s*)?//g"`
		MENU_OPTIONS=( "${MENU_OPTIONS[@]}" "${IP}" )
		MENU_OPTIONS=( "${MENU_OPTIONS[@]}" "${DESC}" )
		COUNT=$((COUNT+1))
	done
	if [ "$COUNT" -gt "1" ] ; then
		dialog --menu "The following devices match your search term of $1 please select one to connect" 0 0 0 "${MENU_OPTIONS[@]}" 2>/tmp/menu-selection
		if [ "$?" != "0" ]; then
       	 		clear
       	        	exit
       		else
			IP=$(</tmp/menu-selection)
			unlink /tmp/menu-selection
		fi
	fi
fi
for GROUP in `grep '^LIST_OF_GROUPS' /etc/rancid/rancid.conf | sed -r -e "s/LIST_OF_GROUPS(\s*)?=(\s*)?\"(.*)\"/\3/"`
do
	TYPE=`grep "^${IP}:" ~rancid/${GROUP}"/router.db" | awk -F: '{print $2}'`
	if [ -z $TYPE ]; then
		dialog --yesno "Couldn't find a manufacturer type for $IP in any router.db file, you should probably add it.  If you want i can assume that it's a Cisco and try and connect anyway?" 0 0
		if [ "$?" != "0" ]; then
			clear
			exit
		else
			clear
			clogin $IP
		fi
	else
		LOGIN_COMMAND=`egrep "\s"\'"${TYPE}"\'"\s" ~rancid/bin/rancid-fe | awk '{print $3}' |sed -r -e s/\'\(,\)\?//g | xargs -I arg1 egrep "\s*open\(INPUT,\".*login " ~rancid/bin/arg1 |awk '{print $1}' |sed -e s/open\(INPUT,\"//`
	fi
	$LOGIN_COMMAND $IP
done