Hi,
SSHD client is timing out when input stream is passed to any remote program, 
and pty has been enabled in the client. Apparently, the remote program doesn't 
stop waiting on the input stream even after the input stream has been closed in 
the client code.  ( When pty is not enabled in the client, the code works 
fine). The client finally times out and getExitStatus() returns null. I am 
using Mina sshd client 2.5.1, and openssh server on RHEL 7. My test code is 
given below. I am passing some input to the "cat" program on the server. cat is 
expected to read the input and just send it back to the client:
/** start test code **/import java.io.ByteArrayInputStream;import 
java.io.ByteArrayOutputStream;import java.io.InputStream;import 
java.nio.file.Path;import java.nio.file.Paths;import 
java.security.KeyPair;import java.util.EnumSet;
import java.util.Set;import java.util.concurrent.TimeUnit;
import org.apache.sshd.client.SshClient;import 
org.apache.sshd.client.channel.ChannelExec;import 
org.apache.sshd.client.channel.ClientChannelEvent;import 
org.apache.sshd.client.session.ClientSession;
import org.apache.sshd.common.keyprovider.FileKeyPairProvider;
public class SshClientPtyTest {
 public static void main(String[] args) throws Exception { final String HOST = 
"a.b.c.d"; final int PORT = 22; final String KEYPATH = 
"C:\\Users\\xyz\\.ssh\\id_rsa"; final String USER = "xyz";
 int defaultTimeout = 30;
 SshClient client = SshClient.setUpDefaultClient(); client.start(); try 
(ClientSession session = client.connect(USER, HOST, 
PORT).verify(defaultTimeout, TimeUnit.SECONDS) .getSession()) { Path keyLoc = 
Paths.get(KEYPATH); FileKeyPairProvider keyPairProvider = new 
FileKeyPairProvider(keyLoc); KeyPair keyPair = 
keyPairProvider.loadKeys(null).iterator().next(); 
session.addPublicKeyIdentity(keyPair); session.auth().verify(defaultTimeout, 
TimeUnit.SECONDS);
 ByteArrayOutputStream bout = new ByteArrayOutputStream(); 
ByteArrayOutputStream berr = new ByteArrayOutputStream(); String contentToSend 
= "Content be sent to remote command"; InputStream bins = new 
ByteArrayInputStream(contentToSend.getBytes());
 try (ChannelExec channel = session.createExecChannel("cat")) {                
/** if setUsePty(false) then the issue disappears **/ channel.setUsePty(true); 
channel.setOut(bout); channel.setErr(berr); channel.setIn(bins); 
System.out.println("isUsePty() " + channel.isUsePty()); channel.open().verify();
 Set<ClientChannelEvent> result = 
channel.waitFor(EnumSet.of(ClientChannelEvent.EXIT_STATUS), 
TimeUnit.SECONDS.toMillis(defaultTimeout)); System.out.println("Stdout: " + 
bout + ", Stderr:" + berr); int es = channel.getExitStatus(); 
System.out.println("Exit Status: " + es);
 } } finally { client.stop();
 }
 }
}/** end test code **/
I am new to Mina sshd, so very likely there is something wrong withe code 
above, but I can't figure out what it is. Any help will be appreciated.
Thanks,Hari

Reply via email to