/Main_Page

::You must have ninja focus to complete your mission::NinjaFocus::

Bash

Views:


I've been amassing a range of little script-lets designed to make my life as a Linux sys admin easier.
I thought now would be a good time to start plonking them somewhere.
They're all for my own use - don't use one and then complain if it breaks something ;D

Russ

See Also: scripts

more Linux

Contents

find_usernames

I whacked this script together as I kept getting requests from academics to set up various systems/servers (mysql databases and subversion repositories in particular) for lab use and sending me a huge list of student names rather than usernames.
It simply takes a list of students from $1, cleans it up using sed and then greps a passwd file for each name in turn.
All our users are currently in NIS, hence the 'ypcat passwd'.

#!/bin/bash
# Grabs usernames from nis passwd file when given a list of names #
# Russ Cox Feb '09 #
TMP="/tmp/find_usernames.tmp"

# split $L variable into first name and last name then search
srch ()
{
 F1=`echo $L |awk '{print $1}'`
 F2=`echo $L |awk '{print $2}'`
 ypcat passwd |grep -i $F1 |grep -i $F2
 if [ $? -eq 1 ]
  then echo "$F1 $F2 not found" 
 fi
}

if [ -z $1 ]
 then
  echo "No input file given"
  echo "Usage: find_usernames 'input_file'"
 else
  # if tempfile exists, remove it
  if [ -e $TMP ]
   then rm $TMP
  fi
  # clean up input file
  sed s/[.,]/\\n/g $1 | sed 's/^[ \t]*//;s/[ \t]*$//;/^$/d;/[0-9]/d' > $TMP
  # run srch function - clean up output
  while read L; do srch; done < $TMP | awk -F: '{print $5,$1}'
fi


mysql_mod

A basic loop to mass create databases - gets used quite a bit

#!/bin/bash
clear
echo " Mass mysql database creation "
if [ -z $1 ]
then
 echo "No student list specified"
else

dbhost=xxxxx.ee.surrey.ac.uk
dbuser=root

echo "dbhost = $dbhost"
echo "dbuser = $dbuser"
echo "enter password for $dbuser"
read -s passwd

mysql -u $dbuser -h $dbhost -p$passwd -e "create database xxxx; flush privileges;"

for U in `awk '{print $3}' $1`; do
echo "Setting user permissions on xxxx to $U on $dbhost"
mysql -u $dbuser -h $dbhost -p$passwd -e "grant usage on *.* to '$U'@'%' identified by '$U'; grant select, insert, update, delete, create, drop, references, index, alter on xxxx.* to '$U'@'%'; flush privileges;";
done

fi

strip_comments

Really useful 'ronseal' script - takes a text config file and strips out any commented lines

#!/bin/bash
cat $1 | sed -e 's/^ *#.*$//g' -e '/^ *$/d'

check_tapes

This still isn't quite ready - I've not had time to work on it for a little while.
It's designed to make the checking of the numerous amanda backup heads and tape libraries in FEPS a little easier - it is a bit of a mess :P

#!/bin/bash
######################
#### check_tapes #####
##Russ Cox-Nov 2008###
######################

# ./check_tapes -p to print output to screen instead of $OUTPUT 

function set_vars()
{
# backup-head specific config 
case `hostname` in
 epsback01.* ) TPDEV=2 CONF=cvpA ;;
 epsback02.* ) TPDEV=1 CONF=cvpB ;;
 epsback03.* ) TPDEV=1 CONF=cvpC ;;
 epsback05.* ) TPDEV=1 CONF=ibcA ;;
 epsback06.* ) TPDEV=3 CONF=atiC ;;
 epsback07.* ) TPDEV=1 CONF=eeB ;;
 epsback08.* ) TPDEV=2 CONF=csA ;;
 * ) echo "Host is not epsback01-08" ; exit 2 ;;
esac

OUTPUT="/vol/scratch2/rc0019/tapes_output" #.`date +%d%m%y`"
TMP="/tmp/check_tapes.tmp"

}

function delete_tmpfile()
{
# is tmp file there? delete it
if [ -e "$TMP" ]
 then
  rm $TMP
fi
}

function check_loader()
{

echo "@=======================$(hostname |cut -d. -f1)================================@"

# strip out anything other than tape names from mtx and amnexttape output
LD=`/usr/sbin/mtx -f /dev/sg$TPDEV status |egrep -o $CONF[0-9]+` 
NX=`/root/bin/amnexttape $CONF 17 |egrep -o $CONF[0-9]+`

# list current tapes with a slot number 

echo "Tapes in $(hostname) loader"

mtx -f /dev/sg$TPDEV status | egrep $CONF[0-9]+ |cut -d" " -f9-10

for a in $LD
 do
  echo "$a" >> $TMP 
 done
#cat -n $TMP

# Compare the next 17 tapes needed with tapes in loader
for b in $NX
 do 
  if [ `grep -q $b $TMP; echo $?` -eq 0 ] 
   then
   OLIST="$OLIST $b" 
   else
   NLIST="$NLIST $b"
  fi
 done

# if any unused tapes are in loader, print info on them
if [ -n "${OLIST}" ] 
 then 
  ONUM=`echo ${OLIST//" "/" "} |wc -w |cut -d" " -f1`
  echo -e "\n $ONUM new tapes are already present in the loader"
  echo -e "$OLIST \n"
fi

# print info on new tapes not in loader
NNUM=`echo ${NLIST//" "/" "} |wc -w |cut -d" " -f1`
echo -e "\n $NNUM new tapes need adding to the loader"
echo -e "$NLIST \n"

# df and amanda processes
echo "/dumps status"
echo "`df -h /dumps`"
echo ""
echo "@=================================================================@"
}

set_vars
delete_tmpfile

# if amanda processes are still running - exit, otherwise - run check_loader
`ps -u amanda 2>&1 > /dev/null` > /dev/null
if [ $? -eq 0 ] 
 then exit 4
 else
 set_vars
 delete_tmpfile
 check_loader
 exit 0
fi

Main Menu

Personal tools

Toolbox