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.