2011-04-14 107 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...?