Bug 36302

Summary: sshexec closes System.out
Product: Ant Reporter: Grzegorz Kruk <Grzegorz_Kruk>
Component: Optional TasksAssignee: Ant Notifications List <notifications>
Status: RESOLVED FIXED    
Severity: normal CC: bodewig, glennoph, grave, Markus.Barchfeld, psen, ritesh.trivedi, riznob
Priority: P2    
Version: 1.6.5   
Target Milestone: 1.7.0   
Hardware: All   
OS: All   
Attachments: from Atsuhiko Yamanaka - patch for TeeOuputStream

Description Grzegorz Kruk 2005-08-22 15:49:44 UTC
ChannelExec as an output stream uses TeeOutputStream which is created using
directly System.out. Finalization of this channel object closes System.out. 
The effect is that there is no output from second and subsequent calls to the
sshexec task.
Comment 1 Antoine Levy-Lambert 2005-08-31 19:58:51 UTC
Created attachment 16265 [details]
from Atsuhiko Yamanaka - patch for TeeOuputStream

Atsuhiko Yamanaka wrote :
I think that this issue comes from that System.out will be closed in
'close' method of TeeOutputStream[1].
You know that TeeOutputStream has been used in SSHExec[2].

It seems following modification for TeeOutputStream fixes this problem,
Comment 2 Matt Benson 2005-08-31 20:37:05 UTC
More generic would be the use of a KeepAliveOutputStream in the correct place; 
i.e. SSHExec's TeeOutputStream:

new TeeOutputStream(out, new KeepAliveOutputStream(System.out))

I would question the use of System.out in the first place--within Ant it would 
probably be more correct to use a LogOutputStream, but I suppose that's a 
different issue.
Comment 3 Matt Benson 2005-08-31 21:41:54 UTC
My suggestion appears to fix this problem; committed to CVS HEAD.
Comment 4 Robert Anderson 2006-02-21 20:47:11 UTC
*** Bug 38720 has been marked as a duplicate of this bug. ***
Comment 5 Stefan Bodewig 2006-04-12 20:24:00 UTC
*** Bug 39186 has been marked as a duplicate of this bug. ***
Comment 6 Prapti Sen 2006-09-06 23:46:51 UTC
Has this really been fixed. I am using ant-1.6.5 and I still can't make
subsequenet calls to sshexec. 

The second sshexec command results in 
==============================================================================
[sshexec] Connecting to 10.10.0.220:22

BUILD SUCCESSFUL
Total time: 57 seconds
==============================================================================

The second sshexec commands are NOT executed. 
Comment 7 Antoine Levy-Lambert 2006-09-07 02:07:25 UTC
this is fixed in Ant 1.7, but not in Ant 1.6.5.
you can download the Ant 1.7Beta1 from the ant.apache.org website to check.
Comment 8 Peter Reilly 2006-11-22 01:22:59 UTC
*** Bug 41012 has been marked as a duplicate of this bug. ***
Comment 9 ghertz 2007-11-07 08:52:09 UTC
I still have this problem with Ant 1.7
Any suggestions?
Thanks in advance
Comment 10 Peter Reilly 2007-11-07 09:04:25 UTC
1) can you provide an example *small* build.xml that shows the problem
2) have you tried the SVN version of ant? - a number of bugs
   have been fixed wrt ssh
3) this bug specificly referres to TeeOutputStream closing System.out,
   is this the problem you are seeing?
Comment 11 ghertz 2007-11-08 02:05:07 UTC
This is the code in my build.xml (in the last target that it has to be 
executed):

<sshexec host="${remote.host}"
         username="${remote.user}"
         password="${remote.password}"
         command="cd ${remote.commonshare};rm -rf ${codeline.dir}-OLD;mkdir -p 
${codeline.dir};mv ${codeline.dir} ${codeline.dir}-OLD;"/>

