开发者

ssh through java

I am using java ssh tools to make an ssh connection to my school account and look for a file and then delete it. However I am creating three functions to do the same thing but with different files, I am looking for a way to do that at the same thing instead of doing one after the other. Here is some code. Basically I want to know if there is a way to do this all on one ssh connection or some kind of fork or multithreading.

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.se开发者_StackOverflow中文版tXForwarding(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);
    }
}


If you want to run multiple tasks perhaps ExpectJ would work better for you. ExpectJ uses JSCH under the covers, so this may make your life easier.

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();


As said by the commenters, better factor it out in separate functions. Then is gets more clear how to reuse the same connection (i.e. Session) for several commands.

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();
}

Then put the reading from the BufferedReader (it has a readLine method) in the method eatOutput, and have another method which opens the session and calls three times the method shown here (with different file names), and closes the session again.


In respect to the usage of ExpectJ, which is the best library to use in machine critical production application for expect based functionality?

ExpectJ or Expect4J library? Though, both library uses the JSch underneath! Or Apache SSHD - Client:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