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