Linux SSH UsernamePasswordInteractive Login with Java ganymed-ssh2

引用
Ganymed SSH-2 for Java is a library which implements the SSH-2 protocol in pure Java, more details http://www.ganymed.ethz.ch/ssh2/


引用
Here is a full sample for how to do UsernamePasswordInteractive Login and run scp/shell comamnd


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.KnownHosts;
import ch.ethz.ssh2.SCPClient;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;


public class SSHUtils
{
    public static Connection getSSHConnection() throws IOException {
        KnownHosts database = new KnownHosts();
        Connection conn = new Connection("TARGET_LINUX_HOSTNAME");

        String[] hostkeyAlgos = database.getPreferredServerHostkeyAlgorithmOrder("TARGET_LINUX_HOSTNAME");

        if (hostkeyAlgos != null)
        {
            conn.setServerHostKeyAlgorithms(hostkeyAlgos);
        }

        conn.connect(new AdvancedVerifier());

        if (conn.isAuthMethodAvailable("USERNAME", "keyboard-interactive"))
        {
            // login for the first time
            UsernamePasswordInteractiveCallback il = new UsernamePasswordInteractiveCallback();
            boolean loginSuccess = conn.authenticateWithKeyboardInteractive("USERNAME", il);
            if (!loginSuccess)
            {
                System.out.println("login failed");
            }

        }
        return conn;
    }
    
    public static List<String> runCommand(final Connection conn,String command) throws IOException {
        //handle error stream in real case
        Session sess = conn.openSession();
        List<String> files = new ArrayList<String>();
        sess.execCommand(command);
        InputStream stdout = new StreamGobbler(sess.getStdout());
        BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
        while (true)
        {
            String line = br.readLine();
            if (line == null)
            {
                break;
            }
            if (!line.trim().isEmpty())
            {
                files.add(line.trim());
            }
        }
        sess.close();
        return files;
    }
    
    public static void copyFile(final Connection conn, String sourcePath, String targetPart) throws IOException {
        SCPClient scpClient = conn.createSCPClient();
        scpClient.get(sourcePath, targetPart);
    }
}


// EOF


public class UsernamePasswordInteractiveCallback implements InteractiveCallback
{

    @Override
    public String[] replyToChallenge(String arg0, String arg1, int arg2, String[] arg3, boolean[] arg4)
        throws Exception
    {
        final String[] password = new String[1];
        password[0] = "Password";
        return password;
    }

}


class AdvancedVerifier implements ServerHostKeyVerifier
{

    public boolean verifyServerHostKey(String hostname, int port, String serverHostKeyAlgorithm,
        byte[] serverHostKey) throws Exception
    {

        return true;
    }
}

猜你喜欢

转载自caerun.iteye.com/blog/2300910