LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 08-24-2010, 06:13 AM   #1
Jerry Mcguire
Member
 
Registered: Jul 2009
Location: Hong Kong SAR
Distribution: RedHat, Fedora
Posts: 143

Rep: Reputation: 17
Executing Shell Command in JSP


My colleague has quit and disappeared suddenly, left us some unfinished webpages that we are totally new to. We think she has won a lottery, big, or something.

Anyway, ideally what we are trying to do is, to let the customer click a button in the web browser, and then the web server to call a shell script to do the work. The output from the stdout && stderr of the script should be displayed in the web browser once finished or timeout, along with the exit code of the script.

The shell script is however not on the web server, but on another app server. So to call this script from the web server as the identity 'tomcat':

Code:
$ sh appuser@app-server:\$appbin/app-script
The .ssh/id_rsa.pub thing is done, and we have no problem doing this in the command line so far.

Our loaded ex-colleage has left us the webpages (jsp) with code like these:
Code:
<%@ include file="jsp_functions.jsp" %>
<%
String cmd = "sh $appbin/app-script"; 
ExecResult r = new ExecResult();
execCommand(cmd, r);
int iCode = r.getExitCode();
String result = r.getResult();
%>
<html>
<head>
<link rel="stylesheet" type="text/css" href="../css/app.css" />
<title> Generate Reports </title>
</head>
<h1 id="rpts_h1"> Reports Retrieval </h1>
<body>
<form id="rpts_a_form">
<textarea rows=35 cols=80 id="result_textarea">

Exit Code: <%=iCode%>

<%=result%>

</textarea>
<input type="button" value="Menu" onClick="<%=cancel_back%>">
</form>
</body>
</html>
My questions are:
- How do you do it?
- and why 'sh $appbin/app-script' is sufficient? There is a suspicious jar file named 'ganymed-ssh2-build250.jar' in WEB-INF/lib/ .
- half of the time r.getExitCode() returns NULL, and half returns the expected code. why!?
 
Old 08-25-2010, 02:52 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by Jerry Mcguire View Post
...
- half of the time r.getExitCode() returns NULL, and half returns the expected code. why!?
You think people here possess telepathic abilities ? I.e. you think people here can know how some shell script works not seeing its source and not seeing its input and not seeing messages it produces ?
 
Old 08-30-2010, 02:58 AM   #3
Jerry Mcguire
Member
 
Registered: Jul 2009
Location: Hong Kong SAR
Distribution: RedHat, Fedora
Posts: 143

Original Poster
Rep: Reputation: 17
No. And No. I've already said we are totally new to the stuff. It wouldn't be nice if I post 30 pages of code here, would it. We are frustrated too.

Forget about what we have in hand already or what weird things had happened to us. Can any one show me how to call a shell script within a webpage named something.jsp, and capture its stdout/stderr and exit status. Can any one help please?

One example script is like:
Code:
#!/bin/sh
# Name : app-script
# Desc : to download the app configure file from user's ftp
#        server to app-server.
#        $appbin is well-defined and accessible.
#        ftp username and password are already defined.
#
ftp -iv customer-file-server <<END_OF_FTP
ascii
lcd $appbin
get app.conf
bye
END_OF_FTP
exit 0
The script has been tested on the webserver in the command line as the user 'tomcat'.

Code:
# su - tomcat
$ ssh appuser@app-server "sh \$appbin/app-script"

Last edited by Jerry Mcguire; 08-30-2010 at 03:30 AM.
 
Old 08-30-2010, 06:02 AM   #4
Jerry Mcguire
Member
 
Registered: Jul 2009
Location: Hong Kong SAR
Distribution: RedHat, Fedora
Posts: 143

Original Poster
Rep: Reputation: 17
This does seem to work on specific commands such as /bin/pwd.

Code:
<%@ page import="java.util.*,java.io.*" %>
<%
String cmd = "ssh appuser@app-server '/bin/pwd'";
String outstr = "";

        try {
                Runtime rt = Runtime.getRuntime();
                Process p = rt.exec(cmd);

                try {
InputStreamReader ise = new InputStreamReader(p.getErrorStream());
BufferedReader bre = new BufferedReader(ise);
InputStreamReader iso = new InputStreamReader(p.getInputStream());
BufferedReader bro = new BufferedReader(iso);

String line=null;
while ( (line = bre.readLine()) != null ) {
        System.out.println( "ERR>" + line );
}
while ( (line = bro.readLine()) != null ) {
        System.out.println( "OUT>" + line );
}

                } catch (IOException ioe)
                {
                        ioe.printStackTrace();  
                }

                int exitVal = p.waitFor();

                System.out.println( exitVal );
        }
        catch (Throwable t) {
                t.printStackTrace();
        }

