public void close() {
    try {
      channel.exit();
    } catch (Exception e) {
    }
    try {
      session.disconnect();
    } catch (Exception e) {

    }
  }
Esempio n. 2
0
 @Override
 public void send(String path, String filename, Binary content) throws IOException {
   Session session = null;
   Channel channel = null;
   ChannelSftp channelSftp = null;
   logger.debug("preparing the host information for sftp.");
   InputStream data = null;
   try {
     JSch jsch = new JSch();
     session = jsch.getSession(this.username, this.server, this.remotePort);
     if (this.password != null) {
       session.setPassword(this.password);
     }
     java.util.Properties config = new java.util.Properties();
     config.put("StrictHostKeyChecking", "no");
     session.setConfig(config);
     session.connect();
     logger.debug("Host connected.");
     channel = session.openChannel("sftp");
     channel.connect();
     logger.debug("sftp channel opened and connected.");
     channelSftp = (ChannelSftp) channel;
     if (path != null) {
       channelSftp.cd(path);
     }
     File f = new File(filename);
     data = content.getDataAsStream();
     channelSftp.put(data, f.getName());
     logger.info("File transfered successfully to host.");
   } catch (Exception ex) {
     throw new IOException("SFTP problem", ex);
   } finally {
     if (data != null) {
       try {
         data.close();
       } catch (IOException e) {
       }
     }
     if (channelSftp != null) {
       channelSftp.exit();
     }
     logger.info("sftp Channel exited.");
     if (channel != null) {
       channel.disconnect();
     }
     logger.info("Channel disconnected.");
     if (session != null) {
       session.disconnect();
     }
     logger.info("Host Session disconnected.");
   }
 }
Esempio n. 3
0
  /* (non-Javadoc)
   * @see net.sf.thingamablog.transport.PublishTransport#disconnect()
   */
  public boolean disconnect() {
    try {
      System.out.println("Disconnecting SFTP...");
      if (sftp != null) sftp.exit();
    } catch (Exception ioe) {
      logger.log(Level.WARNING, "Problem disconnecting from SFTP", ioe);
    }

    if (!isSavePassword()) setPassword("");

    System.out.println("Disconnected SFTP");
    logger.info("Disconnected SFTP");
    isConnected = false;
    return true;
  }
