Thursday, September 10, 2009

Some important unix commands

1) Get the header of a url
curl -i url_value

2) Check if port is opened
netstat -an | grep port_value

3) tar/untar
tar -cvf ROOT.tar ROOT #create tar of ROOT
tar -xvf ROOT.tar #untar

jar xf kb.war #unwar war file

4) Copy to a remote host
scp unix_id_value@/path/to/
scp test.sh c0378015@iTest.corp.com:/tmp/

5)Search for a file in the Root as well as sub directories
find . -name "file_name" -print
find . -type d -maxdepth 2 -name "file_name*" -print

6) Check if a port is opened or not
telnet ip_address_of_host port
i.e. telnet 17.216.26.28 21 (21 is default port for ftp)
17.0.0.1 for localhost

7) Output of a url through unix commands:

curl -l 'http://canaanio.corp.com:11234/kb/index?page=iTestdebug&show=cookie'

8) Generate build file using ant commands:
//ant -buildfile path_of_build_file
ant -buildfile /Users/admin/Applications/workspace/iTest/build.xml

9) Read sth line from a file
sed -n sp REPORT
for 2nd line: sed -n 2p REPORT

10) Disk/File Usage
du -sk .
df -k .


11) list only file name
ls -1

12) Running processes
ps -ef | grep java


13) Kill/thread dump
kill -9 PID //for killing the process
kill -3 PID //for taking thread dump

14) Running script in background
nohup script.sh &

16) Know the current active shell : echo $SHELL #SHELL is environment variable
list supported shells: cat /etc/shells



15) Hiding password characters
echo "Enter your current password: \c"
stty -echo
read oldpass
stty echo

  

Monday, August 31, 2009

Automate scp command using expect script

Usually scp can't be automated, as it asks for password. So we need some program which can simulate the user interaction. One such program is expect script. Using this any user input can be automated.

Without expect:
scp file_name unix_id@server_name:/tmp/ #tmp directory on server where file will be placed.
This approach will required user to enter the password manually. So if the number of servers are very large then it becomes very painful.

This whole process can be automated using 'expect' script. I have automated the complete process using two scripts:
1) shell script: it will take one time input from user and will call expect script.
2) expect script: it will automate the password part. Please note in below shell script that, its calling expect script with some inputs.

--------------------------scp.sh---------
SCP_DIR=`pwd`
echo $SCP_DIR

echo "\n Enter the name of tar file (without extension .tar.gz) :\c"
read TAR_FILE

if [ -f $TAR_FILE.tar.gz ]
then
flag=0
else
echo "tar file $TAR_FILE.tar.gz for deployment doesn't exist at path $SCP_DIR"
exit 0
fi

echo "\n Enter the source servers file :\c"
read SERVER

if [ -f $SERVER ]
then
flag=0
else
echo "File doesn't exist...."
exit 0
fi

echo "Enter UNIX User id : \c"
read USER
echo "Enter Password : \c"
read PASS

for server in `cat $SERVER`
{

echo "Starting scp to server :...... "$server

expect scp.expect $server $USER $PASS $TAR_FILE.tar.gz
echo "\n scp complete......for server $server \n\n\n\n"
}
----------------------------

-------scp.expect---------
#!/usr/bin/expect -f

set SERVER [lindex $argv 0]
set USER [lindex $argv 1]
set PASS [lindex $argv 2]
set TAR_FILE [lindex $argv 3]
#send -- "./qry_sid.sh $KEYVAL\r"

spawn scp $TAR_FILE $USER@$SERVER.corp.apple.com:/tmp/
match_max 100000
expect "*assword*" {
send -- "$PASS\r"
}
sleep 50
#send -- "exit\r"

expect eof
-----------------

How to run the script:
sh scp.sh
rest everything will be taken by these two scripts.

Monday, August 24, 2009

ANT build

To generate a build.xml for your project from ECLIPSE:
1) Right-click on your project, select Export...
2) Select Ant Buildfiles under the General Folder, click Next
3) Select your project, click Finish This will create a build.xml file at the root of your project. To run the build, right-click on the build.xml file and select Run As > Ant Build