Linux Firewall: Simple Shell Script To Stop and Flush All Iptables Rules

in Categories Security last updated April 24, 2008
#!/bin/bash
# Linux Firewall: Simple Shell Script To Stop and Flush All Iptables Rules
# Some Linux distros like Debian do not have /etc/init.d/iptables stop script
# This can be also called from cron job if you are testing new firewall on
# remote box to avoid lock out
# -------------------------------------------------------------------------
# Copyright (c) 2004 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.
# -------------------------------------------------------------------------
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT

Remote Server Rsync Backup Replication Shell Script

in Categories Backup last updated April 24, 2008

You need to setup password less login using ssh keys; refer to following tutorials:
+ Howto Linux / UNIX setup SSH with DSA public key authentication (password less login)

+ SSH Public key based authentication – Howto

#!/bin/bash
# Remote Server Rsync backup Replication 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.
# ------------------------------------------------------------------------- 
# Local dir location
LOCALBAKPOINT=/disk3
LOCALBAKDIR=/remote/home/httpd/
# remote ssh server
# user
SSHUER=brootbeer 
 
# server IP / host
SSHSERVER=10.10.11.12 
 
#remote dir to backup
SSHBACKUPROOT=/disk2.backup/hot/ 
 
rsync --exclude '*access.log*' --exclude '*error.log*' -avz -e 'ssh ' ${SSHUER}@${SSHSERVER}:${SSHBACKUPROOT} ${LOCALBAKPOINT}${LOCALBAKDIR}
 
# log if backup failed or not to /var/log/messages file
[ $? -eq 0 ] && logger 'RSYNC BACKUP : Done' || logger 'RSYNC BACKUP : FAILED!'
 
# Replicate backup to /disk1 and /disk2
# You can also use format user@host:/path
# refer to rsync man page
SRC=${LOCALBAKPOINT}${LOCALBAKDIR}
DST="/disk1/remote /disk2/remote"
for d in $DST
do
 [ ! -d $d ] && mkdir -p $d || :
 rsync -avr $SRC $d
done

Shell Script To Count English Language Articles Such As 'A', 'An' and 'The'

in Categories Decision Making, File-management last updated April 21, 2008

This script also covers following techniques:
=> Reading input text file line by line
=> Reading and processing words in input
=> Convert word to lowercase

#!/bin/bash
# Write a shell script that counts English language articles (a, an, the) 
# in a given text file.
#
# --------------------------------------------------------------------
# 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.
# -------------------------------------------------------------------------
echo -n "Enter a file name : "
read file
a=0
the=0
an=0
 
# make sure file exist
if  [ ! -f $file ]
then
	echo "$file not a file!"
	exit 1
fi
 
# put while loop to read a $file
while read line
do
       #process each word 
	for w in $line 
	do
		# convert word to lowercase; so that we can count ThE, THE, the, THe etc all
		lword="$(echo $w | tr '[A-Z]' '[a-z]')"
 
		# is it 'a' article?
		[ $lword = "a" ] && (( a++ )) || :
		[ $lword = "the" ] && (( the++ )) || :
		[ $lword = "an" ] && (( an++ )) || :
	done
done < $file
 
# display stats
echo "a article occured $a times"
echo "the article occured $the times"
echo "an article occured $an times"

Shell Script to send HTTP Code 301 Moved Permanently Followed by Actual URL Redirection

in Categories File-management last updated April 10, 2008

This script depends upon shell array.

#!/bin/bash
# Shell script to update old php files to point out to new CMS url
# it placed http code 301.
# You must create an array in following format:
# path/to/old/url1.php  path/to/new/url/slug/
# --------------------------------------------------------------------------
# Copyright (C) 2008 nixCraft project <http://www.cyberciti.biz/tips/contact-us>
# 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.
# -------------------------------------------------------------------------
declare -a t=("http://domain.com/1.php" "http://domain.com/new/path/slug/"
"http://domain.com/2.php" "http://domain.com/new/path/slug/"
"http://domain.com/3.php" "http://domain.com/new/path/slug/"
"http://domain.com/4.php" "http://domain.com/new/path/slug/"
"http://domain.com/5.php" "http://domain.com/new/path/slug/")
# set array total array size -1 ; if you have 5 item it should set to 4
for i in `seq 0 4` 
do
	#only work on zero or even element  
	if [ $i -eq 0 -o  $( expr $i % 2 ) -eq 0  ]; then
		s="${t[$i]}"
                d="${t[` expr $i + 1 ` ]}"
                file=$(basename $s)
 		echo "Patching $file...."
		if [ -f $file ]; then
		# if file exists, create new php redirection code
			echo '<?php'>$file
			echo 'header("HTTP/1.1 301 Moved Permanently");'>>$file
			echo 'header("Location: ' $d '");'>>$file
			echo '?>'>>$file
		fi
	fi
done

Shell script to generate random files using RANDOM variable

in Categories Tempfile last updated April 10, 2008
#!/bin/bash
# Shell script to generate random file method # 1 
#
# This is a free shell script under GNU GPL version 2.0 or above
#
# Copyright (C) 2005 nixCraft
#
# Feedback/comment/suggestions : http://cyberciti.biz/fb/
 
echo "List of temporary files : "
for i  in 1 2 3 4 5
do
   FILE="/tmp/$(basename $0).$RANDOM.txt"
   echo $FILE # show file name
   > $FILE # create files
done

Script to update user password in batch mode using pwgen and chpasswd

in Categories Security last updated October 23, 2008
#!/bin/bash
# Script to update user password in batch mode
# You must be a root user to use this script
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project 
# 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.
# ----------------------------------------------------------------------
# /root is good place to store clear text password
FILE="/root/batch.passwd"
 
# get all non-root user account
# By default on most linux non-root uid starts
# from 1000
USERS=$(awk -F: '{ if ( $3 > 1000 ) print $1}' /etc/passwd)
 
# create file with random password
echo "Generating file, please wait..."
 
# overwrite file, this is bash specific a better solution is cat > $FILE
>$FILE
 
for u in $USERS
do
   p=$(pwgen -1 -n 8) # create random password
   echo "$u:$p" >> $FILE # save USERNAME:PASSWORD pair
done
echo ""
echo "Random password and username list stored in $FILE file"
echo "Review $FILE file, once satisfied execute command: "
echo "chpasswd &lt; $FILE"
 
# Uncomment following line if you want immediately update all users password,
# be careful with this option, it is recommended that you review $FILE first
# chpasswd < $FILE