Friday, November 26, 2010

Directory watcher

Directory watcher will update update.txt file with what are the file or directories removed or added from previous run of script.

This is totally shell script.
First copy with dir_watcher name and give execution permissions and run as "./dir_watcher dir_path"

For the first time runs. It takes snapshot of ur directory structure which was provided in cmd.
When second time runs it will take snapshot of directory and if any new files or dir are added then those updates will be written in to update.txt in parent directory.
If dir path not mentioned it will take present dir.

Note:- It won't give information of updated files.
Put this script in crontab so it will run periodically and updates log file with changes.
ex:- 0 12 * * * sh /bin/dir_watcher /home/balu/project/
So, every day at 12 noon it will run and updates the update.txt file in "/home/balu/project/"

Wednesday, November 3, 2010

Use cmd line arguments for expect script.

Below I have given a small example to get a file from ftp server. This code was written using expect.
Usage:- HOST PROMPT$ expect ftp_get_file.exp < IP > < user_name > < password > < file_to_get >

EX:-
#!/usr/bin/expect -f < IP >
set ip [lrange $argv 0 0]
set username [lrange $argv 1 1]
set password [lrange $argv 2 2]
set file [lrange $argv 3 3]
set timeout -1
#Spwan the Cfm1 Util stub
spawn ftp $ip
expect ":"
send -- "$username\r"
sleep 1
expect "Password:"
send -- "$password\r"
sleep 2
expect ">"
send -- "bi\r"
sleep 1
expect ">"
send -- "ha\r"
sleep 1
expect ">"
send -- "mget $file\r"
sleep 2
expect "?"
send -- "y\r"
sleep 1
expect ">"
send -- "by\r"
sleep 1
expect eof

NOTE:- If ur password contain any special character then use "\" in front of those characters.

Friday, October 8, 2010

Expect scritp runs multiple cmds taking as a variable

Hi as part of unit testing i have written a expect script that expects same things but i need to send some list of cmds. For the first time i have written in the sequence of expect, send, sleep and then for the second and go on. But script is very lengthy if i want to see execution flow of cmds i need to scroll down to entire script.
So i find an alternate solution where i kept all my cmds in one varible and split on newline (i used newline, can be changed) and send these cmds one by one using for.
Below script is small example which will explain what i have done. Pretty simple logic and very basic too.
Have a look......

#!/usr/bin/expect -f
set data "ls
pwd
cd ..
ls
pwd
cd ..
pwd
ls
cd /etc
pwd
ls
cd /home/baluenigma
exit"

#Spwan the Cfm1 Util stub
spawn /bin/bash
match_max 100000

foreach line [split $data \n] {
expect "[baluenigma@192.168.2.79 *]#"
send -- "$line\r"
sleep 1
}

Above data is varible which holds all cmds which need to be executed.
Foreach is used to read data varible and split on newline char and line varible takes that value. Now expect cmd expects "[baluenigma@192.168.2.79 *]#" here '*' used because pwd will change when i used cd cmds which are in data variable.

Tuesday, October 5, 2010

Flv to 3gp converter

First of all I was installed ffmpeg on my centos 5.1 using yum.
If you don't have then simply run "yum install ffmpeg*".
Now to convert flv into 3gp, ffmpeg should contain h263 vedio codes generally it has and amrnb audio codes. In my case i don't have this audio code which was downloaded. Compiled and installed without any problem.
Create a file /bin/fl23gp and write below code.
------------------------------------------------------------------------------------------------------------
#!/bin/bash
echo "flv 2 3gb Convertion"
echo ""
if (($# ==0))
then
echo "Usage: flvto3gp [flv files] ..."
exit
fi

while (($# !=0 ))
do
ffmpeg -i $1 -s 176x144 -vcodec h263 -r 25 -b 200 -ab 6.7k -sameq -acodec libopencore_amrnb -ac 1 -ar 8000 ${1//.flv/.3gp}
shift
done
echo "Finished flv-to-3gp convertion"
echo "\" Enjoy \""
echo ""
------------------------------------------------------------------------------------------------------------
Run "chmod 777 /bin/flv23gp" .

"ffmpeg -formats | grep amr" will return what are audio codes preset with amr. Which was mentioned in /bin/flv3mp3 file for -acodec as "libopencore_amrnb" .