Click here to Skip to main content
15,741,892 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Members,

I am using a JSCH api to execute commands in remote linux machines, since I have 200+ machines where I have to manually go to putty and change the password.

Now the problem with JSCH is that I cannot user "exec" to execute "passwd" command since linux takes only --stdin and I am not logging in as super user.

So using an other way around I would open a "shell" (from jsch api) in background and give input to shell via a String through InputStream

I made a code like below:

String s = "cd bin\n";

byte bb[] = s.getBytes();

InputStream intt = new ByteArrayInputStream(bb);

channel.setInputStream(new FilterInputStream(intt) {
    public int read(byte[] b, int off, int len) throws IOException {
        return, off, (len > 1024 ? 1024 : len));

Now this works perfectly when I want to execute only one command but I want to give multiple commands, for which I am facing issue.

Any suggestions?

pasztorpisti 23-Aug-13 14:21pm    
Shouldn't this work if you specify more lines in your string?
Ishan Shah 24-Aug-13 1:41am    
Sorry pastztorpisti, I didn't get your question. What more lines?
pasztorpisti 24-Aug-13 8:41am    
For example: String s = "cd bin\nls -l\n";
Now the string s contains 2 lines with 2 commands.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900