web analytics

Category Scripts

Script to clear spoofed emails from email queue

One of my friends email account was hacked, and was used to send tons of spam mails. Some of the mails were sent direct from his account while some others where spoofed.  There were around 50K emails in the queue.

I cleared all mails sent from him with the following command.

exiqgrep -i -f abc@abc.com | xargs exim -Mrm

where abc@abc.com is the account that was hacked.

Still there were close to 49000 mails in the queue. I wrote the following script to clear them. It basically checked the auth_id field in the header section of every email in the queue and if it matches abc@abc.com, then delete the message from the queue.

[root@abc ~]# vi script.sh


phrase=”-auth_id abc@abc.com”

for i in `exim -bp | awk ‘{print $3}’`
authid=`exim -Mvh $i | grep auth_id`
if [ “$authid” == “$ph...

Read More

PHP Script to create a test database, collection and data in mongo for testing purpose

If you need an immediate mongo database with data for test purpose, you can use the following script.

The script to be put in a nutshell – it will create a database named commandtest, create a collection named customer inside it. The collection customer has two fields – customerid, company

root@abc# vi createdb.php

$m = new MongoClient();    // connect
$db = $m->commandtest;            // create database commandtest
$chars = “abcdefghijklmnopqrstuvwxyz”;
$arraykeys = range(‘a’, ‘z’);
$collection = $db->customer;   // create collection customer
$string = substr(str_shuffle($chars), 0, 5);
$document = array(“customer_id” => $i.”_”.$string,”company” => $string );

Now try executing the script from backend its...

Read More

Script to delete all messages from mailman archives

One of our customer wanted to delete all messages from mailman archives. He had around 101 mailing lists and hence manually deleting them was not possible. I wrote a small script to delete the contents and attachments  – and Bingo – all cleared !! :- )

Check out the script here

First I took the list details into a file called ss

cd   /usr/local/cpanel/3rdparty/mailman/archives/private

ls | grep abc.org | grep -v mbox > ss

For a given mailing list, the path “/usr/local/cpanel/3rdparty/mailman/archives/private” contains two folders namely listname_abc.org and listname_abc.org.mbox . listname_abc.org contains a folder called ‘attachments’ which contains the attachments and files of following three types.

2013-April 2013-April.txt 2013-April.txt.gz

Our intent is to delete all such files a...

Read More

Software raid monitoring script

You wake up one day and realize that your site is dead. You try to reboot your server, nothing happens, your hard disk is gone 🙁 🙁

You wish if there was an alert to notify of the disk problem. See a sample script below with software raid which will alert you when disk fails.




#Suppose there are two raid arrays md1 and md3

CONTENT=`/sbin/mdadm –detail /dev/md3 ; /sbin/mdadm –detail /dev/md1`

COMPARE_md3=`/sbin/mdadm –detail /dev/md3 | grep “State :” | awk -F’:’ ‘{ print $2 }’`
COMPARE_md1=`/sbin/mdadm –detail /dev/md1 | grep “State :” | awk -F’:’ ‘{ print $2 }’`

if [[ ( $COMPARE_md3 = ” clean ” || $COMPARE_md3 = ” active ” || $COMPARE_md3 = “clean, checking” || $COMPARE_md3 = “active, checking” &&  $COMPARE_md1 = ” clean ” ||  $COMPARE_md1 =...

Read More

Script to backup svn repo and copy to backup server

A small script which will be helpful for developers, it will take the svn backup on a daily basis. Now you can simple restore this backup to start working with it in another server.


SUBJECT=” SVN backup done”
SUCCESS_MSG=”SVN dump has been copied successfully”
ERROR_MSG=”SVN dump error”

### Suppose name of the repository is myrepo which resides at /var/www/svn/myrepo###
cd /var/www/svn
svnadmin dump myrepo > myrepo.dump  2>$svn_output_file
if  [ $? -eq 0 ]
rsync -avzrp myrepo.dump user@xxx.xxx.xxx.xxx:/data/rsync_backups/svn_dump/  1>$rsync_output_file
if [ $? -eq 0 ]
cat $svn_output_file | /bin/mail -s “$S...

Read More

Script to take mysql backup in plesk server and rsync to backup server

The following script will take the backup of all mysql databases and copy them to your backup server. This is a plesk server.


SUBJECT=”MySql backup – $HOSTNAME”
SUCCESS_MSG=” Mysql Backup Done”
ERROR_MSG=”URGENT – Mysql backup error ”

## Find files which are older than 2 days and delete them ###
find  /home/rsync_backups/mysql_backup/ -type f -mtime +2 -exec rm {} ;

mysql_pass=`cat /etc/psa/.psa.shadow`
cd /home/rsync_backups/mysql_backup/

mysql_databases=`echo “show databases;” |mysql –user=$mysql_user –password=$mysql_pass | grep -v ^Database$`

for i in $mysql_databases
mysqldump –user=$mysql_user –password=$mysql_pass $i > $i...

Read More

Script to monitor software raid array



CONTENT=`/sbin/mdadm –detail /dev/md3 ; /sbin/mdadm –detail /dev/md1`

COMPARE_md3=`/sbin/mdadm –detail /dev/md3 | grep “State :” | awk -F’:’ ‘{ print $2 }’`
COMPARE_md1=`/sbin/mdadm –detail /dev/md1 | grep “State :” | awk -F’:’ ‘{ print $2 }’`

if [ $COMPARE_md3 == “clean” ] || [ $COMPARE_md3 == “active” ] || [ $COMPARE_md3 == “clean, checking” ] || [ $COMPARE_md3 == “active, checking” ] && [ $COMPARE_md1 == “clean” ] || [ $COMPARE_md1 == “active” ] || [ $COMPARE_md1 == “clean, checking” ] || [ $COMPARE_md1 == “active, checking” ]
echo  “$CONTENT”| /bin/mail -s “$SUBJECT” “$TO”
SUBJECT=”URGENT – Suspected failed drive in $HOSTNAME”
echo “$CONTENT” | /bin/mail -s “$SUBJECT” “$TO”

Read More

Login to website using curl

Suppose there is a domain  divya.com which has a login form. You need to login to the site every hour, to make sure that the login is working. For that the following script helps, which can be run as a cron.

cd /root/test/abc/
> cjar
> test2.txt
> test3.txt
##Cookies will be stored in the file cjar##
curl –cookie-jar cjar –data “username=abc@gmail.com&password=12345” –location “http://abc.com/login/” > test2.txt

##Specify the cookie file and access another link to see if it works. If login works, you will get the code of this page, otherwise it will get redirected to the login page itself, returning a 302 status code##

curl -b cjar http://abc.com/save_org > test3.txt

CODE=$(grep -o “302 – Found” test3.txt)
if [ “$CODE” == “302 – Found” ]
echo “abc...

Read More