Me in IT UNIX/Linux Consultancy is based in Breukelen, The Netherlands and specialized in UNIX and Linux consultancy. Experience with Red Hat Enterprise Linux (Red Hat Certified Architect), Fedora Project, CentOS, OpenBSD and related released Open Source products makes Me in IT UNIX/Linux Consultancy a great partner in implementing, maintaining and upgrading your environment.

Open Source software is an important aspect of any Linux distribution. Me in IT UNIX/Linux Consultancy tries to use Open Source software where possible and tries to share experiences actively. In the articles section you will find many UNIX/Linux adventures shared for others to benefit.

Solaris in combination with NetApps

here are some interesting settings that i tested. it seemed to work, so here you are.

/etc/system
set nfs:nfs_nra=10
set nfs:nfs3_nra=10
set nfs:nfs_max_threads=24
set nfs:nfs3_max_threads=24
set ncsize=8192
set tcp:tcp_xmit_hiwat 65536
set tcp:tcp_recv_hiwat 65536
set nstrpush=9
set rlim_fd_cur=1024
set rlim_fd_max=1024
set sq_max_size=0x19
ce:ce_taskq_disable=1

/etc/vfstab
netappfiler:/some/volume - /a/mountpoint nfs no yes hard,intr,bg,proto=tcp,vers=3,rsize=32768,wsize=32768,largefiles

/platform/sun4u/kernel/drv/ce.conf
interrupts=1;
accept-jumbo=1;

Enter your ssh-passphrase once, use it many times, even from crontab

Here is a way to use ssh-agent, ssh-add without having to type you password every session. Just once (when the machine has been rebooted) you will have to enter you passphrase, from then on you are authenticated. Here is the script you will have to add to your .bashrc or .profile in you homedirectory:

variables=~/.ssh/variables

sshadd() {
source "$variables" > /dev/null
ssh-add -l > /dev/null 2>&1
case "$?" in
  1)
   ssh-add > /dev/null 2>&1
  ;;
  2)
   rm "$variables"
   sshagent
  ;;
esac
}

sshagent() {
if [ -f "$variables" ] ; then
  sshadd
else
  ssh-agent -s > $variables
  sshadd
fi
}

sshagent

Have fun, I have been using this great hint for quite some time now, it seems to work perfectly!

Matching with awk

here is a very simple cool trick. Imagine you would have a file (test.txt) with a few million lines in it looking something like this:

abc 123 def
ghi 123 jkl
mno 456 pqr

Imagine you would like to know how many lines there are with "123" in the second column. try something like this:

cat test.txt | awk 'match ($2,/123/)' | wc -l

This will open the file test.txt ("cat test.txt") and give the results to awk ("|awk"). awk will look for 123 in column 2. ("match ($2,/123/)". these results are passed to wc, a counter, that can count lines with the -l option. ("| wc -l")

Think of another example; you'd like to find all pids where the connected tty is ttys000:

ps -ef | awk '{ if ($6 == "ttys000") print $1 }'

Expect Example

Here is a very simple script illustrating the use of expect:

#!/usr/bin/expect -f
log_user 0
set server [lrange $argv 0 0]
set from [lrange $argv 1 1]
set to [lrange $argv 2 2]
set message [lrange $argv 3 end]
spawn telnet "$server" 25
send "EHLO me\r"
set timeout 30
send "MAIL FROM: $from\r"
send "RCPT TO: $to\r"
send "DATA\r"
send "$message\r"
send ".\r"
send "QUIT\r"
expect oef

Options to arguments into scripts

Have you ever wondered how to get variables from a command line in a decent way?

Imagine you wrote a script that must check certain files how easy would it be to just enter the file on the command line interface as an option like this:

./yourscript.sh -f firstfile.txt
./yourscript.sh -f secondfile.txt

Here is some code to do just that. You will need to change the options according to your wishes.

#!/bin/sh

usage() {
echo "Usage: $0 -f FILE"
echo
echo " -f FILE"
echo "     Set the FILE variable."
exit 1
}

readargs() {
while [ "$#" -gt 0 ] ; do
  case "$1" in
   -f)
    if [ "$2" ] ; then
     file="$2"
     shift ; shift
    else
     echo "Missing a value for $1."
     echo
     shift
     usage
    fi
   ;;
   *)
    echo "Unknown option $1."
    echo
    shift
    usage
   ;;
  esac
