misc-code

=Misc. scripts, code excerpts, and scraps - Page 1= toc

bash colors
code format="bash"
 * 1) !/bin/bash
 * 2)   This file echoes a bunch of color codes to the
 * 3)   terminal to demonstrate what's available.  Each
 * 4)   line is the color code of one forground color,
 * 5)   out of 17 (default + 16 escapes), followed by a
 * 6)   test use of that color on all nine background
 * 7)   colors (default + 8 escapes).
 * 1)   colors (default + 8 escapes).

T='gYw'  # The test text

echo -e "\n                40m     41m     42m     43m\     44m     45m     46m     47m";

for FGs in '   m' '   1m' '  30m' '1;30m' '  31m' '1;31m' '  32m' \ '1;32m' ' 33m' '1;33m' '  34m' '1;34m' '  35m' '1;35m' \ ' 36m' '1;36m' '  37m' '1;37m'; do FG=${FGs// /} echo -en " $FGs \033[$FG $T  " for BG in 40m 41m 42m 43m 44m 45m 46m 47m; do echo -en "$EINS \033[$FG\033[$BG $T  \033[0m"; done echo; done echo code

check_args
See also: []

code format="bash" function USAGE { echo "" echo "USAGE: " echo "   check_args [-?dh]" echo "" echo "OPTIONS:" echo "   -d  date of logfiles (format: yyyymmdd)" echo "   -h  logfile (format: 12 or 1[2-4])" echo "   -?  this usage information" echo "" echo "EXAMPLE:" echo "   ineff.sh -d 20060801 -h 2[12]" echo "" exit $E_OPTERROR   # Exit and explain usage, if no argument(s) given. } while getopts "dux:?" OPTION; do case $OPTION in d ) echo "The date is `date`";; u ) uname -a;; x ) VAR=$OPTARG   echo "VAR is set to $VAR";; ? ) USAGE exit 0;; echo "Unimplemented option chosen." USAGE  # DEFAULT esac done
 * 1) !/bin/bash
 * 2) IF NO ARGUMENTS WERE PROVIDED
 * 1) PROCESS ARGS
 * ) echo ""

shift $(($OPTIND - 1))


 * 1)  Decrements the argument pointer so it points to next argument.
 * 2)  $1 now references the first non option item supplied on the command line
 * 3) + if one exists.

if -z "$1" then echo "NO SERVER NAME GIVEN" exit 1 else echo "info for user $1 is: "`getent passwd $1` fi code
 * 1) whatever you want to do when no parms

app_on_port
code format="bash"
 * 1) !/bin/bash


 * 1) Get the process which listens on port


 * 1) $1 is the port we are looking for

if [ $# -lt 1 ] then echo "Please provide a port number parameter for this script" echo "e.g. $0 22" exit fi

echo "Greping for your port, please be patient (CTRL+C breaks) ... "

for i in `ls /proc` do pfiles $i | grep AF_INET | grep $1 if [ $? -eq 0 ] then echo Is owned by pid $i fi done

code

checkinterfaces_sol9
code format="bash"
 * 1) !/bin/sh


 * 1) checkinterfaces
 * 2) Written by Alec Effrat, aeffrat@tribune.com, this script
 * 3)   is designed to display the plumbed interfaces and show
 * 4)   the speed, and duplex currently configured.  Please
 * 5)   e-mail me to add any interface support or if you
 * 6)   encounter any bugs.
 * 1)   e-mail me to add any interface support or if you
 * 2)   encounter any bugs.

root_check { if [ "`/usr/bin/id | /usr/bin/cut -c1-5`" != "uid=0" ] ; then echo "Interface $INTERFACE requires root user priveledges." ROOT=1 fi }
 * 1) Only the root user can run the ndd commands

speed_lookup {     case $1 in         0) SPEED="10 Mbit/s" ;;         1) SPEED="100 Mbit/s" ;; 10) SPEED="10 Mbit/s" ;;        100) SPEED="100 Mbit/s" ;; 1000) SPEED="1 Gbit/s" ;;        10000000) SPEED="10 Mbit/s" ;; 100000000) SPEED="100 Mbit/s" ;;        1000000000) SPEED="1 Gbit/s" ;; esac }
 * 1) speed lookup

duplex_lookup {     case $1 in         0) DUPLEX="link down" ;;         1) DUPLEX="half" ;; 2) DUPLEX="full" ;;        half) DUPLEX="half" ;; full) DUPLEX="full" ;;        ndd_0) DUPLEX="half" ;; ndd_1) DUPLEX="full" ;;     esac }

ndd_set { RESULT=`ndd -set /dev/$DEVICE $1` }
 * 1) ndd routine

ndd_get { VALUE=`ndd -get /dev/$DEVICE $1` }

kstat_get { set - `kstat -p "$DEVICE:$INSTANCE:$INTERFACE:$1"` VALUE=$2 }
 * 1) kstat routine

ce_get { kstat_get "link_speed" speed_lookup $VALUE kstat_get "link_duplex" duplex_lookup $VALUE }
 * 1) Interface Routines

qfe_get { kstat_get "ifspeed" speed_lookup $VALUE if [ `uname -r` = "5.8" ]; then root_check if [ ! $ROOT ]; then ndd_set "instance $INSTANCE" ndd_get "link_mode" duplex_lookup "ndd_$VALUE" fi else kstat_get "link_duplex" duplex_lookup $VALUE fi }

nge_get { kstat_get "ifspeed" speed_lookup $VALUE kstat_get "duplex" duplex_lookup $VALUE }

ge_get { root_check if [ ! $ROOT ]; then ndd_set "instance $INSTANCE" ndd_get "link_speed" speed_lookup $VALUE ndd_get "link_mode" duplex_lookup "ndd_$VALUE" fi }

dman_get { SPEED=internal DUPLEX=n/a }

interface { # Break down the interface into device and instance # call a function for each interface to set the # SPEED and DUPLEX variables appropriately

INTERFACE=$1 set - `echo "$1" | sed -e 's/[0-9]\{1,\}/ &/g;'` DEVICE=$1 INSTANCE=$2

case $DEVICE in   ce) ce_get;;    nge|bge) nge_get;; dman) dman_get;;   ge) ge_get;; qfe|hme|eri) qfe_get;;   *) echo "Unknown interface $INTERFACE";; esac }

/usr/bin/echo "Interface\tSpeed\t\tDuplex" /usr/bin/echo "-\t-\t\t--"
 * 1) Print column header information

for INTERFACE in `/usr/bin/netstat -i | /usr/bin/egrep -v "^Name|^lo0" \ | /usr/bin/awk '{print $1}' | /usr/bin/sort | /usr/bin/uniq` do unset SPEED; unset DUPLEX; unset DEVICE; unset INSTANCE interface $INTERFACE /usr/bin/echo "$INTERFACE\t\t$SPEED\t$DUPLEX" done
 * 1) Determine the speed and duplex for each live NIC on the system


 * 1) This script is submitted to BigAdmin by a user of the BigAdmin community.
 * 2) Sun Microsystems, Inc. is not responsible for the
 * 3) contents or the code enclosed.
 * 4)  Copyright Sun Microsystems, Inc. ALL RIGHTS RESERVED
 * 5) Use of this software is authorized pursuant to the
 * 6) terms of the license found at
 * 7) http://www.sun.com/bigadmin/common/berkeley_license.jsp
 * 1) Use of this software is authorized pursuant to the
 * 2) terms of the license found at
 * 3) http://www.sun.com/bigadmin/common/berkeley_license.jsp

code

Go to Page 2