Package org.vngx.jsch.exception

Examples of org.vngx.jsch.exception.JSchException


   * @throws JSchException
   */
  static void delPort(Session session, String address, int localPort) throws JSchException {
    PortWatcher pw = getPort(session, address, localPort);
    if( pw == null ) {
      throw new JSchException("PortForwardingL: local port " + address + ":" + localPort + " is not registered");
    }
    pw.delete();
    PORT_WATCHER_POOL.remove(pw);
  }
View Full Code Here


      _boundAddress = InetAddress.getByName(address);
      _serverSocket = (factory == null)
          ? new ServerSocket(localPort, 0, _boundAddress)
          : factory.createServerSocket(localPort, 0, _boundAddress);
    } catch(Exception e) {
      throw new JSchException("PortForwardingL: local port " + address + ":" + localPort + " cannot be bound", e);
    }
    if( localPort == 0 ) {
      int assigned = _serverSocket.getLocalPort();
      if( assigned != -1 ) {
        _localPort = assigned;
View Full Code Here

   * @param password for connecting
   * @throws JSchException if any errors occur
   */
  public void connect(int connectTimeout, byte[] password) throws JSchException {
    if( _connected ) {
      throw new JSchException("Session is already connected");
    }
    JSch.getLogger().log(Logger.Level.INFO, "Connecting to " + _host + " port " + _port);

    try {
      /* Create the socket to the remote host and set the input/output
       * streams. If a proxy instance is set, use the proxy for creating
       * the socket. If socket factory is set, use factory for creating
       * socket, otherwise use default socket factory.
       */
      _io = new IO()// Create new IO instance for current connection
      if( _proxy == null ) {
        _socket = _socketFactory.createSocket(_host, _port, connectTimeout);
        _io.setInputStream(_socketFactory.getInputStream(_socket));
        _io.setOutputStream(_socketFactory.getOutputStream(_socket));
        _socket.setTcpNoDelay(true);
      } else {
        synchronized( _proxyLock ) {
          _proxy.connect(_socketFactory, _host, _port, connectTimeout);
          _socket = _proxy.getSocket();
          _io.setInputStream(_proxy.getInputStream());
          _io.setOutputStream(_proxy.getOutputStream());
        }
      }

      // Set the socket timeout for reads if timeout is greater than zero
      if( connectTimeout > 0 && _socket != null ) {
        _socket.setSoTimeout(connectTimeout);
      }
      _connected = true;    // Set connected as true (socket is connected)
      _sessionIO = SessionIO.createIO(this, _io.in, _io._out);
      JSch.getLogger().log(Logger.Level.INFO, "Connection established");

      // Exchange version information (send client version, read server version)
      _versionExchange.exchangeVersions(_io.in, _io._out);
      JSch.getLogger().log(Logger.Level.INFO, "Server SSH version: " + getServerVersion());
      JSch.getLogger().log(Logger.Level.INFO, "Client SSH version: " + getClientVersion());

      // Create key exchange and start kex process
      _keyExchange = new KeyExchange(this);
      _sessionId = _keyExchange.runFirstKex();
      _sessionIO.initNewKeys(_keyExchange);

      // Perform user authentication
      if( !UserAuth.authenticateUser(this, password) ) {
        throw new JSchException("User was not authenticated");
      }
      _authenticated = true;

      // Updates the socket timeout to the session timeout, replacing any
      // local timeout set in the connect(timeout) method
      if( (connectTimeout > 0 || _timeout > 0) && _timeout != connectTimeout ) {
        _socket.setSoTimeout(_timeout);
      }

      synchronized( _writeLock ) {
        if( _connected ) {
          _connectThread = _threadFactory.newThread(this);
          _connectThread.setName("Connect thread " + _host + " session");
          _connectThread.setDaemon(_daemonThread);
          _connectThread.start();
        }
      }
    } catch(Exception e) {
      if( _keyExchange != null ) {
        _keyExchange.kexCompleted();
      }
      if( _connected ) {
        try {
          // Make best effort to notify server we are disconnecting
          Buffer buffer = new Buffer(500);
          Packet packet = new Packet(buffer);
          packet.reset();
          buffer.putByte(SSH_MSG_DISCONNECT);
          buffer.putInt(SSH_DISCONNECT_KEY_EXCHANGE_FAILED)// TODO Value should be dynamic
          buffer.putString(e.toString());
          buffer.putString("en");
          write(packet);
        } catch(Exception ee) {
          /* Ignore close error. */
        } finally {
          disconnect()// Ensure disconnect in finally!
        }
      }
      _connected = false;
      if( e instanceof JSchException ) {
        throw (JSchException) e;
      }
      throw new JSchException("Failed to connect session: " + e, e);
    } finally {
      Util.bzero(password);
    }
  }
View Full Code Here

   *      opened or if any other unspecified error occurs
   */
  @SuppressWarnings("unchecked")
  public <T extends Channel> T openChannel(ChannelType type) throws JSchException {
    if( !_connected ) {
      throw new JSchException("Failed to open channel, session is closed");
    }
    try {
      Channel channel = type.createChannel(this);
      channel.init();
      return (T) channel;
    } catch(Exception e) {
      throw new JSchException("Failed to open channel: "+type, e);
    }
  }
View Full Code Here

        buffer.getInt();
        buffer.getShort();
        int reasonCode = buffer.getInt();
        byte[] description = buffer.getString();
        byte[] language = buffer.getString();
        throw new JSchException("SSH_MSG_DISCONNECT: " + reasonCode +
            " " + Util.byte2str(description) + " " + Util.byte2str(language));
      } else if( type == SSH_MSG_IGNORE ) {
        /* Ignore packet as per SSH spec. */
      } else if( type == SSH_MSG_UNIMPLEMENTED ) {
        buffer.getInt();
View Full Code Here

        globalBuffer.putString(ChannelForwardedTCPIP.normalize(bindAddress));
        globalBuffer.putInt(remotePort);
        write(globalPacket);
      } catch(Exception e) {
        _globalRequest.setThread(null);
        throw new JSchException("Failed to set port forwarding: "+e, e);
      }

      int count = 0, reply = _globalRequest.getReply();
      while( count < 10 && reply == -1 ) {
        try {
          Thread.sleep(1000)// TODO Make response wait value configurable
        } catch(Exception e) { /* Ignore error. */ }
        count++;
        reply = _globalRequest.getReply();
      }
      _globalRequest.setThread(null)// Resets reply value as well
      if( reply != 1 ) {
        throw new JSchException("Remote port forwarding failed for listen port " + remotePort);
      }
    }
  }
