2011-04-14 68 views
0

我使用java ssh工具來連接到我的學校帳戶的ssh連接,然後找到一個文件然後刪除它。然而,我創建了三個函數來完成同樣的事情,但使用不同的文件,我正在尋找一種方法來做同樣的事情,而不是一個接一個地做。這是一些代碼。基本上我想知道是否有一種方法可以在一個ssh連接或某種分叉或多線程上完成此操作。ssh通過java

public void updateinterval() { 
    try { 
     JSch jsch = new JSch(); 

     String user = "***********"; 
     String host = "********"; 
     Session session = jsch.getSession(user, host, 22); 

     session.setPassword("*********"); 

     // username and password will be given via UserInfo interface. 
     UserInfo userInfo = new SftpUserInfo(); 

     session.setUserInfo(userInfo); 

     session.connect(); 

     // look for a file named feedinterval 

     String checkfortimeupdate = 
       "cd public_html/final;grep '-send' feedinterval"; 

     Channel channel = session.openChannel("exec"); 
     ((ChannelExec) channel).setCommand(checkfortimeupdate); 

     // X Forwarding 
     // channel.setXForwarding(true); 

     // channel.setInputStream(System.in); 
     channel.setInputStream(null); 

     // channel.setOutputStream(System.out); 

     // FileOutputStream fos=new FileOutputStream("/tmp/stderr"); 
     // ((ChannelExec)channel).setErrStream(fos); 
     ((ChannelExec) channel).setErrStream(System.err); 

     InputStream in = channel.getInputStream(); 

     channel.connect(); 

     byte[] tmp = new byte[1024]; 

     while (true) { 
      while (in.available() > 0) { 
       int i = in.read(tmp, 0, 1024); 
       if (i < 0) 
        break; 
       String returned = new String(tmp, 0, i); 

       String argument = returned.substring(6); 

       if (returned.contains("-send")) { 

        // if its there its calls the removeinterval function 
        // which removes the file it found 
        // by doing the same thing this function does but with a 
        // different ssh command 

        arduinoupdate hey = new arduinoupdate(); 

        hey.removeinterval(); 

        try { 
         Runtime rt = Runtime.getRuntime(); 

         String[] commands = { 
           "system.exe", "-send", argument }; 

         Process proc = rt.exec(commands); 
        } 

        catch (IOException e) { 
        } 
       } 
      } 

      if (channel.isClosed()) { 
       System.out.println("UpdateInterval Closed exit-status: " 
         + channel.getExitStatus()); 
       break; 
      } 
      try { 
       /* Thread.sleep(1000); */ 
      } catch (Exception ee) { 
      } 
     } 
     channel.disconnect(); 
     session.disconnect(); 
    } catch (Exception e) { 
     System.out.println(e); 
    } 
} 
+1

請至少花幾分鐘正確格式化您的代碼。使用編輯器上的「{}」按鈕,並確保您的代碼在預覽中可讀。 – Mat 2011-04-14 22:06:35

+1

你能格式化你的代碼或刪除不必要的片段嗎? – smas 2011-04-14 22:07:25

+1

就像一面,我建議把這個功能分解成1-4行小方法以提高可讀性。 – Mike 2011-04-14 22:34:43

回答

1

如果您想運行多個任務,或許ExpectJ會更適合您。 ExpectJ在封面上使用JSCH,所以這可能會讓你的生活更輕鬆。

final ExpectJ expectJ = new ExpectJ(); 
final Spawn spawn = expectJ.spawn("host", 22, "user", "pass"); 
spawn.send("cd public_html/final\n"); 
spawn.expect("someReturn"); 
... 
spawn.send("exit\n"); 
spawn.expectClose(); 
0

正如評論者所說,更好的因素在單獨的功能。 然後更清楚地知道如何重複使用同一個連接(即Session)幾個命令。

public void updateInterval(Session s, String filename) { 
    String checkfortimeupdate = "fgrep '-send' \"public_html/final/" + filename + "\""; 

    ChannelExec channel = (ChannelExec)session.openChannel("exec"); 
    channel.setCommand(checkfortimeupdate); 

    channel.setInputStream(null); 
    channel.setErrStream(System.err); 
    BufferedReader in = new BufferedReader(new InputStreamReader(channel.getInputStream(), encoding)); 

    channel.connect(); 

    eatOutput(in); 

    if (channel.isClosed()) { 
     System.out.println("UpdateInterval Closed exit-status: " 
          + channel.getExitStatus()); 
     break; 
    } 
    channel.disconnect(); 
} 

然後把讀數從BufferedReader中(它有一個readLine方法)的方法eatOutput,並且具有打開的會話,並呼籲三次這裏顯示的方法(用不同的文件名)的另一種方法,和再次關閉會話。

0

關於ExpectJ的使用, 這是機器關鍵生產應用中用於預期功能的最佳庫嗎?

ExpectJ or Expect4J library?儘管這兩個庫都在下面使用JSch! 或Apache SSHD - 客戶端:

SshClient client = SshClient.setUpDefaultClient(); PipedOutputStream PipedIn...?