I am trying a Java program to run multiple commands in unix environment. I would need to pass 'ENTER' after each command. Is there some way to pass enter in the InputStream.
JSch jsch=new JSch();
Session session=jsch.getSession("MYUSERNAME", "SERVER", 22);
session.setPassword("MYPASSWORD");
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel= session.openChannel("shell");
channel.setInputStream(getInputStream("ls -l"));
channel.setInputStream(getInputStream("\r\n"));
channel.setInputStream(getInputStream("pwd"));
channel.setInputStream(getInputStream("\r\n"));
channel.connect();
When I do ls -l, I want to add enter here, so that the command is executed. getInputStream is a method to convert String into InputStream.
Any help will be appreciated.
According to the JSch javadoc, you must call
setInputStream()
orgetOutputStream()
beforeconnect()
. You can only do one of these, once.For your purposes,
getOutputStream()
seems more appropriate. Once you have an OutputStream, you can wrap it in a PrintWriter to make sending commands easier.Similarly you can use
channel.getInputStream()
to acquire an InputStream from which you can read results.If you're determined to use
setInputStream()
instead ofgetOutputStream()
then you can only do that once, so you'll have to put all your lines into one String:(I don't think you need
\r
, but add it back in if necessary)If you're not familiar with working with streams, writers and readers, do some study on these before working with JSch.