无法通过SSH从JAVA程序进行交互

时间:2011-10-20 06:54:49

标签: java linux ssh

我正在通过java程序与我的linux服务器进行SSH,我能够执行shell脚本等命令。但问题是每当我执行一个特定的shell脚本时,它提供了在是或否之间进行选择的选项,现在从我的java控制台我选择是,但这实际上并不是与linux服务器进行交互。如何通过java编程完成这项工作?如果你有任何代码,请分享。我正在为执行SSH操作代码。

    String arr1[]=new String[arr.length];
    ReadConfig rc=new ReadConfig();
    rc.readConfig();
    hostname=rc.hostname;
    username=rc.username;
    password=rc.password;
    for(int i=0;i<arr.length;i++)
    {
        arr1[i]= arr[i];
        System.out.println("the array value "+arr1[i]+" "+i);
    }

    try{
         String com=arr1[5].toString().trim();
         System.out.println("  "+com);
        /* Create a connection instance */

        conn = new Connection(hostname);

        /* Now connect */
                conn.connect();


                /* Authenticate.
         * If you get an IOException saying something like
         * "Authentication method password not supported by the server at this stage."
         * then please check the FAQ.
         */

            boolean isAuthenticated = conn.authenticateWithPassword(username, password);


                if (isAuthenticated == false)
                    throw new IOException("Authentication failed.");

                /* Create a session */

                sess = conn.openSession();
               try{
                  sess.execCommand(com);


                //obj.append("Pass");
               }catch(Exception e){


                   //obj.append("Fail");
               }
                //Session sess1 = conn.openSession();
                System.out.println("Here is some information about the remote host:");
                InputStream stdout = new StreamGobbler(sess.getStdout());
                BufferedReader br = new BufferedReader(new InputStreamReader(stdout));

                while (true)
                {
                    String line = br.readLine();
                    if (line == null)
                        break;
                    System.out.println(line);
                }
                /* Show exit status, if available (otherwise "null") */

                System.out.println("ExitCode: " + sess.getExitStatus());

    }catch (Exception e) {
        // TODO: handle exception
    }finally{
        /* Close this session */
    //obj.close();
        sess.close();
        /* Close the connection */

        conn.close();
    }

2 个答案:

答案 0 :(得分:1)

您没有在输入流中写任何内容。只需输入java控制台就什么都不做。

为会话stdin-OutputStream创建一个PrintWriter:

然后,如果需要,您可以out.println("y")。如果您需要使用java-console进行交互,则需要从System.in读取并将其发送到您的输出流。

PrintWriter out = createPW(new OutputStreamWriter(sess.getStdin()), true);

这是使用合适的行分隔符创建PrintWriter的一种不错的方法。

public static PrintWriter createPrintWriter(OutputStreamWriter out, boolean autoflush) {
    Properties props = System.getProperties();
    synchronized (props) {
        Object old = null;
        try {
            old = props.setProperty("line.separator", "\n");
            return new PrintWriter(out, autoflush);
        } finally {
            if (old != null) {
                props.put("line.separator", old);
            }
        }
    }
}

答案 1 :(得分:-2)

你可以使用“是”命令。管道输出从Yes输入到下一个命令

yes | apt-get install some_software