<scp file="${tmp.dir}/${zip.file}" todir="${remote.user}:${remote.password}
@${remote.host}:${remote.commonshare}"/>

<sshexec host="${remote.host}"
         username="${remote.user}"
         password="${remote.password}"
         command="cd ${remote.commonshare};unzip ${zip.file};rm -f 
${zip.file}"/>


The result is the same as reported by Prapti Sen (comment #6): the first 
sshexec is executed successfully, the same thing with the scp command, but the 
result of the second sshexec is like:


The second sshexec command results in 
==============================================================================
[sshexec] Connecting to 10.10.0.220:22

BUILD SUCCESSFUL
Total time: 57 seconds
==============================================================================


It connects to the remote machine, but it doesn't make anything.
However, the build doesn't fail.

Now I've tested, and it seems working fine, an external library, doing the 
same things.
If you want, consult this link:

http://www.sshtools.com/showMaverickAntTask.do




Comment 12 Steve Loughran 2007-11-08 02:36:07 UTC
I do a truckload of sshexecs in my build, on ant 1.7.0 pure

    <presetdef name="ssh-remote">
      <sshexec host="${ssh.server}"
          username="${ssh.user}"
          passphrase="${ssh.passphrase}"
          trust="${ssh.trust}"
          timeout="6000000"
          keyfile="${ssh.keyfile}"
          />
    </presetdef>
    <ssh-remote command="mkdir -p ${ssh.maven.dir}"/>
    <property name="ssh.maven.path"
        value="${ssh.user}@${ssh.server}:${ssh.maven.dir}" />
    <scp remoteToDir="${ssh.maven.path}"
        passphrase="${ssh.passphrase}"
        keyfile="${ssh.keyfile}"
        trust="${ssh.trust}"
        verbose="${ssh.verbose}" >
      <fileset file="${maven.zip}" />
    </scp>
    <ssh-remote 
      command="unzip ${ssh.maven.dir}/${maven.zip.filename} -d ${ssh.maven.dir}"/>
    <ssh-remote command="rm ${ssh.maven.dir}/${maven.zip.filename}"/>
    <ssh-remote command="chmod a+r ${ssh.maven.dir}"/>

it works for me. 

1. What does ant -diagnostics print?
2. which version of jsch are you using?
Comment 13 ghertz 2007-11-08 08:58:21 UTC
I've tested your suggestion and it works fine.Thanks
However, every time ssh-remote is called:

1) You have to specify the path (ie, you don't make somthing like this:
<ssh-remote command="cd ${remote.dir}"/>
<ssh-remote command="touch foo.txt"/>

The foo.txt will not create in the path ${remote.dir}", but in the $HOME of 
the remote user
To make this, I ve to do : <ssh-remote command="touch ${remote.dir}/foo.txt"/>
If I have to do another operation in the same directory, I must specify the 
path again.

2) makes a new connection with the remote machine, and I don't like this thing 
too much.
Instead, importing in the lib folder maverick-ant.jar, and using this syntax, 
I make all operations I need with one connection.

 <taskdef name="ssh" classname="com.sshtools.ant.Ssh" 
classpath="./lib/maverick-ant.jar"/>
 
       <ssh host="${remote.host}"
                username="${remote.user}"
                password="${remote.password}">
                <exec cmd="cd ${remote.commonshare}"/>
                <exec cmd="rm -rf ${codeline.dir}-OLD"/>
                <exec cmd="mv ${codeline.dir} ${codeline.dir}-OLD"/>
             <sftp action="put"
                   remotedir="${remote.commonshare}"
                   verbose="true">
                   <fileset dir="${tmp.dir}">
                        <include name="${zip.file}"/>
                   </fileset>
             </sftp>
                <exec cmd="cd ${remote.commonshare}"/>
                <exec cmd="unzip ${zip.file}"/>
                <exec cmd="rm -f ${zip.file}"/>
        </ssh>

Thanks everybody for your attention and suggestions