Esempio n. 4
0
  /**
   * @param filename The complete filename which will be after receiving file
   * @param user
   * @param host
   * @param password
   * @param remotFolder
   * @return
   */
  public static int getFileSFTP(
      String filename, String user, String host, String password, String remotFolder) {
    ResourceBundle rb = ResourceBundle.getBundle(EzLinkConstant.PATH_CONFIG_PROPERTY_FILE_NAME);
    File f = new File(filename);
    logger.error("SFTP file receive start");
    JSch jsch = new JSch();
    Session session = null;
    int error = 0;
    try {
      session = jsch.getSession(user, host, 22);
      session.setConfig("StrictHostKeyChecking", "no");
      session.setPassword(password);
      //		    UserInfo ui=new MyUserInfo();
      //		      session.setUserInfo(ui);
      session.connect();

      Channel channel = session.openChannel("sftp");
      channel.connect();
      ChannelSftp sftpChannel = (ChannelSftp) channel;
      logger.debug("receiving file:" + f.getAbsolutePath());
      SftpProgressMonitor monitor = new MyProgressMonitor();
      sftpChannel.cd(remotFolder);
      sftpChannel.lcd(f.getParent());
      sftpChannel.get(f.getName(), f.getName(), monitor, ChannelSftp.OVERWRITE);
      sftpChannel.exit();
      //		    session.disconnect();
      logger.error("SFTP file receive successfully");
    } catch (JSchException e) {
      logger.error(
          "File transfer Exception",
          e); // To change body of catch statement use File | Settings | File Templates.
      error = -1;
    } catch (SftpException e) {
      logger.error("SFTP Exception", e);
      error = -2;
      //		} catch (FileNotFoundException e) {
      //			logger.error("File not found to transfer"+filename);
    } finally {

      session.disconnect();
    }
    return error;
  }
  /**
   * This method should only be run when a new version of the Manager will be released. It tries to
   * make the process more automatic. Currently: - Writes in the Version file in LOCAL DISK (Dropbox
   * folder, so Dropbox must be running!) - Uploads the file to the correct folder tree in the
   * SourceForge project, creating all needed folder on the way.
   */
  public static void main(String[] args) throws ZipException, IOException, JSchException {

    /*
     * IMPORTANT
     *
     * Before releasing a new version of the Manager, please, follow these instructions:
     * 0 - Update the Changelog.txt and Version.txt files
     * 1 - Check if the older version is fully compatible with this one after an update. managerOptions.xml shall not be lost by any reasons.
     * 2 - Check if the file paths below are correct.
     * 3 - Clean and build the project, then Package-for-store the Manager.jar
     * 4 - Goto Sourceforge.net and update the LABEL and the OS supported for the new Manager file.
     * 5 - Update in the HoN forums page (changelog, topic title and version in first line)
     */

    // First step is to get the version we want to release.
    String targetVersion = ManagerOptions.getInstance().getVersion();
    // Get the old jar
    File oldJarVersion =
        new File(
            "C:\\Users\\Shirkit\\Dropbox\\HonModManager\\Dropbox\\Public\\versions\\Manager.jar");
    // And the newly generated one
    File newJarVersion = new File("store\\Manager.jar");
    // Target output for where the differences will be generated
    String verionsFile =
        "C:\\Users\\Shirkit\\Dropbox\\HonModManager\\Dropbox\\Public\\versions.txt";
    File rootVersionsFolder =
        new File("C:\\Users\\Shirkit\\Dropbox\\HonModManager\\Dropbox\\Public\\versions\\");
    File output = new File(rootVersionsFolder, targetVersion + ".jar");

    System.out.println("Version to be released=" + targetVersion);
    System.out.println("Old version file=" + oldJarVersion.getAbsolutePath());
    System.out.println("New version file=" + newJarVersion.getAbsolutePath());
    System.out.println();

    if (calculate(oldJarVersion, newJarVersion, output.getAbsolutePath())) {
      System.out.println(
          "Output file generated.\nPath="
              + output.getAbsolutePath()
              + "\nSize="
              + output.length() / 1024
              + "KB");

      // Read the current versions file and store it for later
      FileInputStream fis = new FileInputStream(verionsFile);
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      copyInputStream(fis, baos);
      String s = baos.toString();
      fis.close();
      baos.close();

      System.out.println(s);

      if (!s.trim().isEmpty()) {
        if (s.contains("\n")) {
          System.out.println("Older version=" + s.substring(0, s.indexOf("\n")));
        } else {
          System.out.println("Older version=" + s);
        }
        s = targetVersion + "\n" + s;
      } else {
        System.out.println("First version!");
        s = targetVersion;
      }

      if (JOptionPane.showConfirmDialog(
              null, "Confirm upload?", "Confirmation", JOptionPane.YES_NO_OPTION)
          != 0) {
        System.exit(0);
      }

      // Write new versions file with the new released version
      FileWriter fw = new FileWriter(verionsFile);
      fw.write(s);
      fw.flush();
      fw.close();
      System.out.println("Versions file written with sucess!");

      fis = new FileInputStream(newJarVersion);
      FileOutputStream fos =
          new FileOutputStream(rootVersionsFolder + File.separator + "Manager.jar");
      copyInputStream(fis, fos);
      fis.close();
      fos.close();
      System.out.println("Manager.jar file written!");

      System.out.println();
    } else {
      System.err.println("No differences file. Output file not generated.");
      System.exit(0);
    }

    JSch jsch = new JSch();
    Session session = null;
    try {
      System.out.println("Connecting to SF");

      session =
          jsch.getSession(
              JOptionPane.showInputDialog("SourceForge Username") + ",all-inhonmodman",
              "frs.sourceforge.net",
              22);
      session.setConfig("StrictHostKeyChecking", "no");
      session.setPassword(JOptionPane.showInputDialog("SourceForge Password"));
      session.connect();

      Channel channel = session.openChannel("sftp");
      channel.connect();
      ChannelSftp sftpChannel = (ChannelSftp) channel;
      System.out.println("Connected!");
      String root = "/home/frs/project/a/al/all-inhonmodman";
      sftpChannel.cd(root);
      StringTokenizer versionTokens = new StringTokenizer(targetVersion, " ");
      boolean flag = true;
      while (versionTokens.hasMoreTokens()) {
        String s = versionTokens.nextToken();
        if (!cdExists(sftpChannel, s)) {
          sftpChannel.mkdir(s);
          flag = false;
        }
        sftpChannel.cd(s);
      }
      if (flag) {
        System.err.println("Version already exists!");
        sftpChannel.exit();
        session.disconnect();
        System.exit(0);
      }
      System.out.println("Uploading file");
      OutputStream out = sftpChannel.put("Manager.jar");
      FileInputStream fis = new FileInputStream(newJarVersion);
      copyInputStream(fis, out);
      out.close();
      fis.close();
      System.out.println("Upload complete");
      sftpChannel.exit();
      session.disconnect();
      System.out.println("SUCESS!");
    } catch (JSchException e) {
      e.printStackTrace();
    } catch (SftpException e) {
      e.printStackTrace();
    }

    System.exit(0);
  }
Esempio n. 6
0
 @AfterMethod(groups = "spring")
 public void closeSftp() {
   sftpChannel.exit();
 }
Esempio n. 7
-1
  public static void main(String[] arg) {
    try {
      JSch jsch = new JSch();

      String user = "******";
      String host = "hddev-c01-edge-01";
      int port = 22;
      String privateKey = "src/main/resources/id_rsa";

      jsch.addIdentity(privateKey);

      System.out.println("identity added ");

      Session session = jsch.getSession(user, host, port);
      System.out.println("session created.");

      // disabling StrictHostKeyChecking may help to make connection but makes it insecure
      // see
      // http://stackoverflow.com/questions/30178936/jsch-sftp-security-with-session-setconfigstricthostkeychecking-no
      //
      java.util.Properties config = new java.util.Properties();
      config.put("StrictHostKeyChecking", "no");
      session.setConfig(config);

      session.connect();
      System.out.println("session connected.....");

      Channel channel = session.openChannel("sftp");
      channel.setInputStream(System.in);
      channel.setOutputStream(System.out);
      channel.connect();
      System.out.println("shell channel connected....");

      ChannelSftp c = (ChannelSftp) channel;

      String fileName = "src/main/resources/test.txt";
      c.put(fileName, "./in/");
      c.exit();
      System.out.println("done");

    } catch (Exception e) {
      System.err.println(e);
    }
  }