从字符串中获取命令(密码)并将其设置为InputStream到Java(JSCH)中的Unix服务器 [英] Take commands(password) from string and set as InputStream to Unix servers in Java (JSCH)

查看:561
本文介绍了从字符串中获取命令(密码)并将其设置为InputStream到Java(JSCH)中的Unix服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几乎与主题类似,但此处我不是超级用户--stdin。

Almost similar to this topic, but here I am not a superuser to use --stdin.

所以我找到了另一种方法,我会在后台打开一个shell并通过字符串给shell输入通过 InputStream

So I found an other way round, I would open a "shell" in background and give input to shell via a String through InputStream

我做了如下代码:

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.

有什么建议吗?

问候,

Ishan

推荐答案

我找到了一个问题的解决方案我问过,不知道它是否有完整的证明解决方案,因为我在有限的时间内进行了测试,并且我做了它的野兽。

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);

在命令字符串中

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"

就是这样;)

问候,

icr

这篇关于从字符串中获取命令(密码)并将其设置为InputStream到Java(JSCH)中的Unix服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