【问题标题】:read output from Linux shell and store it to a JSON object从 Linux shell 读取输出并将其存储到 JSON 对象
【发布时间】:2017-08-21 16:22:36
【问题描述】:

我想从 linux shell 上的命令读取 json 输出。

我使用jcraft建立ssh连接,使用channel执行telnet命令。

 String host="<host>";
        String user="<user>";
        String password="<pass>";
        String command1="telnet <ip>";
        try{

            java.util.Properties config = new java.util.Properties(); 
            config.put("StrictHostKeyChecking", "no");
            JSch jsch = new JSch();
            Session session=jsch.getSession(user, host, 22);
            session.setPassword(password);
            session.setConfig(config);
            session.connect();
            System.out.println("Connected");

            Channel channel=session.openChannel("exec");
            ((ChannelExec)channel).setCommand(command1);
            channel.setInputStream(null);
            ((ChannelExec)channel).setErrStream(System.err);
            InputStream in=channel.getInputStream();
            channel.connect();
            DataOutputStream dataOut = new DataOutputStream(channel.getOutputStream());

            byte[] tmp=new byte[1024];
            int count = 0;
            int count1 = 0;
            while(true){
              while(in.available()>0){
                int i=in.read(tmp, 0, 1024);
                if(i<0)break;
                System.out.println(new String(tmp, 0, i));
                String asksForUsername = new String(tmp, 0, i);
                if(asksForUsername.contains("login") && count <= 0) {
                    dataOut.writeBytes("<username>");
                    dataOut.writeBytes("\n");
                    dataOut.flush();
                    count++;
                }
                if(asksForUsername.contains("Password") && count1 <= 0) {
                    dataOut.writeBytes("<password>");
                    dataOut.writeBytes("\n");
                    dataOut.flush();
                    count1++;
                }
                if(count == 1 && count1 == 1) {
                    count++;
                    count1++;

                        dataOut.writeBytes("<shell command to get the json response>");

                    dataOut.writeBytes("\n");
                    dataOut.flush();



                }


              }
              if(channel.isClosed()){
                System.out.println("exit-status: "+channel.getExitStatus());
                break;
              }
              try{Thread.sleep(1000);}catch(Exception ee){}
            }
            channel.disconnect();
            session.disconnect();
            count = 0;
            count1 = 0;

        }catch(Exception e){
            e.printStackTrace();
        }

执行命令以使用写入字节获取 Json 响应后,我需要读取输出并将其分配给 json 对象吗?有可能吗。

提前致谢。

问候

【问题讨论】:

    标签: java json linux ssh telnet


    【解决方案1】:

    您需要添加以下代码行来获取和阅读消息

    // 获取此通道的 InputStream。从远程端作为消息到达的所有数据都可以从此流中读取。 InputStream in = channelExec.getInputStream();

    // 从我们上面设置的输入流中读取输出 BufferedReader reader = new BufferedReader(new InputStreamReader(in));

    【讨论】:

    • ~ 我已经在使用输入流读取输出,问题是我需要提供一些控制台输入并读取它们的输出。我将得到的最终输出是 JSON,我需要读取它并将其分配给 String 或 JSON 对象以供进一步使用。
    猜你喜欢
    • 2018-01-28
    • 2014-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-09
    相关资源
    最近更新 更多