问题
Almost similar to this topic, but here I am not a superuser to use --stdin.
So I found an other way round, I would open a "shell" 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 in.read(b, 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?
Regards,
Ishan
回答1:
I found a solution to question I asked, don't know if its a full proof solution since I tested with limited period of time and I did it beast way I can.
channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
((ChannelExec) channel).setPty(true);
In the command string
String command = "(echo old_password; echo new_password; echo new_password) | passwd username";
or if not a superuser then,
String command = "(echo old_password; echo new_password; echo new_password) | passwd"
That is it ;)
regards,
icr
来源:https://stackoverflow.com/questions/18407089/take-commandspassword-from-string-and-set-as-inputstream-to-unix-servers-in-ja