1

MY Question:I want to connect to UNIX server and run a script by passing a run time argument to the script using java program. I have requirement to connect to a UNIX server and run a script by passing a run time argument to the script.I have used below code to connect to Unix server and i am succesfull connecting to the server.The script has general grep command with a small logic and it has all the permissions.When i run the java code i am able to see the output text files but has nodata in the files, but when i run the Script manually from shell prompt i am getting proper output. Please help me to resolve this issue and let me know if there is any better option which fullfills this requirement.

Java Code:

import com.jscape.inet.ssh.*;   
import com.jscape.inet.ssh.util.SshParameters;   
import java.io.*;     
public class Sshexamplelalit implements SshListener {          
// state of SSH connection   
private boolean connected = false;   
public Sshexamplelalit() {   
    Ssh ssh = null;        

    try {   
        // create new Ssh instance   
        SshParameters params = new SshParameters("sacsun","mwctrl","m33tz0n3");   
        System.out.print("sacsun Unix server is connected \n");   
        ssh = new Ssh(params);   
        // register to capture events   
        ssh.addSshListener(this);   

        System.out.println("sacsun Connecting please wait...\n");   
        SshScript script = new SshScript(ssh);   
        SshTask task = new SshTask(">","./SACSUN.sh '07Gretchen01965'",">");
                            //07Gretchen01965
        // connect   
        script.addTask(task); 
        ssh.connect();   
        while(!script.isComplete()) { 
            System.out.println("Waiting");
             Thread.sleep(30000);   
            }                  
        } catch (Exception e) {   
        e.printStackTrace();               
        } finally {   
        try {   
            if(connected) {   
              ssh.disconnect();    
            }   
        } catch(Exception e) {   

        }              
    }   
}   

/**  
 * Captures SshConnectedEvent  
 */  
public void connected(SshConnectedEvent ev) {   
    System.out.println("Connected: " + ev.getHost()+"\n");   
    connected = true;   
    System.out.println("Connected to server  "+ ev.getHost()+"\n");    
}   

/**  
 * Captures SshDataReceivedEvent  
 */  
public void dataReceived(SshDataReceivedEvent ev) {   

    ev.getData();   

}         

/**  
 * Captures SshDisconnectedEvent  
 */  
public void disconnected(SshDisconnectedEvent ev) {   

    System.out.println("Wait server is Disconnecting: " + ev.getHost()+"\n");   
    connected = false;   
}   

/*  
 * Main method for SshExample  
 */  
public static void main(String[] args) {   
    Sshexamplelalit test = new Sshexamplelalit();   
}   

}  

Script:

    #!/bin/sh

    HOME_DIR=/usr/ftadapters/logs/adapters/rivaadp
    HOME_DIR_ARCH=$HOME_DIR/archive
    cd $HOME_DIR
    rm RivaAdp.txt
    rm BrokerLog.txt
    rm Result.txt
    rm ArchRivaAdp.txt
    echo $1 > out.txt
    grep -i $1  *.log > RivaAdp.txt
    cd $HOME_DIR_ARCH
    grep -i $1 *.log > ArchRivaAdp.txt
    echo $1 > out.txt
    mv ArchRivaAdp.txt ../
    cd ..
    grep -i 'publishMessage' RivaAdp.txt > BrokerLog.txt
    grep -i 'publishMessage' ArchRivaAdp.txt >> BrokerLog.txt
    cnt=`cat BrokerLog.txt | wc -l`
    echo $cnt
    if [ $cnt -gt 0 ]
    then
            echo "Notifications Received in the Broker Logs" > Result.txt
    else
            echo "No Data Found in Broker Logs" > Result.txt
    fi
2
  • Going to take a shot in the dark... Have you tried using the full path of the script instead of ./ ? Also full path of file argument? Commented Feb 23, 2012 at 7:53
  • The Script is executing i mean outputfile are generated and runtime arg is also recognized by script but some how the text files ArchRivaAdp.txt has no data in it and hence skiping next steps after grep -i $1 *.log > ArchRivaAdp.txt. do u think that the while condition is causing problem. while(!script.isComplete()) { System.out.println("Waiting"); Thread.sleep(30000); } . Even i tried with Thread.sleep(180000);(3 min) but not working Commented Feb 23, 2012 at 10:15

1 Answer 1

1

I think you want to do an SshSession:

http://files.jscape.com/sshfactory/docs/javadoc/com/jscape/inet/ssh/SshSession.html

What is happening is that the SshScript you are using is invoking the script, but the isComplete method does not do what you are expecting--the script does not communicate back any status to your task. In other words, you are remotely logging in, invoking the script as a batch job, and isComplete returns as true immediately, so the while loop with the timeout doesn't do anything at all.

The SshTask documentation explains this: http://files.jscape.com/sshfactory/docs/javadoc/com/jscape/inet/ssh/SshTask.html

"The SshTask class represents a command to be executed in batch (non-interactive) mode on SSH server"

I find that typically if you are invoking some command and there is some kind of sleep needed to wait, it is a warning sign that something is not right with the design.

ETA: Reading the docs on SshTask some more, I think you can also set an end prompt for the task so that the Java app can tell when the script is complete.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.