Hi,
I am using Ganymed-ssh2 library for connecting to a remote server and trying to execute a shell script there. I am first transferring a script, then giving all permissions with chmod and then executing it. I am able to execute chmod but unable to execute sh command. Below is the code I used :-
public synchronized boolean runScript(
String remoteFile)
{
try
{
Connection conn = new Connection(host);
conn.connect();
boolean isAuth = conn.authenticateWithPassword(user,password);
Session sess = conn.openSession();
sess.execCommand("/usr/bin/chmod 777 " + remoteFile + ".sh"); //This is working as expected.
sess.close();
sess = conn.openSession();
sess.execCommand("/usr/bin/sh " + remoteFile + ".sh"); // This is the problematic part for me.
InputStream stdout = new StreamGobbler(sess.getStdout());
InputStream stderr = new StreamGobbler(sess.getStderr());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
BufferedReader brErr = new BufferedReader(new InputStreamReader(stderr));
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while( line != null )
{
sb.append( line + "\n" );
line = br.readLine();
}
String lineErr = brErr.readLine();
while( lineErr != null )
{
sb.append( lineErr + "\n" );
lineErr = brErr.readLine();
}
sess.close();
conn.close();
return true;
}
catch(Exception e)
{
StringWriter sw0= new StringWriter ();
PrintWriter p0= new PrintWriter ( sw0, true );
e.printStackTrace ( p0 );
erMesg = sw0.getBuffer().toString();
return false;
}
}
And this method returns true that means execCommand is not throwing any exception. But the script is supposed to display some output on the screen and it also creates another file. Neither of these two happens.