簡體   English   中英

Jsch-通過ssh發送命令

[英]Jsch - Sending commands via ssh

問題:我似乎無法從緩沖的reader.readline()中擺脫“無限循環/塊”。 它傳遞一次,然后在while循環中第二次出現后掛在那兒。

這是我的代碼:

public void sendCommand(String command){
        {
            String line;
             try{
                 while((line = consoleOutput.readLine())!= null)
                    {
                         System.out.println(line);
                         if(line.equals("rkscli:") || line.equals("password :") || line.equals("Please login: ")){
                             pin.write((command + "\n\r").getBytes());
                         }
                    }
                 }

我正在使用nilbot從以下位置使用的方法: 在這里 ,我正在嘗試通過shell發送命令。

問題:

我要完成的工作是讓程序等待,直到服務器准備接收更多命令為止(等待提示“ rkscli”,因為某些命令花費更多時間)。 我該怎么做? 我目前正在使用pipedinput / output方法。

其他資訊:

服務器將在提示前給我“”(帶換行符的空字符串)。 經過幾次這些會給我提示。 我還注意到,如果我更改了代碼,使其看起來像這樣:

public void sendCommand(String command){
    {
        String line;
         try{
             while((line = consoleOutput.readLine())!= null)
                {
                     System.out.println(line);
                                     pin.write("\r\n".getBytes());//Added line
                     if(line.equals("rkscli:") || line.equals("password :") || line.equals("Please login: ")){
                         pin.write((command + "\n\r").getBytes());
                     }
                }
             }

它不會掛在bufferedReader.readline()上,而是會跳過並跳過寫操作(它似乎沒有向shell寫任何東西,有時會而且是無法預料的)

關於我能做什么的任何想法?

好的,我相信我是通過讀取每個字符(逐個字符)而不是讀取行來解決問題的,因為因為它是外殼,所以永遠不會給出終止(空)字符“ / 00”,並且如果我沒有記錯的話讀取行會尋找該行,並將繼續等待,直到發現它因此阻塞為止而不是繼續。

這是修改后的代碼:

 line = new StringBuilder();
                 boolean ready = false;
                 int c = 0;
                        Thread.sleep(1000);//needs to sleep, it isnt ready :(
                        while((ready = consoleOutput.ready()) == true){
                            ready = consoleOutput.ready();
                            c = consoleOutput.read();//read char by char
                            line.append((char)c);
                            if((char)c == ':' && (c=consoleOutput.read()) == 0x20 && (consoleOutput.ready()!= true)){
                                pin.write((command + "\n\r").getBytes());//writing after ready
                                pin.flush();
                                System.out.println(line.toString() + command);
                                line = null;
                                break;
                            }

我在第一個問題中向shell寫東西時遇到的奇怪問題是,它將繼續讀取行而不是在我向shell寫任何東西時阻塞,原因是它正在讀取並讀取行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM