Java遠程執行Shell命令


需要ganymed-ssh2-build210.jar

1 連接

Connection conn = new Connection(ip地址, 端口號);

conn.connect();

2認證並判斷

boolean isAuthenticated = conn.authenticateWithPassword(用戶名, 密碼);

if (isAuthenticated == false){
System.out.println("密碼錯誤");
throw new IOException("Authentication failed.");
 }

3打開一個Session

final Session session=conn.openSession();

4上傳文件(根據情況)

SCPClient client = new SCPClient(conn);
client.put(updateFileBytes, "xlsbz_server.zip", "/app/mhfx/");

5執行Shell命令(一般放線程里執行)

new Thread(new Runnable() {
                    public void run() {
                        try {
                            session.requestDumbPTY();                            //建立虛擬終端
                            session.startShell();                                         //打開一個shell
                            OutputStream fout = session.getStdin();          //也可以用PrintWriter out = new PrintWriter(session.getStdin());
                            //輸入命令
                            fout.write(("bash xlsbz_server/bin/telnet.sh" + "\n").getBytes());
                            Thread.sleep(1000);
                            fout.write(("freeze" + "\n").getBytes());
                            Thread.sleep(60000);
                            fout.write(("quit" + "\n").getBytes());
                            Thread.sleep(1000);
                            fout.write(("bash xlsbz_server/bin/xlsbz.sh" + "\n").getBytes());
                            Thread.sleep(1000);

                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }).start();

6關閉session和相應流

                            session.close();
                            Thread.sleep(1000);
                            conn.close();
                            Thread.sleep(1000);

補充一點:

有時候需要從遠程服務器獲取返回結果

final InputStream is = new StreamGobbler(session.getStdout());                      //StreamGobbler的作用是把session的標准輸出包裝成InputStream,用於接收目標服務器上的控制台返回結果,讀取br中的內容。然后在循環中,把每一行的內容添加到StringBuffer里面。

        new Thread() {
            public void run() {
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                while (true) {
                    try {
                        String line = br.readLine();
                        if (line == null) break;
                        System.out.println(line);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                try {
                    is.close();
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM