Take commands(password) from string and set as InputStream to Unix servers in Java (JSCH)

六眼飞鱼酱① 提交于 2020-01-10 05:58:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!