尝试使用此方法发送多个命令通过shell.This致力于通过JSCH客户端SSH
Channel channel=session.openChannel("shell");
OutputStream ops = channel.getOutputStream();
PrintStream ps = new PrintStream(ops, true);
channel.connect();
ps.println("mkdir folder");
ps.println("dir");
//give commands to be executed inside println.and can have any no of commands sent.
ps.close();
InputStream in=channel.getInputStream();
byte[] bt=new byte[1024];
while(true)
{
while(in.available()>0)
{
int i=in.read(bt, 0, 1024);
if(i<0)
break;
String str=new String(bt, 0, i);
//displays the output of the command executed.
System.out.print(str);
}
if(channel.isClosed())
{
break;
}
Thread.sleep(1000);
channel.disconnect();
session.disconnect();
}
嗨。感谢您的重播,但是这些命令会立即被一一发送。但我想要的是发送命令,比如'cp',然后读取输出,一旦输出包含'你想重写文件吗?我发送响应'Y',这是可能的,我能够读取和分析响应,但我不能发送响应,以便在问题提供的背景下理解它,如果我使用'shell.exec(“Y”);'它会给出错误。 (我知道你可以使用标志覆盖所有,但我必须每次询问用户)。 – user2570174
我认为你所要求的是不可能的,因为这些功能只能用于命令而不能用于用户输入。 – krishna
好吧,也许有可能运行隐藏在后台的终端外壳,并发送输入,就好像用户在真实shell中键入它一样,这可能吗? – user2570174