Package com.jcraft.jsch

Examples of com.jcraft.jsch.Session


     * @return session or null if not successful
     */
    public Session getSession(String host, int port, String username, String userPassword,
            File pemFile, String pemPassword, File passFile) throws IOException {
        Entry entry = getCacheEntry(username, host, port);
        Session session = null;
        if (entry != null) {
            session = entry.getSession();
        }
        if (session == null || !session.isConnected()) {
            Message.verbose(":: SSH :: connecting to " + host + "...");
            try {
                JSch jsch = new JSch();
                if (port != -1) {
                    session = jsch.getSession(username, host, port);
                } else {
                    session = jsch.getSession(username, host);
                }
                if (pemFile != null) {
                    jsch.addIdentity(pemFile.getAbsolutePath(), pemPassword);
                }
                session.setUserInfo(new CfUserInfo(host, username, userPassword, pemFile,
                        pemPassword, passFile));
                session.connect();
                Message.verbose(":: SSH :: connected to " + host + "!");
                setSession(username, host, port, session);
            } catch (JSchException e) {
                if (passFile != null && passFile.exists()) {
                    passFile.delete();
View Full Code Here


     * @see org.apache.ivy.plugins.repository.Repository#getResource(java.lang.String)
     */
    public SshResource resolveResource(String source) {
        Message.debug("SShRepository:resolveResource called: " + source);
        SshResource result = null;
        Session session = null;
        try {
            session = getSession(source);
            Scp myCopy = new Scp(session);
            Scp.FileInfo fileInfo = myCopy.getFileinfo(new URI(source).getPath());
            result = new SshResource(this, source, true, fileInfo.getLength(), fileInfo
View Full Code Here

     * @see org.apache.ivy.repository.Repository#list(java.lang.String)
     */
    public List list(String parent) throws IOException {
        Message.debug("SShRepository:list called: " + parent);
        ArrayList result = new ArrayList();
        Session session = null;
        ChannelExec channel = null;
        session = getSession(parent);
        channel = getExecChannel(session);
        URI parentUri = null;
        try {
View Full Code Here

     *
     * @see org.apache.ivy.repository.Repository#put(java.io.File, java.lang.String, boolean)
     */
    public void put(File source, String destination, boolean overwrite) throws IOException {
        Message.debug("SShRepository:put called: " + destination);
        Session session = getSession(destination);
        try {
            URI destinationUri = null;
            try {
                destinationUri = new URI(destination);
            } catch (URISyntaxException e) {
View Full Code Here

        Message.debug("SShRepository:get called: " + source + " to "
                + destination.getCanonicalPath());
        if (destination.getParentFile() != null) {
            destination.getParentFile().mkdirs();
        }
        Session session = getSession(source);
        try {
            URI sourceUri = null;
            try {
                sourceUri = new URI(source);
            } catch (URISyntaxException e) {
View Full Code Here

     * @param resource
     *            to stream
     * @return InputStream of the resource data
     */
    public InputStream openStream(SshResource resource) throws IOException {
        Session session = getSession(resource.getName());
        Scp scp = new Scp(session);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            scp.get(resource.getName(), os);
        } catch (IOException e) {
View Full Code Here

     * @return the ChannelSftp with which a connection is established
     * @throws IOException
     *             if any connection problem occurs
     */
    private ChannelSftp getSftpChannel(String pathOrUri) throws IOException {
        Session session = getSession(pathOrUri);
        String host = session.getHost();
        ChannelSftp channel = SshCache.getInstance().getChannelSftp(session);
        if (channel == null) {
            try {
                channel = (ChannelSftp) session.openChannel("sftp");
                channel.connect();
                Message.verbose(":: SFTP :: connected to " + host + "!");
                SshCache.getInstance().attachChannelSftp(session, channel);
            } catch (JSchException e) {
                IOException ex = new IOException(e.getMessage());
View Full Code Here

        if (isNotEmpty(sftpConfig.getKnownHostsFile())) {
            LOG.debug("Using knownhosts file: " + sftpConfig.getKnownHostsFile());
            jsch.setKnownHosts(sftpConfig.getKnownHostsFile());
        }

        final Session session = jsch.getSession(configuration.getUsername(), configuration.getHost(), configuration.getPort());

        if (isNotEmpty(sftpConfig.getStrictHostKeyChecking())) {
            LOG.debug("Using StrickHostKeyChecking: " + sftpConfig.getStrictHostKeyChecking());
            session.setConfig("StrictHostKeyChecking", sftpConfig.getStrictHostKeyChecking());
        }

        // set user information
        session.setUserInfo(new UserInfo() {
            public String getPassphrase() {
                return null;
            }

            public String getPassword() {
View Full Code Here

        if(new File(localFile).isDirectory()){
          prefix = localFile + File.separator;
        }
       
        JSch jsch = new JSch();
        Session session = jsch.getSession(userName, hostName, hostPort);

        session.setUserInfo(userInfo);
       
        logger.debug("A iniciar sess�o...");
        session.connect();
       
        // exec 'scp -f rfile' remotely
        String command = "scp -f "+remoteFile;
        Channel channel = session.openChannel("exec");
        ((ChannelExec)channel).setCommand(command);
       
        // get I/O streams for remote scp
        OutputStream out = channel.getOutputStream();
        InputStream in = channel.getInputStream();
       
        logger.debug("A abrir canal...");
        channel.connect();
        byte[] buf = new byte[1024];
       
        // send '\0'
        buf[0] = 0;
        out.write(buf, 0, 1);
        out.flush();
       
        while(true) {
          int c = checkAck(in, logger);
          if(c != 'C'){
            break;
          }
         
          // read '0644 '
          in.read(buf, 0, 5);
         
          long filesize = 0L;
          while(true) {
            if(in.read(buf, 0, 1) < 0){
              logger.error("Erro: read bytes count < 0");
              _error = true;
              break;
            }
            if(buf[0] == ' ') {
              break;
            }
            filesize = filesize * 10L + (long)(buf[0] - '0');
          }
         
          String file = null;
          for(int i = 0; ; i++){
            in.read(buf, i, 1);
            if(buf[i] == (byte)0x0a){
              file = new String(buf, 0, i);
              break;
            }
          }
         
          logger.debug("filesize=" + filesize+", file=" + file);
         
          // send '\0'
          buf[0] = 0;
          out.write(buf, 0, 1);
          out.flush();
         
          // read a content of localFile
          fos = new FileOutputStream(prefix == null ? localFile : prefix + file);
          int foo;
          while(true) {
            if(buf.length < filesize) {
              foo = buf.length;
            } else {
              foo = (int)filesize;
            }
            foo = in.read(buf, 0, foo);
            if(foo < 0) {
              logger.error("Erro: read bytes count < 0");
              _error = true;
              break;
            }
            fos.write(buf, 0, foo);
            filesize -= foo;
            if(filesize == 0L) {
              break;
            }
          }
          fos.close();
          fos = null;
         
          if(checkAck(in, logger) != 0) {
            logger.error("Erro ao finalizar ficheiro");
            _error = true;
          }
         
          // send '\0'
          buf[0] = 0;
          out.write(buf, 0, 1);
          out.flush();
        }
       
        logger.debug("A terminar sess�o...");
        session.disconnect();
      } catch(Exception e) {
        logger.error(e);
        try{
          if(fos != null) {
            fos.close();
View Full Code Here

        return consumer;
    }

    protected Session createSession() throws JSchException {
        final JSch jsch = new JSch();
        final Session session = jsch.getSession(getConfiguration().getUsername(), getConfiguration().getHost());
        // TODO there's got to be a better way to deal with accepting new hosts...
        session.setUserInfo(new UserInfo() {
            public String getPassphrase() {
                return null;
            }

            public String getPassword() {
View Full Code Here

TOP

Related Classes of com.jcraft.jsch.Session

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.