Package ch.ethz.ssh2.packets

Examples of ch.ethz.ssh2.packets.TypesWriter


   
    try
    {
      /* Create a connection instance */

      Connection conn = new Connection(hostname);

      /* We want to connect through a HTTP proxy */
     
      conn.setProxyData(new HTTPProxyData(proxyHost, proxyPort));
     
      // if the proxy requires basic authentication:
      // conn.setProxyData(new HTTPProxyData(proxyHost, proxyPort, "username", "secret"));
     
      /* Now connect (through the proxy) */

      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 */

      Session sess = conn.openSession();

      sess.execCommand("uname -a && date && uptime && who");

      System.out.println("Here is some information about the remote host:");

      /*
       * This basic example does not handle stderr, which is sometimes dangerous
       * (please read the FAQ).
       */

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

      /* Close this session */

      sess.close();

      /* Close the connection */

      conn.close();

    }
    catch (IOException e)
    {
      e.printStackTrace(System.err);
View Full Code Here


    try
    {
      /* Create a connection instance */

      Connection conn = new Connection(hostname);

      /* Now connect */

      conn.connect();

      /* Authenticate */

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

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

      /* Create a session */

      Session sess = conn.openSession();

      sess.execCommand("echo \"Huge amounts of text on STDOUT\"; echo \"Huge amounts of text on STDERR\" >&2");

      /*
       * Advanced:
       * The following is a demo on how one can read from stdout and
       * stderr without having to use two parallel worker threads (i.e.,
       * we don't use the Streamgobblers here) and at the same time not
       * risking a deadlock (due to a filled SSH2 channel window, caused
       * by the stream which you are currently NOT reading from =).
       */

      /* Don't wrap these streams and don't let other threads work on
       * these streams while you work with Session.waitForCondition()!!!
       */

      InputStream stdout = sess.getStdout();
      InputStream stderr = sess.getStderr();

      byte[] buffer = new byte[8192];

      while (true)
      {
        if ((stdout.available() == 0) && (stderr.available() == 0))
        {
          /* Even though currently there is no data available, it may be that new data arrives
           * and the session's underlying channel is closed before we call waitForCondition().
           * This means that EOF and STDOUT_DATA (or STDERR_DATA, or both) may
           * be set together.
           */

          int conditions = sess.waitForCondition(ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA
              | ChannelCondition.EOF, 2000);

          /* Wait no longer than 2 seconds (= 2000 milliseconds) */

          if ((conditions & ChannelCondition.TIMEOUT) != 0)
          {
            /* A timeout occured. */
            throw new IOException("Timeout while waiting for data from peer.");
          }

          /* Here we do not need to check separately for CLOSED, since CLOSED implies EOF */

          if ((conditions & ChannelCondition.EOF) != 0)
          {
            /* The remote side won't send us further data... */

            if ((conditions & (ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA)) == 0)
            {
              /* ... and we have consumed all data in the local arrival window. */
              break;
            }
          }

          /* OK, either STDOUT_DATA or STDERR_DATA (or both) is set. */

          // You can be paranoid and check that the library is not going nuts:
          // if ((conditions & (ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA)) == 0)
          //  throw new IllegalStateException("Unexpected condition result (" + conditions + ")");
        }

        /* If you below replace "while" with "if", then the way the output appears on the local
         * stdout and stder streams is more "balanced". Addtionally reducing the buffer size
         * will also improve the interleaving, but performance will slightly suffer.
         * OKOK, that all matters only if you get HUGE amounts of stdout and stderr data =)
         */

        while (stdout.available() > 0)
        {
          int len = stdout.read(buffer);
          if (len > 0) // this check is somewhat paranoid
            System.out.write(buffer, 0, len);
        }

        while (stderr.available() > 0)
        {
          int len = stderr.read(buffer);
          if (len > 0) // this check is somewhat paranoid
            System.err.write(buffer, 0, len);
        }
      }

      /* Close this session */

      sess.close();

      /* Close the connection */

      conn.close();

    }
    catch (IOException e)
    {
      e.printStackTrace(System.err);
View Full Code Here

      Connection conn = new Connection(hostname);

      /* We want to connect through a HTTP proxy */
     
      conn.setProxyData(new HTTPProxyData(proxyHost, proxyPort));
     
      // if the proxy requires basic authentication:
      // conn.setProxyData(new HTTPProxyData(proxyHost, proxyPort, "username", "secret"));
     
      /* Now connect (through the proxy) */
 
View Full Code Here

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

      /* Create a session */

      Session sess = conn.openSession();

      sess.execCommand("uname -a && date && uptime && who");

      System.out.println("Here is some information about the remote host:");

      /*
       * This basic example does not handle stderr, which is sometimes dangerous
       * (please read the FAQ).
       */

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

      /* Close this session */

      sess.close();

      /* Close the connection */

      conn.close();

 
View Full Code Here

      /*
       * This basic example does not handle stderr, which is sometimes dangerous
       * (please read the FAQ).
       */

      InputStream stdout = new StreamGobbler(sess.getStdout());

      BufferedReader br = new BufferedReader(new InputStreamReader(stdout));

      while (true)
      {
View Full Code Here

  private final void closeHandle(byte[] handle) throws IOException
  {
    int req_id = generateNextRequestID();

    TypesWriter tw = new TypesWriter();
    tw.writeString(handle, 0, handle.length);

    sendMessage(Packet.SSH_FXP_CLOSE, req_id, tw.getBytes());

    expectStatusOKMessage(req_id);
  }
View Full Code Here

  {
    checkHandleValidAndOpen(handle);

    int req_id = generateNextRequestID();

    TypesWriter tw = new TypesWriter();
    tw.writeString(handle.fileHandle, 0, handle.fileHandle.length);

    if (debug != null)
    {
      debug.println("Sending SSH_FXP_FSTAT...");
      debug.flush();
    }

    sendMessage(Packet.SSH_FXP_FSTAT, req_id, tw.getBytes());

    byte[] resp = receiveMessage(34000);

    if (debug != null)
    {
View Full Code Here

  private SFTPv3FileAttributes statBoth(String path, int statMethod) throws IOException
  {
    int req_id = generateNextRequestID();

    TypesWriter tw = new TypesWriter();
    tw.writeString(path, charsetName);

    if (debug != null)
    {
      debug.println("Sending SSH_FXP_STAT/SSH_FXP_LSTAT...");
      debug.flush();
    }

    sendMessage(statMethod, req_id, tw.getBytes());

    byte[] resp = receiveMessage(34000);

    if (debug != null)
    {
View Full Code Here

   */
  public String readLink(String path) throws IOException
  {
    int req_id = generateNextRequestID();

    TypesWriter tw = new TypesWriter();
    tw.writeString(path, charsetName);

    if (debug != null)
    {
      debug.println("Sending SSH_FXP_READLINK...");
      debug.flush();
    }

    sendMessage(Packet.SSH_FXP_READLINK, req_id, tw.getBytes());

    byte[] resp = receiveMessage(34000);

    if (debug != null)
    {
View Full Code Here

   */
  public void setstat(String path, SFTPv3FileAttributes attr) throws IOException
  {
    int req_id = generateNextRequestID();

    TypesWriter tw = new TypesWriter();
    tw.writeString(path, charsetName);
    tw.writeBytes(createAttrs(attr));

    if (debug != null)
    {
      debug.println("Sending SSH_FXP_SETSTAT...");
      debug.flush();
    }

    sendMessage(Packet.SSH_FXP_SETSTAT, req_id, tw.getBytes());

    expectStatusOKMessage(req_id);
  }
View Full Code Here

TOP

Related Classes of ch.ethz.ssh2.packets.TypesWriter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.