View Full Code Here

   * @param timeout in milliseconds (zero for no timeout)
   * @throws JSchException if any errors occur
   */
  public void setTimeout(int timeout) throws JSchException {
    if( timeout < 0 ) {
      throw new JSchException("Invalid timeout value: "+timeout);
    }
    if( _socket != null ) {
      try {
        _socket.setSoTimeout(timeout);
      } catch(Exception e) {
        throw new JSchException("Failed to set socket timeout: "+e, e);
      }
    }
    _timeout = timeout;
  }
View Full Code Here

      // if user doesn't accept the new key
      if( _userinfo != null ) {
        if( "ask".equals(shkc) ) {
          if( !_userinfo.promptYesNo(String.format(MessageConstants.PROMPT_REPLACE_KEY,
              kex._hostKeyType.DISPLAY_NAME, Util.getFingerPrint(kex.K_S), file)) ) {
            throw new JSchException("HostKey has changed (StrictHostKeyChecking:ask): "+chost);
          }
        } else // shkc.equals("yes")
          _userinfo.showMessage(String.format(MessageConstants.INVALID_SERVER_HOST,
              kex._hostKeyType.DISPLAY_NAME, Util.getFingerPrint(kex.K_S), file));
          throw new JSchException("HostKey has changed (StrictHostKeyChecking:yes): "+chost);
        }
      }

      // Remove the old key from the repository
      synchronized ( hkr ) {
        hkr.remove(chost, kex._hostKeyType, null);
        insert = true;
      }
    }

    if( ("ask".equals(shkc) || "yes".equals(shkc)) && keyCheck != Check.OK && !insert ) {
      if( "yes".equals(shkc) ) {
        throw new JSchException("HostKey does not match known hosts (StrictHostKeyChecking:yes): "+chost);
      }
      if( _userinfo != null ) {
        if( !_userinfo.promptYesNo(String.format(MessageConstants.PROMPT_UNKNOWN_KEY,
            chost, kex._hostKeyType.DISPLAY_NAME, Util.getFingerPrint(kex.K_S))) ) {
          throw new JSchException("HostKey does not match known hosts (StrictHostKeyChecking:ask): "+chost);
        }
        insert = true;
      } else {
        if( keyCheck == Check.NOT_INCLUDED ) {
          throw new JSchException("UnknownHostKey: "+chost+". "+kex._hostKeyType+" key fingerprint is "+Util.getFingerPrint(kex.K_S));
        } else {
          throw new JSchException("HostKey has been changed (StrictHostKeyChecking:ask): " + chost);
        }
      }
    }

    if( "no".equals(shkc) && keyCheck == Check.NOT_INCLUDED ) {
View Full Code Here

          sb.append(':');
        }
      }
      return sb.toString();
    } catch(Exception e) {
      throw new JSchException("Failed to generate fingerprint", e);
    }
  }
View Full Code Here

        if( currentLine.startsWith(SSHConstants.SSH_VERSION_2_0) || currentLine.startsWith(SSHConstants.SSH_VERSION_1_99) ) {
          _serverVersion = currentLine;  // Set the server version
          return;              // and return from exchange
        }
        // Unsupported version returned from server (not 2.0 or 1.99)
        throw new JSchException("Unsupported server version: "+Util.sanitize(currentLine), SSH_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED);
      }
    }
    // If no supported version has been found after max read attempts, throw
    // an exception as this might have been a denial of service on client
    throw new JSchException("Invalid server version response: exceeds maximum read attempts", SSH_DISCONNECT_PROTOCOL_ERROR);
  }
View Full Code Here

TOP

Related Classes of org.vngx.jsch.exception.JSchException

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.