Shell script to find all programs and scripts with setgid bit set on

in Categories Monitoring last updated April 10, 2008

setuid and setgid (short for set user ID upon execution and set group ID upon execution, respectively) are Unix access rights flags that allow users to run an executable with the permissions of the executable’s owner or group. They are often used to allow users on a computer system to run programs with temporarily elevated privileges in order to perform a specific task. While the assumed user id or group id privileges provided are not always elevated, at a minimum they are specific.

setuid and setgid are needed for tasks that require higher privileges than those which a common user has, such as changing his or her login password. Some of the tasks that require elevated privileges may not immediately be obvious, though — such as the ping command, which must send and listen for control packets on a network interface.

Sample Shell Script

#!/bin/bash
# Shell script to find all programs and scripts with setgid bit set on.
# If your system ever cracked (aka hacked) then system has this kind of binary 
# installed; besides the normal setuuid scripts/programs
# Copyright (c) 2005 nixCraft project
# This script is licensed under GNU GPL version 2.0 or above
# For more info, please visit: 
#            http://cyberciti.biz/shell_scripting/bmsinstall.php
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
#SPATH="/usr/local/etc/bashmonscripts"
#INITBMS="$SPATH/defaults.conf" 
#[ ! -f $INITBMS ] && exit 1 || . $INITBMS
 
