This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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)
Connection conn = new Connection(host);
boolean isAuth = conn.authenticateWithPassword(user,password);
Session sess = conn.openSession();
sess.execCommand("/usr/bin/chmod 777 " + remoteFile + ".sh"); //This is working as expected.
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();
StringWriter sw0= new StringWriter ();
PrintWriter p0= new PrintWriter ( sw0, true );
e.printStackTrace ( p0 );
erMesg = sw0.getBuffer().toString();
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.
Not sure why you close and then open connection again between the 2 execCommand() calls? Does /usr/bin/sh exist on the remote server? Could you run the 2 commands on one line like this:
In your remote script, try and write something to a file like this: and check if this file gets created on the remote server. It's possible you don't see screen output if no console (tty) is associated to your remote session.
Joined: Apr 11, 2012
Thanks for your reply Koen. I didn't know to execute 2 different commands in one statement. Let me try running the 2 commands in one line as said by you.
/usr/bin/sh exists in my remote server. which sh --> /usr/bin/sh
I will also check if any file gets created by running shell script from remote server.
I created a simple program just only to test whether I am able to execute a script in my remote server. So It executed as expected. Also I am able to create a new file.
But in my main program my shell script contains "FNDLOAD" command which is an Oracle Utility. I did not mention the absolute path of FNDLOAD, that may be the reason that script didnt executed as expected. Although it didnot catch any exception. Also, I didnt get any error message, as I used getStderr() to collect the error if any in my TextArea.
So what I concluded from this is StreamGobbler is only used to get the output and error of any unix commands but not Oracle commands.
I will try now giving the absolute path of "FNDLOAD" and see if there is any luck.
The environment settings for the script may not be the same as those in a regular (terminal) session, for instance because .profile, .bashrc, or whatever other post-logon script is normally only executed after regular terminal logins. To test this, you could try a "env > /tmp/myenv.txt" in your script, then execute your script manually from a terminal and again through your remote program, then compare the two outputs from the "env" command. Likely they're different. So either your script either has to set required variables itself, or execute your default logon script (i.e. .profile, .bashrc, ... depending on the shell you're using, which you can find in /etc/passwd) and assuming it's using the same user account. Also, in the first line of your script you can specify which shell it needs to run in, for instance like "#!/usr/bin/sh". So then instead of "/usr/bin/sh script.sh" you just call your script like "script.sh" and it will still use /usr/bin/sh.