【发布时间】:2015-10-28 22:03:07
【问题描述】:
我正在尝试使用 JSch 首先在远程系统中执行 sudo su - user 命令,然后连续执行管道 shell 命令。我想将命令输出读入String。我怎样才能做到这一点?我正在尝试这样的事情,但无法获得任何输出。请指教。
session = getSession();
channel = (ChannelShell)session.openChannel( "shell" );
String sudoCommand = "sudo su - " + sudoAs;
String nextCommand = "ps -eaf | grep user | wc -l";
pipeIn = new PipedInputStream();
pipeOut = new PipedOutputStream( pipeIn );
channel.setInputStream(pipeIn);
channel.setOutputStream(System.out, true);
channel.connect(3*1000);
pipeOut.write(sudoCommand.getBytes());
Thread.sleep(1000);
pipeOut.write(nextCommand.getBytes());
【问题讨论】: