Wednesday, October 17, 2007

ConCatPASSWD.sh

#!/bin/bash
# Concatenate two passwords files. The resulting will contain entries of the first file with UID < 500 and that of second with UID > 500
DATE=`date +%d.%m.%Y`
TIME=`date +%H.%M.%S`
echo Name of File 1
read f1
echo Name of File 2
read f2
echo Name for New file
read newfile
mv $newfile $newfile.bak.$DATE.$TIME


## Manipulate first file

count=`cat $f1 | cut -f3 -d : | sort -n | wc -l`
h=1
while [ "$count" -ge "$h" ]
do
{
id=`head -$h $f1 | tail -1 | cut -f3 -d:`
if [ $id -lt "500" ]
then
{
head -$h $f1 | tail -1 >> $newfile
}
fi
h=$[$h + 1 ]
}
done

## file 2
count=`cat $f2 | cut -f3 -d : | sort -n | wc -l`
count=$(`expr 'cat count'`)
h=1
while [ "$count" -ge "$h" ]
do
{
id=`head -$h $f2 | tail -1 | cut -f3 -d:`
if [ $id -gt "499" ]
then
{
username=`head -$h $f2 | tail -1 | cut -f1 -d:`
grep $username: $newfile
if [ $? = "0" ]
then
echo "User $username or *$username* already exists in $newfile came from $f1 with UID less than 500"
else
head -$h $f2 | tail -1 >> $newfile
fi

}
fi
h=$[$h + 1 ]
}
done

ChangeGID.sh

#!/bin/bash
# Change the GID of a Group and make it effective for all the members in /etc/passwd
pwfile=/etc/passwd
grpfile=/etc/group
DATE=`date +%d.%b.%Y`
TIME=`date +%H.%M.%S`


echo Creating backups $pwfile.$DATE.$TIME and $grpfile.$DATE.$TIME
cat $pwfile > $pwfile.$DATE.$TIME
cat $grpfile > $grpfile.$DATE.$TIME

echo Enter group name
read gname
cgid=`grep $gname $grpfile | cut -f3 -d:`
username_s=`cat $pwfile | grep $cgid | cut -f1 -d:`
echo Following users will be affected
echo "##############"
echo $username_s
echo "##############"


echo Enter new GID
read ngid

## Change the GID of the group
groupmod -g $ngid $gname

## Change GID(s) in passwd file

uidc=`grep $cgid $pwfile | cut -f3 -d: | wc -l`
uidh=1
while [ "$uidc" -ge "$uidh" ]
do
{
id=`head -$uidh uid | tail -1`
sed 's/'$id':'$cgid'/'$id':'$ngid'/g' $pwfile > $pwfile.new
cat $pwfile.new > $pwfile
uidh=$[$uidh + 1 ]
}
done

echo The entries in replaced File are
grep $ngid $pwfile.new
echo The original has been backed up as $pwfile.$DATE
echo Thank you
cd $PWD
exit 0

RUNCMDS.sh

#!/bin/bash
## BASH Script to Run any System command #####
## Script should be initialized as ./script.sh arg1 arg2 arg3,....
## echo "Commands with spaces should be supplied as \`COMMAND OPTIONS\`."
## echo "For example "./runcdms.sh \`mkdir test\` \`chmod 777 test\`""


for i in `$*`
do
#sudo - u root $i ## You can sudo if you are not root
$i
done

PING.sh

#!/usr/bin/env bash
## Ping all machines in a Network
PING="$(which ping) -c 1 -W 1"
echo "Enter Subnet(eg:192.168.0)"
read Subnet
echo "Do you want to PING the entire network or a RANGE of IPs ? Enter your choice"
echo 1. Ping Entire Network
echo 2. Ping a RANGE
read choice

if [ $choice = 1 ];
then
{
echo Pinging.....
for((i=1;i<255;i++)); do
${PING} ${Subnet}.${i} > /dev/null 2> /dev/null
if [ $? -eq 0 ];
then
echo -e "${Subnet}.${i} is up"
fi
done
}
fi


if [ $choice = 2 ];
then
{
echo Enter the Starting IP of Range
read a
echo Enter the Last IP of Range
read b
echo Pinging.....
for((i=$a;i<$b;i++)); do
${PING} ${Subnet}.${i} > /dev/null 2> /dev/null
if [ $? -eq 0 ];
then
echo -e "${Subnet}.${i} is up"
fi
done
}
fi
exit 0