Jsch : Command Output unavailable

前端 未结 1 1516
你的背包
你的背包 2021-01-23 05:33

I am trying to use jsch to connect to a remote switch and run some command and extract the output.

I am able to connect to the switch using , however the command output

相关标签:
1条回答
  • 2021-01-23 06:20

    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

    0 讨论(0)
提交回复
热议问题