Jsch : Command Output unavailable

懵懂的女人 提交于 2019-12-01 23:45:55

Please try the code below - tested and working

    Channel channel = session.openChannel("shell");
    OutputStream ops = channel.getOutputStream();
    PrintStream ps = new PrintStream(ops);
    channel.connect();
    ps.println("pwd");
    ps.println("exit");
    ps.flush();
    ps.close();

    InputStream in = channel.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    System.out.println("Opening...");

    String jarOutput;
    while ((jarOutput = reader.readLine()) != null)
        System.out.println(jarOutput);
    reader.close();
    channel.disconnect();

Output -

Opening...
user@host:~> pwd
/home/user
user@host:~>
user@host:~> exit
logout

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