亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標(biāo)題: Ganymed SSH-2 for Java 示例 [打印本頁]

作者: meteorm    時(shí)間: 2010-02-01 10:29
標(biāo)題: Ganymed SSH-2 for Java 示例
The most often source of problems when executing a command with Session.execCommand() are missing/wrong set environment variables on the remote machine.

so don't use Session.execCommand(), instead aquire a pty (pseudo terminal) and then start a shell (use Session.requestPTY() and Session.startShell()). You then have to communicate with the shell process at the other end through stdin and stdout. However, you also have to implement terminal logic (e.g., escape sequence handling (unless you use a "dumb" pty), "expect-send" logic (output parsing, shell prompt detection), etc.).

package c1;
import ch.ethz.ssh2.ChannelCondition;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Test4 {
    public static void main(String args[]) {
     try
  {
   /* Create a connection instance */
   Connection conn = new Connection("127.0.0.1");
   
   /* Now connect */
   conn.connect();
   /* Authenticate */
   boolean isAuthenticated = conn.authenticateWithPassword("username","password");
   if (isAuthenticated == false)
    throw new IOException("Authentication failed. Please check hostname, username and password.");
   /* Create a session */
   Session sess = conn.openSession();
   // sess.execCommand("uname -a && date && uptime && who");
   System.out.println("start exec command.......");
   
   //sess.execCommand("echo \"Text on STDOUT\"; echo \"Text on STDERR\" >&2");
   //sess.execCommand("env");
            sess.requestPTY("bash");
            
            sess.startShell();
            
            
   InputStream stdout = new StreamGobbler(sess.getStdout());
   InputStream stderr = new StreamGobbler(sess.getStderr());

   BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(stdout));
   BufferedReader stderrReader = new BufferedReader(new InputStreamReader(stderr));
   
   //if you want to use sess.getStdin().write(), here is a sample
   //byte b[]={'e','n','v','\n'};
   //byte b[]={'e','x','i','t','\n'};
   //sess.getStdin().write(b)
/*   
String str="env";
   String str1="exit";
   System.out.println(str+str1);
   out.write(str.getBytes());
   out.write('\n');
   out.write(str1.getBytes());
   out.write('\n');
*/
   //we used PrintWriter, it makes things simple
   PrintWriter out =new PrintWriter(sess.getStdin());
   out.println("env");
   out.println("exit");
   out.close();
   sess.waitForCondition(ChannelCondition.CLOSED | ChannelCondition.EOF | ChannelCondition.EXIT_STATUS, 30000);
   
   System.out.println("Here is the output from stdout:");
   
   while (true)
   {
    String line = stdoutReader.readLine();
    if (line == null)
     break;
    System.out.println(line);
   }
   System.out.println("Here is the output from stderr:");
   while (true)
   {
    String line = stderrReader.readLine();
    if (line == null)
     break;
    System.out.println(line);
   }
   /* Show exit status, if available (otherwise "null") */
   System.out.println("ExitCode: " + sess.getExitStatus());
   sess.close();/* Close this session */   
   conn.close();/* Close the connection */

  }
  catch (IOException e)
  {
   e.printStackTrace(System.err);
   System.exit(2);
  }
  }
    }


本文來自ChinaUnix博客,如果查看原文請點(diǎn):http://blog.chinaunix.net/u/19919/showart_2166145.html




歡迎光臨 Chinaunix (http://72891.cn/) Powered by Discuz! X3.2