Search in sources :

Example 41 with JSchException

use of com.jcraft.jsch.JSchException in project GNS by MobilityFirst.

the class UserAuthPubKey method main.

/**
   *
   * @param arg
   */
public static void main(String[] arg) {
    try {
        JSch jsch = new JSch();
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle("Choose your privatekey(ex. ~/.ssh/id_dsa)");
        chooser.setFileHidingEnabled(false);
        int returnVal = chooser.showOpenDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            System.out.println("You chose " + chooser.getSelectedFile().getAbsolutePath() + ".");
            //			 , "passphrase"
            jsch.addIdentity(//			 , "passphrase"
            chooser.getSelectedFile().getAbsolutePath());
        }
        String host = null;
        if (arg.length > 0) {
            host = arg[0];
        } else {
            host = JOptionPane.showInputDialog("Enter username@hostname", System.getProperty("user.name") + "@localhost");
        }
        String user = host.substring(0, host.indexOf('@'));
        host = host.substring(host.indexOf('@') + 1);
        Session session = jsch.getSession(user, host, 22);
        // username and passphrase will be given via UserInfo interface.
        UserInfo ui = new UserInfoPrompted();
        session.setUserInfo(ui);
        session.connect();
        Channel channel = session.openChannel("shell");
        channel.setInputStream(System.in);
        channel.setOutputStream(System.out);
        channel.connect();
    } catch (HeadlessException | JSchException e) {
        System.out.println(e);
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) JFileChooser(javax.swing.JFileChooser) UserInfoPrompted(edu.umass.cs.aws.networktools.UserInfoPrompted) HeadlessException(java.awt.HeadlessException) Channel(com.jcraft.jsch.Channel) UserInfo(com.jcraft.jsch.UserInfo) JSch(com.jcraft.jsch.JSch) Session(com.jcraft.jsch.Session)

Example 42 with JSchException

use of com.jcraft.jsch.JSchException in project voltdb by VoltDB.

the class SFTPSession method pipeListToShellCommand.

/**
     * Executes the given command with the given list as its input
     *
     * @param list input
     * @param command command to execute on remote host
     * @return the output of the command as a list
     * @throws {@link SSHException} when an error occurs during SSH
     *   command performed by this method
     */
public List<String> pipeListToShellCommand(final Collection<String> list, final String command) {
    Preconditions.checkArgument(command != null && !command.trim().isEmpty(), "specified empty or null command string");
    Preconditions.checkState(m_channel != null, "stale session");
    ChannelExec e = null;
    BufferedReader sherr = null;
    BufferedReader shout = null;
    List<String> shellout = new ArrayList<String>();
    try {
        try {
            e = (ChannelExec) m_channel.getSession().openChannel("exec");
        } catch (JSchException jex) {
            throw new SSHException("opening ssh exec channel", jex);
        }
        try {
            shout = new BufferedReader(new InputStreamReader(e.getInputStream(), Charsets.UTF_8));
        } catch (IOException ioex) {
            throw new SSHException("geting exec channel input stream", ioex);
        }
        try {
            sherr = new BufferedReader(new InputStreamReader(e.getErrStream(), Charsets.UTF_8));
        } catch (IOException ioex) {
            throw new SSHException("getting exec channel error stream", ioex);
        }
        if (list != null && !list.isEmpty()) {
            e.setInputStream(listAsInputStream(list));
        }
        e.setCommand(command);
        try {
            e.connect(5000);
            int retries = 50;
            while (!e.isClosed() && retries-- > 0) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException ignoreIt) {
                }
            }
            if (retries < 0) {
                throw new SSHException("'" + command + "' timed out");
            }
        } catch (JSchException jex) {
            throw new SSHException("executing '" + command + "'", jex);
        }
        try {
            String outputLine = shout.readLine();
            while (outputLine != null) {
                shellout.add(outputLine);
                outputLine = shout.readLine();
            }
        } catch (IOException ioex) {
            throw new SSHException("capturing '" + command + "' output", ioex);
        }
        if (e.getExitStatus() != 0) {
            try {
                String errorLine = sherr.readLine();
                while (errorLine != null) {
                    shellout.add(errorLine);
                    errorLine = sherr.readLine();
                }
            } catch (IOException ioex) {
                throw new SSHException("capturing '" + command + "' error", ioex);
            }
            throw new SSHException("error output from '" + command + "':\n\t" + join(shellout, "\n\t"));
        }
        if (m_log.isDebugEnabled()) {
            m_log.debug("SSH: " + command);
        }
    } finally {
        if (sherr != null)
            try {
                sherr.close();
            } catch (Exception ignoreIt) {
            }
        if (shout != null)
            try {
                shout.close();
            } catch (Exception ignoreIt) {
            }
    }
    return shellout;
}
Also used : JSchException(com.jcraft.jsch.JSchException) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ChannelExec(com.jcraft.jsch.ChannelExec) SftpException(com.jcraft.jsch.SftpException) IOException(java.io.IOException) JSchException(com.jcraft.jsch.JSchException)

