Wednesday, December 9, 2009

Namespace approach to call a javascript method

html snippet:

javascript snippet: index.js
myApp = new Object();

myApp.fun=function(method1){
alert('inside overidden method');
if(arguments.length == 1)
window[method1]();
else if(arguments.length == 2)
window[method1](arguments[1]);
else if(arguments.length == 3)
window[method1](arguments[1], arguments[2]);
else if(arguments.length == 4)
window[method1](arguments[1], arguments[2], arguments[3]);
else if(arguments.length == 5)
window[method1](arguments[1], arguments[2], arguments[3], arguments[4]);
};
function invokerMethod(method){
if(typeof myApp != "undefined"){
myApp.fun(method);
}
else{
alert('namespace is not created for this release');
fun(method);
}
}

Actual method: test.js
function one(){
alert('actual method....');
}

Monday, November 23, 2009

Shell script: check for a exact pattern in a file


-----------------------------------------
#!/bin/sh
#bash
#generatehtml.sh

if [ -f output ]
then rm output
fi

if [ -f discussionJumpPage.html ]
then rm discussionJumpPage.html
fi

echo 'http://blind.apple.com/featured/featuredlist.php' >> output

baseUrl='http://blind.com/featured/featuredlist.php?limit=500&skip='
flag=1
skip=500

while [ $flag -eq 1 ]
do
url=$baseUrl$skip
echo $url >> output
if curl -l $url | grep -o '>next 500<'
then
skip=`expr $skip + 500`
echo "skip = "$skip
else
flag=`expr $flag - 1`
fi


Shell script to read lines from a file

1) Read lines from a file having only one word in each row
-----------------------------------------------
for server in `cat $SERVER` # file name
{
echo "Starting scp to server :...... "$server
}
-----------------------------------------------

here each line will come in variable server. SERVER is the name of the input file which is a variable file name. If there are two words in one line then second word would be read in next loop.

2) Now suppose you want to read complete line in the same iteration, then below approach would help:
------------------------------
while read inputline
do
docId="$(echo $inputline | cut -d" " -f1)" #seperated by space
localeId="$(echo $inputline | cut -d" " -f2)"
echo doc = $docId and locale = $localeId
done (less than sign ) REPORT
--------------------------------

Monday, November 16, 2009

Read from a file, jdbc query and then write to a file

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;



public class FinalClass {
final static String driverClass = "oracle.jdbc.OracleDriver";
final static String connectionURL="jdbc:oracle:thin:@fawkessa.corp.apple.com:1867:test";
final static String userID = "ima_owner_80";
final static String userPassword = "ima_owner_80";
static Connection oraConnection = null;
public static int countDoc=0;
static Statement stmt1 =null;
ResultSet rset1 =null;
private static BufferedWriter bufferedWriter = null;

public void checkInDB(String docId,String localeId){
String input=null;
try{
//System.out.println("-----------------"+docId+localeId);
String contenttextpub="select count(*) from contenttextpub where documentid='"+docId+"'"+"AND localeid='"+localeId+"'";;
rset1 = stmt1.executeQuery(contenttextpub);
//String s = rs.getString(1);
while(rset1.next()){ //System.out.println(rset1.getString(1)+"querry output........................"+rset1.getString(1).equals("0"));
//System.out.println(countDoc+")"+docId+","+localeId+"output is "+rset1.getString(1));
if(rset1.getString(1).equals("0")){
countDoc++;
System.out.println(countDoc+")"+docId+","+localeId+"output is "+rset1.getString(1));
bufferedWriter.write(countDoc+")"+docId+","+localeId);
bufferedWriter.newLine();
bufferedWriter.flush();
}
}
}catch(Exception e){
System.out.println("db exception "+e);
}
}

public static void main(String[] args){
System.out.println("calling");
FinalClass obj = new FinalClass();
try{
Class.forName(driverClass).newInstance();

//System.out.print(" Connecting to -> " + connectionURL + "\n");
oraConnection = DriverManager.getConnection(connectionURL, userID, userPassword);
//System.out.print(" Connected as -> " + userID + "\n");
stmt1 = oraConnection.createStatement();
}catch(Exception e){
System.out.println("exception e"+e);
}
String filename = "/Users/admin/Applications/workspace/InQuiraJar/src/LiveDetailsProdCo1.txt"; //input.rtf -UAT
try{
File fileliveDetails = new File(filename);
BufferedReader FileInputStream1= new BufferedReader(new FileReader(filename));
bufferedWriter=new BufferedWriter(new FileWriter("/Users/admin/Applications/workspace/InQuiraJar/src/newOUTPUT.rtf"));
String line=null;
do{
line = FileInputStream1.readLine();

//line has entries like .. /ngs/app/fsnt/oa/suuser/base/Scopes/iKnew/abc/instances/iknewco1/appserverim/webapps/ROOT/resources/sites/APPLE/content/live/MANUALS//0/MA233/en_US/AppleCare_Protection_Plan_for_Apple_TV.pdf
if(line.contains("/ngs/app/fsnt/oa/suuser/base/Sco"+"pes/iKnew/abc/instances/iknewco1/")){
String DocId=null;
String Locale=null;
//System.out.println("beginning....");
String[] DocandLoacle =new String[5];
String [] Path=line.split("//0/");
String DocIdAndLoacel=Path[1];
DocandLoacle=DocIdAndLoacel.split("/");
DocId=DocandLoacle[0];
Locale =DocandLoacle[1];
//System.out.println("doc id ="+DocId+" Locale= "+Locale);
//call method for checking in DB
obj.checkInDB(DocId,Locale);
}
}while(line!=null);
//String line = FileInputStream1.readLine();
//oraConnection.close();
FileInputStream1.close();
oraConnection.close();
bufferedWriter.flush();
bufferedWriter.close();
}catch(Exception e){
System.out.println("exception with file reading"+e);
}
//String input=obj.readDataFromFile();
}
}


Wednesday, October 7, 2009

automate ftp

ftp server: remote host from which you want to get file. Make sure that ftp server is installed and sharing is enabled.
ftp client:local machine

syntax: ftp ip_address
then give user_id
then password

Automation
create a file with below content
///// ftp.txt ////////////
server_login_id(like admin)
server_password( sid)
bin
cd full_path (cd users/admin/directory/)
get file_name(get test.txt)
bye
/////////////////////////

automated command
ftp -s:ftp.txt ftp.server.com
or ftp -s:ftp.txt ftp.server.ipaddress

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