簡體   English   中英

Java SWT使用Jcraft JSch連接到遠程服務器

[英]java SWT to connect to remote server using Jcraft JSch

我正在研究使用jcraft.JSch (ssh到Unix服務器)連接到遠程服務器並返回輸出並顯示其輸出的工具(類似膩子,但我不喜歡Plink或任何第三方的東西)。 當通道輸入輸出為System.inSystem.out並且該程序只是控制台Java應用程序時,該代碼可以正常工作。 但是當我嘗試使用SWT控件時,它遇到了問題,如如何將System.out和System.in映射到SWT控件中所述

因此,我相信它在INFO和ERR標簽(稍后我將要處理)中都存在,無論如何我都改變了方法。 現在,我通過SWT Text輸入輸入命令。 對於單行輸出,它的工作正常。 但不適用於多行。

在此處輸入圖片說明

觸發到Shell的連接並發送命令的代碼如下。

        public void widgetSelected(SelectionEvent e) {
        String command=CmdText.getText();
        System.out.println("runButton.widgetSelected:Command Obtained by buton is:"+command);
        if ( command.equals("") )
        {
           MessageDialog.openInformation(dialogShell,"Information","Please Eneter a Command!");
           return;
        }
        else if (command.contains("@"))
        {
            channel=CommandHandler.openshell();
            LogText.append("Connected to - :"+command+"\n>");
        }
        else
        {
            LogText.append(command+"\n");
            String outputstring="";
            try
            {
                 BufferedReader dataIn = new BufferedReader(new InputStreamReader(channel.getInputStream()));
                 DataOutputStream dataOut = new DataOutputStream(channel.getOutputStream());  

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

                 String line="";
                 outputstring=""+line;
                 while ( line != null) {
                     try
                     {
                        line = dataIn.readLine();
                        line = dataIn.readLine();
                        LogText.append("<CommandResult>:"+line+"\n");
                        return;
                     }
                     catch (IOException e1) { B24IDEConsole.WriteLog("RemoteCall.connect_execute", e1.getMessage()); }
                     outputstring=outputstring+line;
                 }
            }
            catch(Exception e1){ B24IDEConsole.WriteLog("RemoteCall.connect_execute", e1.getMessage()); }
            LogText.append("<EndOfCommand>:"+outputstring+"\n");
        }
    }

當我在while循環中刪除return時,對話框和日食 (觸發對話框)變得沒有響應,我必須殺死它們。

感謝您提供任何幫助,感謝您的寶貴時間。

我在上面的代碼中缺少兩件事。

  1. getOutputStream和getInputStream在channel.connect之后被調用,而Jsch文檔說不應引用它: http ://epaul.github.io/jsch-documentation/simple.javadoc/com/jcraft/jsch/Channel.html
  2. dataIn.available是更好的測試,無論服務器是否有其他可用數據。 感謝通過JSch shell通道向服務器發送命令

更新的代碼是

                 InputStream dataIn = CommandHandler.getdatain();
                 OutputStream dataOut = CommandHandler.getdataout();
                 //dataOut.write(command.getBytes()+"\n");
                 byte[] inputbytes = command.getBytes();
                 dataOut.write(inputbytes,0,inputbytes.length);
                 dataOut.flush();
                 while (true) {
                        boolean readoutput=false;
                        while (dataIn.available() > 0) {
                            readoutput=true;
                            byte[] buffer=new byte[1024];
                            int i = dataIn.read(buffer, 0, 1024);
                            if (i < 0) {
                                System.out.print("No more data available!");//It is printing the response to console
                                break;
                            }
                            LogText.append(new String(buffer, 0, i));
                            //LogText.append(":"+new String(buffer, 0, i)+"\n>");
                            System.out.print(new String(buffer, 0, i));//It is printing the response to console
                        }
                        System.out.println(dataIn.available()+"done");
                        if ( readoutput )
                        {
                            if ( dataIn.available() == 0 )
                            {
                                System.out.print("Read all the data and no nore is available, hence exiting!\n");//It is printing the response to console
                                break;
                            }
                        }
                        if ( channel.isClosed() ) {
                            System.out.println("exit-status: " + channel.getExitStatus());
                            break;
                        }
                        try{Thread.sleep(1000);}catch(Exception ee){}
                 }

中斷仍然是一個問題,但看起來現在可以修復。

暫無
暫無

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

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