done
}

readargs "$@"
if [ "$file" ] ; then
echo "You selected $file"
fi

Simple & Cool backup script

Here is a cool script, used to backup this site and a few others hosted here. The script should be self explanatory.

#!/bin/sh

# First, archive the older backups.
for file in public_html mail database.mysql; do
for number in 6 5 4 3 2 1 ; do
  if [ -f "$HOME"/backup/"$file"."$number".tar.gz ] ; then
   mv "$HOME"/backup/"$file"."$number".tar.gz "$HOME"/backup/"$file".$(($number + 1)).tar.gz
  fi
done
if [ -f "$HOME"/backup/"$file".tar.gz ] ; then
  mv "$HOME"/backup/"$file".tar.gz "$HOME"/backup/"$file".1.tar.gz
fi
done

# Now create new backups for files.
cd $HOME
tar -czf $HOME/backup/public_html.tar.gz public_html
tar -czf $HOME/backup/mail.tar.gz mail

# And create new backups for mysql.
for database in database ; do
mysqldump -u username -ppassword "$database" > "$HOME"/backup/"$database".mysql && tar -czf "$HOME"/backup/"$database".mysql.tar.gz backup/"$database".mysql
rm "$HOME"/backup/"$database".mysql
done

Some modifications have been made for publishing, not to reveal to much information...

US Daylight Savings Time (DST) 2007 Energy Act on Linux

Here is a script to check if your Linux box is ready for the US DST 2007 Energy Act. Save it, chmod it, run it.

#!/bin/bash

status=0

glpackagecheck() {
 rpm=$(rpm -q tzdata)
 case $rpm in
  tzdata-2005m*)
   :
  ;;
  tzdata-2006*)
   :
  ;;
  *)
   echo "RPM tzdata-2005m-1 is not installed. You installed: $rpm"
   status=$(($status+1))
  ;;
 esac
}

glfilecheck() {
 if [ ! -d /usr/share/zoneinfo/America/ ] ; then
  echo "The /usr/share/zoneinfo/America directory does not exist."
  status=$(($status+4))
 fi
}

glfunctioncheck1() {
 storedtz=$(echo $TZ)
 export TZ="America/Adak"
 date | grep 'HA.T' > /dev/null
 if [ $? -gt 0 ] ; then
  echo "Unable to switch timezone."
  status=$(($status+8))
 fi
 export TZ="$storedtz"
}

glfunctioncheck2() {
 zdump -v CST6CDT | grep 2007 | grep "Mar 11" > /dev/null
 if [ $? -gt 0 ] ; then
  echo "Wrong roll over date configured for CST6CDT."
  status=$(($status+16))
 fi
}

glpackagecheck
glfilecheck
glfunctioncheck1
glfunctioncheck2

if [ "$status" -gt 0 ] ; then
 echo "Not all tests ended with success, the status is: $status."
else
 echo "All test successful, you are prepared for the US DST change."
fi

Logratation based on size

Here is a script to rotate any logfile. Optionally a filesize can be given. It contains some extra features that are not (yet) in the logrotate facility.

I am not sure it works, but hey, you can tweak it! ;-)

#!/bin/bash

defaultconfig() {
PATH=/usr/bin:/usr/local/bin
number=9
}

usage() {
echo "Usage: $0 [-h|--help] [-c [VALUE]] [-s VALUE] [-n VALUE] -f FILE"
echo ""
echo " -h, --help"
echo "     Display this screen."
echo " -c"
echo "     Use gzip to compress file."
echo " -s VALUE"
echo "     rotate the file is the size equals VALUE. Size in bytes."
echo "     If no size is specified, the file is rotated without"
echo "     checking its size."
echo " -n VALUE"
echo "     rotate VALUE number of file. Default is set to 9"
echo " -d DIRECTORY"
echo "     Move the rotated files to DIRECTORY."
echo " -f FILE"
echo "     FILE is the file to be rotated. This is an argument, not"
echo "     an option."
echo " -p COMMAND"
echo "     execute COMMAND before (pre) rotating."
echo " -P COMMAND"
echo "     execute COMMAND after (post) rotating."
echo ""
exit 1
}

