#7 Jsch ChanelExec question

open
nobody
None
5
2005-06-10
2005-06-10
mixon
No

What I am trying to achieve is to send a line of
commands separated by semicolon.

One of the commands that I execute is calling sch
script in the korn shell which in turn prompts me for a
password.

I was not able to return password from my PC to the
remote host.

What am I doing wrong?

There is no documentation so I am hopping someone
could give me an example on how to return password to
remote host when I execute script on remote host.

Here is a program sample I am using:

import com.jcraft.jsch.*;

import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;

public class ssh2run {
private static JSch jsch;

public ssh2run()
{
jsch=new JSch();

}

public static void main(String[] args)
{
try
{
ssh2run ssh = new ssh2run();

jsch.setKnownHosts("testhost");

String host = " testhost";
String user ="user";
Session session=jsch.getSession(user, host,
22);
session.setPassword("password");

HostKeyRepository
hkr=jsch.getHostKeyRepository();
HostKey[] hks=hkr.getHostKey();

java.util.Properties config=new
java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);

try
{
session.connect();
}
catch(JSchException e)
{
System.out.println(e);

}

String command ="/home/starttesthost.sh";

Channel channel=session.openChannel("exec");

((ChannelExec)channel).setCommand
(command);
channel.setXForwarding(true);

channel.setInputStream(System.in);
channel.setOutputStream(System.out);

((ChannelExec)channel).setErrStream
(System.err);

InputStream in = channel.getInputStream();

channel.connect();

String password="password";

Channel c = (Channel)channel;

c.getOutputStream().write(password.getBytes
()); c.getOutputStream().flush();

channel.disconnect();

System.out.println("Stop Test \n");
}
catch(Exception e)
{
System.out.println(e);
}
}
}

Discussion