[ $# -eq 1 ] && : || die "Usage: $($BASENAME $0) directory" 1
 
DIRNAME="$1"
$FIND $DIRNAME -xdev -type f -perm +g=s -print

Bash shell script to reverse text file contain using Shell array

in Categories File-management last updated July 24, 2008

This script also demonstrate how to use arrays under bash shell script.

#!/bin/bash
# Bash shell script to reverse text file contain i.e. concatenate files and
# print on the standard output in reverse. This script also demonstrate how 
# to use arrays under bash shell script.
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
FILE="$1"
if [ $# -eq 0 ]; then
  echo "$(basename $0) - file-name"
  exit 1
fi
 
textArray[0]="" # hold text
c=0 # counter 
# read whole file in loop
while read line
do
  textArray[$c]=$line # store line
  c=$(expr $c + 1) # increase counter by 1
done < $FILE
# get length of array 
len=$(expr $c - 1 )
 
# use for loop to reverse the array
for (( i=$len; i>=0; i-- ));
do
  echo ${textArray[$i]}
done

Shell script to install libdvdcss under Debian GNU Linux to play DVDs that use css for encryption

in Categories Desktop last updated April 9, 2008
#!/bin/sh
# Shell script to install libdvdcss under Debian GNU Linux
# Many DVDs use css for encryption.  To play these discs, a special library 
# is needed to decode them, libdvdcss.  Due to legal problems, Debian and most
# Linux distibutions cannot distribute libdvdcss
# Use this shell script to install the libdvdcss under DEBIN GNU/Linux
# --------------------------------------------------------------------------
# Refer url for more info:
# Copyright info -  http://www.dtek.chalmers.se/~dvd/
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
set -e
 
site=http://www.dtek.chalmers.se/groups/dvd/deb/
arch=$(dpkg --print-installation-architecture)
 
soname=2
uversion=1.2.5
#available="alpha hppa i386 ia64 powerpc s390 sparc"
available="i386"
version=${uversion}-1
 
if ! type wget > /dev/null ; then
    echo "Install wget and run this script again"
    exit 1
fi
 
for a in $available; do
    if [  "$a" = "$arch" ]; then
	wget ${site}libdvdcss${soname}_${version}_${arch}.deb -O /tmp/libdvdcss.deb
	dpkg -i /tmp/libdvdcss.deb
	exit $?
    fi
done
 
echo "No binary deb available.  Will try to build and install it."
echo "You need to have debhelper, dpkg-dev and fakeroot installed."
echo "If not, interrupt now, install them and rerun this script."
echo ""
echo "This is higly experimental, look out for what happens below."
echo "If you want to stop, interrupt now (control-c), else press"
echo "return to proceed"
read dum
 
mkdir -p /tmp/dvd
cd /tmp/dvd
wget ${site}libdvdcss_${uversion}.orig.tar.gz
wget ${site}libdvdcss_${version}.diff.gz
wget ${site}libdvdcss_${version}.dsc
dpkg-source -x libdvdcss_${version}.dsc
cd libdvdcss-${uversion}
fakeroot ./debian/rules binary
echo "Any problems?  Interrupt now (control-c) and try to fix"
echo "manually, else go on and install (return)."
dpkg -i ../libdvdcss${soname}_${version}_${arch}.deb

Shell script to create list of backup files in ~/.mybackup file. Use with mybackup shell script

in Categories Backup last updated April 9, 2008
#!/bin/bash
# mybackupadd - Add file to ~/.mybackup file, then backup and email all
# file as tar.gz to your email a/c.
#
# Usage   : ./mybackupadd ~/public_html/
#
# Copyright (C) 2004 nixCraft project
# Email   : http://cyberciti.biz/fb/
# Date    : Aug-2004
# -------------------------------------------------------------------------
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
FILE=~/.mybackup
MYH=~
CWD=`pwd`
SRC=$1
 
if [ "$SRC" == "" ]; then
  echo "Must supply dir or file name"
  exit 1
fi  
# if list $FILE does not exist
[ ! -f $FILE ] && touch $FILE || :
 
# make sure that file or dir exists to backup
if [ ! -f $SRC ]; then
   if [ ! -d $SRC ]; then
      echo "$SRC does not exists"
      exit 2
   fi
fi
# make sure we don't do add duplicate stuff
cat $FILE | grep -w $SRC > /dev/null
if [ "$?" == "0" ]; then
   echo "$SRC exists in $FILE"
   exit 3
fi 
# okay now add that to backup list
echo "$SRC" >> $FILE
echo "$SRC added to $FILE"

Shell script to backup directories from Linux server to Windows 2000/NT Server

in Categories Backup last updated August 5, 2008
#!/bin/bash
# Shell script to backup directories from Linux server to Windows 2000/NT Server. 
# Run it as follows
# Scriptname /home backup abc123 //server2000/backup
# Backup /home directory from Linux box  to NT/2000 box called
# 'server2000' in share called '/backup' with username
# 'backup' and password 'abc123'
# --------------------------------------------------------------------
# This is a free shell script under GNU GPL version 2.0 or above
# Copyright (C) 2005 nixCraft project
# Feedback/comment/suggestions : http://cyberciti.biz/fb/
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
# backup what?
FROM=$1
# NT Connection Info #
# NT Username
NTUSER=$2
# NT Password
NTPASSWD=$3
# NT ShareName i.e //server/backup etc
NTSHARE="$4"
 
# BackUpDir Name
BACKDIR="$(hostname -s)"
 
# Local mount point
MNT="/mnt/smbbox"
 
# Get date and time 
NOW=$(date +"%m-%d-%Y-%I_%M%P")
# backup file  name
BAKFILE="backup.$NOW.tar.gz"
 
if [ "$#" != "4" ]; then
	echo "Syntax:"
	echo "$(basename $0) {Linux-directory} {NTusername} {NTpassword} {//NTserver/share-name}"
	exit 1
fi
# make sure $from do exits
if [ ! -d $FROM ]; then
   echo "Backup source directory \"$FROM\" does NOT exist"
   exit 2
fi
#Create tar to backup first
tar -czf /tmp/$BAKFILE $FROM 
 
#Mount the smb to /mnt 
[ ! -d $MNT ] && mkdir -p $MNT || :
 
mount -t smbfs -o username=$NTUSER,password=$NTPASSWD $NTSHARE $MNT
 
[ ! -d $MNT/$BACKDIR ] && mkdir -p $MNT/$BACKDIR || :
 
# Copy new tar to ntbox
cp /tmp/$BAKFILE $MNT/$BACKDIR
 
# Send sync aka force to write data before issuing umount
sync
 
# issue umount
umount $MNT