%>
<HTML>
<BODY>
...
Other commands like:
Code:
String cmd = "ssh appuser@app-server '/bin/bash --version'";
String cmd = "ssh appuser@app-server '/bin/ls'";
do not work.

Can any one please help?
 
Old 08-30-2010, 04:26 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by Jerry Mcguire View Post
...
Other commands like:
Code:
String cmd = "ssh appuser@app-server '/bin/bash --version'";
String cmd = "ssh appuser@app-server '/bin/ls'";
do not work.

Can any one please help?
How exactly they do not work ?
...
Anyway, the golden rule is to avoid as much as possible command line arguments because different shells interpret command lines differently.

In order to avoid command line arguments a wrapper script needs to be written by the calling application. I.e. if you in reality want to call

Code:
some_app -foo bar -doo dah doo boo
make your calling application to first create 'wrapper.sh' file whose contents look like this:

Code:
#!/bin/sh
# set environment here if necessary
some_app -foo bar -doo dah doo boo
, then make the calling application to do effectively

Code:
chmod +x wrapper.sh
and finally call it from the calling application '/full/path/to/wrapper.sh'.

You can also (temporarily) modify 'wrapper.sh' to become, for example,

Code:
#!/bin/sh
# set environment here if necessary
some_app -foo bar -doo dah doo boo 1>some_app.stdout
cat some_app.stdout

- in such a manner stdout produced by the 'some_app' is preserved in 'some_app.stdout' file, which facilitates debugging. You can similarly preserve stderr - read about shell screen output redirection.
 
Old 08-30-2010, 10:25 PM   #6
Jerry Mcguire
Member
 
Registered: Jul 2009
Location: Hong Kong SAR
Distribution: RedHat, Fedora
Posts: 143

Original Poster
Rep: Reputation: 17
Sorry I forgot to print the output. It was:
Code:
$ tail /var/log/tomcat5/catalina.log
ERR>bash: /bin/bash --version: No such file or directory
127
Thank you for your patience.

Anyway, I got your point on the wrapper script, but in most cases, command parameters are dynamic. The following first 2 command strings are now working:

Code:
String cmd = "ssh appuser@app-server '/usr/local/app/bin/test.sh'"; // ok
String cmd = "ssh appuser@app-server /bin/ls $appbin"; // ok

String cmd = "ssh appuser@app-server '/bin/bash --version'"; // error
I guess it has to do with the string tokenizing processed by exec(). "'/bin/bash" is parsed and thus No such file or directory.

And further, unlike typing through the command prompt, variable expansion in the command does not happen in exec().

A better (may be) approach is to use the string array instead:
Code:
String[] cmd = new String[4];
cmd[0] = "ssh";
cmd[1] = "appuser@app-server";
cmd[2] = "/bin/bash";
cmd[3] = "--version";
Thanks.
 
Old 08-30-2010, 10:37 PM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by Jerry Mcguire View Post
Sorry I forgot to print the output. It was:
Code:
$ tail /var/log/tomcat5/catalina.log
ERR>bash: /bin/bash --version: No such file or directory
127
Thank you for your patience.

Anyway, I got your point on the wrapper script, but in most cases, command parameters are dynamic. The following first 2 command strings are now working:

Code:
String cmd = "ssh appuser@app-server '/usr/local/app/bin/test.sh'"; // ok
String cmd = "ssh appuser@app-server /bin/ls $appbin"; // ok

String cmd = "ssh appuser@app-server '/bin/bash --version'"; // error
I guess it has to do with the string tokenizing processed by exec(). "'/bin/bash" is parsed and thus No such file or directory.

And further, unlike typing through the command prompt, variable expansion in the command does not happen in exec().

A better (may be) approach is to use the string array instead:
Code:
String[] cmd = new String[4];
cmd[0] = "ssh";
cmd[1] = "appuser@app-server";
cmd[2] = "/bin/bash";
cmd[3] = "--version";
Thanks.
The item in bold is questionable in light of

Code:
String cmd = "ssh appuser@app-server /bin/ls $appbin"; // ok
.

OTOH, you may just think that it works, i.e. it may be that just '/bin/ls' is invoked.

...

The fact that command line arguments change from call to call does not defeat my approach - generate the needed wrapper script per call (preferably with unique name).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
executing shell command from java melih utkan Programming 2 05-10-2009 10:36 AM
Executing a command from shell, but with a delay? SirTristan Linux - Newbie 6 05-15-2008 01:23 AM
Executing a command in Shell Script gjagadish Programming 7 10-15-2007 05:23 AM
executing Bourne shell variable as command? ocicat Programming 3 07-31-2007 03:00 AM
Executing a shell script command!! vishamr2000 Programming 4 08-15-2005 01:33 AM


All times are GMT -5. The time now is 05:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration