Package ch.ethz.ssh2

Examples of ch.ethz.ssh2.Connection


    }

    public void connect(final String host, final int port, final String username,
            final String password, final String privateKeyPath) throws IOException {
        /* Create a connection instance */
        final Connection conn = new Connection(host, port);

        /* 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 triedCustomPublicKey = false;
        boolean triedStandardDSAPublicKey = false;
        boolean triedStandardRSAPublicKey = false;
        boolean triedPassword = false;
        boolean triedPasswordInteractive = false;

        final String effectiveUserName;
        // if the username is null, try to resolve it from the system.
        if (username == null) {
            effectiveUserName = System.getProperty("user.name");
            if (effectiveUserName == null) {
                throw new RuntimeException("Username cannot be auto-resolved from the system. Please supply username");
            }
        } else {
            effectiveUserName = username;
        }

        boolean isAuthenticated = false;
        if (privateKeyPath != null) {
            triedCustomPublicKey = true;
            isAuthenticated = conn.authenticateWithPublicKey(effectiveUserName,
                    new File(privateKeyPath), password);
        } else {
            File home = new File(System.getProperty("user.home"));
            try {
                triedStandardDSAPublicKey = true;
                isAuthenticated = conn.authenticateWithPublicKey(effectiveUserName,
                       new File(home, ".ssh/id_dsa"), password);
            } catch (IOException ex) {
                // dsa key probably can't be found
            }
            if (!isAuthenticated) {
                try {
                    triedStandardRSAPublicKey = true;
                    isAuthenticated = conn.authenticateWithPublicKey(effectiveUserName,
                            new File(home, ".ssh/id_rsa"), password);
                } catch (IOException ex) {
                    // rsa key probably can't be found
                }
            }
        }

        if (!isAuthenticated) {
            try {
                triedPassword = true;
                isAuthenticated = conn.authenticateWithPassword(effectiveUserName, password);
            } catch (IOException ex) {
                // Password authentication probably not supported
            }
            if (!isAuthenticated) {
                try {
                    triedPasswordInteractive = true;
                    isAuthenticated = conn.authenticateWithKeyboardInteractive(effectiveUserName, new InteractiveCallback() {
           
                        public String[] replyToChallenge(String name, String instruction, int numPrompts, String[] prompt, boolean[] echo) throws Exception {
                            String[] responses = new String[numPrompts];
                            for (int x=0; x < numPrompts; x++) {
                                responses[x] = password;
View Full Code Here



    public String execute(String cmd) throws RemoteExecuteException {
        StringBuilder result = new StringBuilder();
        try {
            Connection conn = new Connection(this.ip);
            conn.connect();
            boolean isAuthenticated = conn.authenticateWithPassword(this.user, this.password);
            if (isAuthenticated == false) {
                result.append("ERROR: Authentication Failed !");
            }

            Session session = conn.openSession();

            session.execCommand(cmd);
            BufferedReader read =
                    new BufferedReader(new InputStreamReader(new StreamGobbler(session.getStdout()), "GBK"));
            String line = "";
            while ((line = read.readLine()) != null) {
                result.append(line).append("\r\n");
            }
            session.close();
            conn.close();
            return result.toString();
        }
        catch (Throwable e) {
            throw new RemoteExecuteException("ִ���������", e);
        }
View Full Code Here

    boolean testConnection() throws Exception {
        try {
            /* Create a connection instance */

            conn = new Connection(hostname);

            /* Now connect */

            conn.connect();
            boolean isAuthenticated = conn.authenticateWithPublicKey(username, new File(pemPath), password);
View Full Code Here

   */
  protected Connection getBaseAuthentication() throws Exception {

    try { // to connect and authenticate
      boolean isAuthenticated = false;
      this.setSshConnection(new Connection(this.host, this.port));

      if (proxyHost != null && this.proxyHost.length() > 0) {
        if (this.proxyUser != null && this.proxyUser.length() > 0) {
          this.getSshConnection().setProxyData(new HTTPProxyData(this.proxyHost, this.proxyPort));
        }
View Full Code Here

    @SuppressWarnings("unused")
    final String conMethodName = conClassName + "::Connect";

    try {
      isConnected = false;
      this.setSshConnection(new Connection(pstrHostName, pintPortNumber));

    }
    catch (Exception e) {
      if (this.getSshConnection() != null)
        try {
View Full Code Here

    try {
      isConnected = false;
      String strHostName = objCO.getHost().Value();
      int intPortNo = objCO.getPort().value();
      this.setSshConnection(new Connection(strHostName, intPortNo));

      if (objCO.getProxy_host().IsNotEmpty()) {
        HTTPProxyData objProxy = null;
        if (objCO.getProxy_user().IsEmpty()) {
          objProxy = new HTTPProxyData(objCO.getProxy_host().Value(), objCO.getProxy_port().value());
View Full Code Here

     */
    public Connection getBaseAuthentication() throws Exception {
       
        try { // to connect and authenticate
            boolean isAuthenticated = false;
            this.setSshConnection(new Connection(this.getHost(), this.getPort()));
               
            if (this.getProxyHost() != null && this.getProxyHost().length() > 0) {
                if (this.getProxyUser() != null && this.getProxyUser().length() > 0) {
                    this.getSshConnection().setProxyData(new HTTPProxyData(this.getProxyHost(), this.getProxyPort()));
                } else {
View Full Code Here

    }
   
    public void connect() throws Exception{
      try { // to connect and authenticate
            boolean isAuthenticated = false;
            sshConnection = new Connection(this.getHost(), this.getPort());
               
            if (this.getProxyHost() != null && this.getProxyHost().length() > 0) {
                if (this.getProxyUser() != null && this.getProxyUser().length() > 0) {                   
                    sshConnection.setProxyData(new HTTPProxyData(this.getProxyHost(), this.getProxyPort(), this.getProxyUser(), this.getProxyPassword()));
                } else {
View Full Code Here

    try {
      host = phost;
      port = pport;
      logger.debug(String.format("Try to connect to host '%1$s' at Port '%2$d'.", host, port));
      if (isConnected() == false) {
        sshConnection = new Connection(host, port);
        sshConnection.connect();
        isConnected = true;
        logger.debug(String.format("Connected to '%1$s' at Port '%2$d'.", host, port));
        LogReply();
      }
View Full Code Here

    final String conMethodName = conClassName + "::getBaseAuthentication";

    try { // to connect and authenticate
      boolean isAuthenticated = false;
      this.setSshConnection(new Connection(Options().host.Value(), objOptions.port.value()));

      if (objOptions.proxy_host.IsEmpty() == false) {
        if (objOptions.proxy_user.IsEmpty() == false) {
          this.getSshConnection().setProxyData(new HTTPProxyData(objOptions.proxy_host.Value(), objOptions.proxy_port.value()));
        }
View Full Code Here

TOP

Related Classes of ch.ethz.ssh2.Connection

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.