readargs() {
while [ "$#" -gt 0 ] ; do
  case "$1" in
   -c)
    compress="yes" && extension=".gz"
    shift
   ;;
   -s)
    if [ "$2" ] ; then
     size="$2"
     shift ; shift
    else
     echo "No size specified."
     echo ""
     usage
    fi
   ;;
   -n)
    if [ "$2" ] ; then
     number="$2"
     shift ; shift
    else
     echo "Number of files to rotate not specified."
     echo ""
     usage
    fi
   ;;
   -f)
    if [ -f "$2" ] ; then
     file="$2"
     shift ; shift
    else
     echo "No such file: $2."
     echo ""
     usage
    fi
   ;;
   -d)
    if [ -d "$2" ] ; then
     directory="$2"
     shift ; shift
    else
     echo "No such directory: $2."
     echo ""
     usage
    fi
   ;;
   -p)
    if [ "$2" ] ; then
     command=`echo "$2" | awk '{print $1}'`
     if [ -x "$command" ] ; then
      precommand="$2"
      shift ; shift
     else
      echo "Command $precommand cannot be executed"
      echo ""
      usage
     fi
    else
     echo "No command specified"
     echo ""
     usage
    fi
   ;;
   -P)
    if [ "$2" ] ; then
     command=`echo "$2" | awk '{print $1}'`
     if [ -x "$command" ] ; then
      postcommand="$2"
      shift ; shift
     else
      echo "Command $postcommand cannot be executed"
      echo ""
      usage
     fi
    fi
   ;;
   *)
    echo "Unknown option $1"
    echo "You must specify at lease a file with the \"-f\" argument."
    echo ""
    usage
   ;;
  esac
done
}

precommand() {
if [ "$precommand" ] ; then
  $precommand
  if [ "$?" -gt 0 ] ; then
   echo "Command $precommand failed, aborting."
   echo
   usage
  fi
fi
}

postcommand() {
if [ "$postcommand" ] ; then
  $postcommand
  if [ "$?" -gt 0 ] ; then
   echo "Command $postcommand failed, retrying."
   sleep 3
   $postcommand
   if [ "$?" -gt 0 ] ; then
    echo "Command $postcommand failed, aborting."
    echo
    usage
   fi
  fi
fi
}

rotatefiles() {
if [ -f "$file" ] ; then
  while [ "$number" -ge 1 ] ; do
   if [ -f "$directory$file.$number$extension" ] ; then
    let numberplusone="$number + 1"
    mv "$directory$file.$number$extension" "$directory$file.$numberplusone$extension"
   fi
   let number="$number - 1"
  done
else
  echo "No file to rotate."
  echo ""
  usage
fi
}

firstmove() {
precommand
cp "$file" "$directory$file.1"
cp /dev/null "$file"
chmod 644 "$file"
postcommand
if [ "$compress" ] ; then
  gzip "$directory$file.1"
fi
}

startrotator() {
let number="$number -1" && rotatefiles
firstmove
}

checkrunning() {
mode="$1"
instances=`ps -ef | grep "$0" | grep -v "$$"`
numberofinstances=` echo "$instances" | wc -l`
if [ "$numberofinstances" -gt 1 ] ; then
  if [ "$mode" = "silent" ] ; then
   exit 1
  else
   echo "$0 is already running. Stopping this instance."
   echo "instances:"
   echo "---"
   echo "$instances"
   echo "---"
   echo '$0'": $0"
   echo '$$'": $$"
   exit 1
  fi
fi
}

checkrunning silent || ( sleep 60 ; checkrunning )
defaultconfig
readargs "$@"

if [ -f "$file" ] ; then
if [ "$size" ] ; then
  filesize=`ls -l "$file" | awk '{print $5}'`
  if [ "$filesize" -gt "$size" ] ; then
   startrotator
  fi
else
  startrotator
fi
else
echo "You did not specify a file."
echo ""
usage
fi

About Consultancy Articles Contact




References Red Hat Certified Architect By Robert de Bock Robert de Bock
Curriculum Vitae By Fred Clausen +31 6 14 39 58 72
By Nelson Manning robert@meinit.nl
Syndicate content