Example 43 with JSchException

use of com.jcraft.jsch.JSchException in project cdap by caskdata.

the class SFTPInputStream method close.

public synchronized void close() throws IOException {
    if (closed) {
        return;
    }
    super.close();
    closed = true;
    if (!channel.isConnected()) {
        throw new IOException(E_CLIENT_NOTCONNECTED);
    }
    try {
        Session session = channel.getSession();
        channel.disconnect();
        session.disconnect();
    } catch (JSchException e) {
        throw new IOException(StringUtils.stringifyException(e));
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) IOException(java.io.IOException) Session(com.jcraft.jsch.Session)

Example 44 with JSchException

use of com.jcraft.jsch.JSchException in project bamboobsc by billchen198318.

the class SFtpClientUtils method put.

/**
	 * 本地檔案放到遠端SFTP上
	 * 	
	 * @param user
	 * @param password
	 * @param addr
	 * @param port
	 * @param localFile
	 * @param remoteFile
	 * @throws JSchException
	 * @throws SftpException
	 * @throws Exception
	 */
public static void put(String user, String password, String addr, int port, List<String> localFile, List<String> remoteFile) throws JSchException, SftpException, Exception {
    Session session = getSession(user, password, addr, port);
    Channel channel = session.openChannel("sftp");
    channel.connect();
    ChannelSftp sftpChannel = (ChannelSftp) channel;
    try {
        for (int i = 0; i < localFile.size(); i++) {
            String rf = remoteFile.get(i);
            String lf = localFile.get(i);
            logger.info("put local file: " + lf + " write to " + addr + " :" + rf);
            sftpChannel.put(lf, rf);
            logger.info("success write to " + addr + " :" + rf);
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    } finally {
        sftpChannel.exit();
        channel.disconnect();
        session.disconnect();
    }
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) Channel(com.jcraft.jsch.Channel) SftpException(com.jcraft.jsch.SftpException) JSchException(com.jcraft.jsch.JSchException) Session(com.jcraft.jsch.Session)

Example 45 with JSchException

use of com.jcraft.jsch.JSchException in project bamboobsc by billchen198318.

the class SFtpClientUtils method get.

/**
	 * 抓遠端檔案然後存到本機 , 單筆
	 * 
	 * @param user
	 * @param password
	 * @param addr
	 * @param port
	 * @param remoteFile
	 * @param localFile
	 * @throws JSchException
	 * @throws SftpException
	 * @throws Exception
	 */
public static void get(String user, String password, String addr, int port, String remoteFile, String localFile) throws JSchException, SftpException, Exception {
    Session session = getSession(user, password, addr, port);
    Channel channel = session.openChannel("sftp");
    channel.connect();
    ChannelSftp sftpChannel = (ChannelSftp) channel;
    logger.info("get remote file: " + remoteFile + " write to:" + localFile);
    try {
        sftpChannel.get(remoteFile, localFile);
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    } finally {
        sftpChannel.exit();
        channel.disconnect();
        session.disconnect();
    }
    File f = new File(localFile);
    if (!f.exists()) {
        f = null;
        logger.error("get remote file:" + remoteFile + " fail!");
        throw new Exception("get remote file:" + remoteFile + " fail!");
    }
    f = null;
    logger.info("success write:" + localFile);
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) Channel(com.jcraft.jsch.Channel) File(java.io.File) SftpException(com.jcraft.jsch.SftpException) JSchException(com.jcraft.jsch.JSchException) Session(com.jcraft.jsch.Session)

Aggregations

JSchException (com.jcraft.jsch.JSchException)52 IOException (java.io.IOException)25 Session (com.jcraft.jsch.Session)20 JSch (com.jcraft.jsch.JSch)15 Channel (com.jcraft.jsch.Channel)11 ChannelSftp (com.jcraft.jsch.ChannelSftp)11 ChannelExec (com.jcraft.jsch.ChannelExec)9 SftpException (com.jcraft.jsch.SftpException)9 InputStream (java.io.InputStream)7 ArrayList (java.util.ArrayList)7 UserInfo (com.jcraft.jsch.UserInfo)6 File (java.io.File)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 OutputStream (java.io.OutputStream)5 Properties (java.util.Properties)5 MachineException (org.eclipse.che.api.machine.server.exception.MachineException)5 SSHShell (com.microsoft.azure.management.samples.SSHShell)4 FileInputStream (java.io.FileInputStream)4 IStatus (org.eclipse.core.runtime.IStatus)4 Status (org.eclipse.core.runtime.Status)4