• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

chmod command executing fine but sh command not executing the script in remote server.

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 344
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Prem Swaroop
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Basically I was collecting the output in StringBuilder from stdout and stderr. I created a TextArea where I am passing the StringBuilder. But not getting any output.

I will try the above steps any will reply back.

Thanks,
Prem.
 
Prem Swaroop
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Please correct me if I am wrong.
 
Koen Aerts
Ranch Hand
Posts: 344
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic