Month: March 2013

Terminal Colors with Bash

The following is a script I would use for testing. I used it for viewing colors and layout; it’s good for enhancing script feed-back when the script is interactive.

#!/bin/bash
#---------------------------------
#
# Script Name: color_run.sh
# Version: 0.1.1
# Created on: 3/2/2011 Jeff Neuffer Jr
# Updated on: $Id: color_run.sh 105 2013-03-17 14:50:53Z jneuffer $
# Purpose: Display terminal colors
#
#---------------------------------


txtblk='33[0;30m' # Black - Regular
txtred='33[0;31m' # Red
txtgrn='33[0;32m' # Green
txtylw='33[0;33m' # Yellow
txtblu='33[0;34m' # Blue
txtpur='33[0;35m' # Purple
txtcyn='33[0;36m' # Cyan
txtwht='33[0;37m' # White

bldblk='33[1;30m' # Black - Bold
bldred='33[1;31m' # Red
bldgrn='33[1;32m' # Green
bldylw='33[1;33m' # Yellow
bldblu='33[1;34m' # Blue
bldpur='33[1;35m' # Purple
bldcyn='33[1;36m' # Cyan
bldwht='33[1;37m' # White

unkblk='33[4;30m' # Black - Underline
undred='33[4;31m' # Red
undgrn='33[4;32m' # Green
undylw='33[4;33m' # Yellow
undblu='33[4;34m' # Blue
undpur='33[4;35m' # Purple
undcyn='33[4;36m' # Cyan
undwht='33[4;37m' # White

bakblk='33[40m'   # Black - Background
bakred='33[41m'   # Red
badgrn='33[42m'   # Green
bakylw='33[43m'   # Yellow
bakblu='33[44m'   # Blue
bakpur='33[45m'   # Purple
bakcyn='33[46m'   # Cyan
bakwht='33[47m'   # White

txtrst='33[0m'    # Text Reset

echo;echo;
echo -e "33[0;30;47m[         Backup VM Disks           ]33[0m"
echo -e "   33[0;30;47m[ Unregister HDD          ]33[0m"
echo -e "   33[0;30;47m[ Cloning HDD Start       ]33[0m"
echo -e "   33[0;30;47m[ Re-attaching HDD to VM  ]33[0m"
echo; echo;
echo -e "33[0;30;43m[               Disk Usage                   ]33[0m"
df -h

echo; echo;

echo; echo;
exit

Here is what the script produced,
terminal_colors_with_bash_1

There is a better way to view the colors, here is the output from a script from the “Bash-Prompt-HOWTO” [Ref 1]
terminal_colors_with_bash_2

References:
[Ref 1]: http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html

JUNOS, Scripting and SSH-AGENT

In two previous blog entries I talked about leveraging ssh-agent with scripting. Below is an example script I use to run different commands on Junos based routers.

This particular script is menu driven from the terminal prompt. Beyond the menu the script is non-interactive. It is plausible to write a script that will request user input based on some condition learned by the script; so for this reason scripts can be interactive unlike the below script.

The way I see it, your imagination and vision is the only limit to what you can achieve in computing.

So, please reference the two following blog entries to setup the ssh-agent for accessing a Junos based platform.
JUNOS, SSH-KEY Authentication (the how)
Leveraging ssh-agent and Junos based routers (the why)

To run scripts against a Junos based router, the ssh-agent is not required; however using an ssh-agent is convenient and a time saver.

I have several lab routers that I work with and not all of them are always on and running. The following script does a ping across a range of IP addresses to check if they send an ICMP echo-reply. If they do, the script will log into the router and issue a command.

Here is the script.

#!/usr/bin/env bash
################################################
# File  Name: junos_request_system_halt.sh
# Created On: 11/17/2012 Jeff Neuffer Jr
# Updated On: $Id: junos_request_system_halt.sh 102 2012-11-21 15:28:21Z jneuffer $
# 
# Note: Purpose is to sweep a range of IP addresses and issue a command
#
#
################################################
#
SCRIPT_VERSION='0.2.1';

# Lab hosts range from .200 - .254
HOSTS=( 
	172.16.155.200 
	172.16.155.201 
	172.16.155.202 
	172.16.155.203 
	172.16.155.204 
	172.16.155.205 
	172.16.155.206 
	172.16.155.207 
	172.16.155.208
	172.16.155.209
	172.16.155.210 
	172.16.155.211 
	172.16.155.212 
	172.16.155.213 
	172.16.155.214 
	172.16.155.215 
	172.16.155.216 
	172.16.155.217 
	172.16.155.218 
	172.16.155.219 
	172.16.155.220 
	172.16.155.254 
	);
#HOSTS=( 172.16.155.200 172.16.155.201 172.16.155.202 );
#HOSTS=( 172.16.155.200 );

run_command()
{
	echo;echo;
	for a in ${HOSTS[@]}; do
		# the following ping is used to determine which VM is "alive"
        ping -c 1 -t 1 -n $a > /dev/null 2>&1;        
        if [[ $? -gt 0 ]]; then 
        	echo "$a <-- No Reply" 
        else
        	echo "$a <-- Alive"
        	if [[ "$the_command" == "jhalt" ]]; then
        		ssh $a "request system halt"
        		sleep 8
        	elif [[ "$the_command" == "jreboot" ]]; then
        		ssh $a "request system reboot"
        		sleep 58
        	else
        		echo;echo "nothing matches ($the_command)";echo
        	fi
        	sleep 2
        fi
	done
	echo;echo;
}

continue=yes
while [ "$continue" = yes ]
do
        echo; echo;
        echo "Send Junos Command ($SCRIPT_VERSION)";
        echo "--------------------------------------------";
        echo;     
        echo "  1) Halt   (request system halt)";
        echo "  2) Reboot (request system reboot)";    
                
        echo "  9) Quit";        
        read -p "Select an option [1-7,9]: " ANSWER

        if [[ "$ANSWER" == 1 ]]
        then
                the_command="jhalt";
                run_command;                
                continue=yes;
        elif [[ "$ANSWER" == 2 ]]
        then
                the_command="jreboot";
                run_command;                
                continue=yes;
        
        elif [[ "$ANSWER" == [9Qq] ]]
        then                
                echo "Quiting"; echo;echo;
                continue=no;                
        else
                clear;
                continue=yes;
        fi        
done

This is the output from the script. Notice that for each login to a router a password is not requested.

junos scripting and ssh-agent 1