Archiwum

Posty oznaczone ‘FreeBSD’

How to find yesterday’s or tomorrow’s date using shell?

Grudzień 15th, 2011 Brak komentarzy

There are couple of different ways to perform this task using shell date command depending on used OS. I will concentrate on Ubuntu Linux, OpenBSD and FreeBSD.

Ubuntu Linux

Ubuntu Linux offers -d switch to find the answer.

$ date --version
date (GNU coreutils) 8.5
...

Current date:

$ date
Thu Dec 15 21:37:53 CET 2011

Calculate yesterday’s date:

$ date -d "yesterday"
Wed Dec 14 21:38:02 CET 2011
$ date -d "last day"
Wed Dec 14 21:38:03 CET 2011
$ date -d "-1 day"
Wed Dec 14 21:38:05 CET 2011

Calculate tomorrow’s date:

$ date -d "tomorrow"
Fri Dec 16 21:39:20 CET 2011
$ date -d "next day"
Fri Dec 16 21:39:22 CET 2011
$ date -d "+1 day"
Fri Dec 16 21:39:25 CET 2011

OpenBSD

Using OpenBSD this task needs to be performed in different way as there is no equivalent switch.

Current date:

$ date
Thu Dec 15 22:15:07 CET 2011

Calculate yesterday’s date:

$ date -r `expr $(date +%s) - 86400` 
Wed Dec 14 22:15:09 CET 2011

Calculate tomorrow’s date:

$ date -r `expr $(date +%s) + 86400` 
Fri Dec 16 22:15:11 CET 2011

FreeBSD

FreeBSD offers handy -v switch to perform this task.

Current date:

$ date
Thu Dec 15 22:18:14 CET 2011

Calculate yesterday’s date:

$ date -v-1d
Wed Dec 14 22:18:18 CET 2011

Calculate tomorrow’s date:

$ date -v+1d
Fri Dec 16 22:18:22 CET 2011

How to do some work in each active jail (FreeBSD7)

Marzec 16th, 2011 Brak komentarzy

To execute couple of commands inside each active jail you can use such script:

#!/bin/sh
 
jails=`ezjail-admin list | grep -v "^STA" | grep -v "N/A" | grep -v "^\-" | awk '{ print $4}'`
for jail in $jails; do
   ezjail-admin console $jail  << COMMANDS
   # do something inside jail
   cd /var/mail/ && ls
COMMANDS
done

To just mess with directory structure from outside of each active jail look at this one:

#!/bin/sh
 
ports_dir="/home/jails/general/ports"
jails=`ezjail-admin list | grep -v "^STA" | grep -v "N/A" | grep -v "^\-" | awk '{ print $5}'`
for jail in $jails; do
  # start mess here
  mount_nullfs ${ports_dir} ${jail}/usr/ports
done
Tagi:,

Simple script to create new jail in FreeBSD

Marzec 16th, 2011 Brak komentarzy

This script will create image based jail in FreeBSD 7.

#!/bin/sh
 
ezjail="/usr/local/bin/ezjail-admin"
tail="/usr/bin/tail"
 
temp_file=`mktemp -q /tmp/jcreate.XXXXXX`
 
usage() {
  echo "Usage:"
  echo "jcreate.sh name ip size"
}
 
if [ "${#}" != "3" ]; then
  usage
else
  name=$1
  ip=$2
  size=$3
  ${ezjail} create -i -s ${size} -f default ${name} ${ip} > ${temp_file} 2>&1
  if [ "${?}" -eq "0" ]; then
    echo "Jail ${name} with address ${ip} and size ${size} created."
    echo "Log filename: ${temp_file}"
  else
    echo "There was an error"
    echo "Log filename: ${temp_file}"
    tail ${temp_file}
  fi
fi
Tagi:,