Java 类com.jcraft.jsch.OpenSSHConfig 实例源码

项目:openstack-java-sdk    文件:RemoteExecutor.java   
public void open() throws JSchException {
    JSch jsch = new JSch();
    String conf = (String)Configure.getConfigure().getString("SshConfPath");
    if (new File(conf).exists()) {
        try {
            ConfigRepository configRepository = OpenSSHConfig.parseFile(conf);
            jsch.setConfigRepository(configRepository);
        }catch(IOException ignore) {

        }
    }
    session = jsch.getSession(this.username, this.host, this.port);
    session.setConfig("StrictHostKeyChecking", "no");
    session.setPassword(this.password);
    session.setTimeout(timeout);
    session.connect();
    channelSftp = (ChannelSftp) session.openChannel("sftp");
    channelSftp.connect();
}
项目:tigervnc    文件:Tunnel.java   
private static void createTunnelJSch(String gatewayHost, String remoteHost,
                                     int remotePort, int localPort) throws Exception {
  JSch.setLogger(new MyJSchLogger());
  JSch jsch=new JSch();

  try {
    // NOTE: jsch does not support all ciphers.  User may be
    //       prompted to accept host key authenticy even if
    //       the key is in the known_hosts file.
    File knownHosts = new File(FileUtils.getHomeDir()+".ssh/known_hosts");
    if (knownHosts.exists() && knownHosts.canRead())
        jsch.setKnownHosts(knownHosts.getAbsolutePath());
    ArrayList<File> privateKeys = new ArrayList<File>();
    if (!getSshKey().isEmpty()) {
      byte[] keyPass = null, key;
      if (!sshKeyPass.getValue().isEmpty())
        keyPass = sshKeyPass.getValue().getBytes();
      jsch.addIdentity("TigerVNC", getSshKey().getBytes(), null, keyPass);
    } else if (!getSshKeyFile().isEmpty()) {
      File f = new File(getSshKeyFile());
      if (!f.exists() || !f.canRead())
        throw new Exception("Cannot access SSH key file "+getSshKeyFile());
      privateKeys.add(f);
    }
    for (Iterator<File> i = privateKeys.iterator(); i.hasNext();) {
      File privateKey = (File)i.next();
      if (privateKey.exists() && privateKey.canRead())
        if (!sshKeyPass.getValue().isEmpty())
            jsch.addIdentity(privateKey.getAbsolutePath(),
                           sshKeyPass.getValue());
        else
            jsch.addIdentity(privateKey.getAbsolutePath());
    }

    String user = getSshUser();
    String label = new String("SSH Authentication");
    PasswdDialog dlg =
      new PasswdDialog(label, (user == null ? false : true), false);
    dlg.userEntry.setText(user != null ? user : "");
    File ssh_config = new File(sshConfig.getValue());
    if (ssh_config.exists() && ssh_config.canRead()) {
      ConfigRepository repo =
        OpenSSHConfig.parse(ssh_config.getAbsolutePath());
      jsch.setConfigRepository(repo);
    }
    Session session=jsch.getSession(user, gatewayHost, getSshPort());
    session.setUserInfo(dlg);
    // OpenSSHConfig doesn't recognize StrictHostKeyChecking
    if (session.getConfig("StrictHostKeyChecking") == null)
      session.setConfig("StrictHostKeyChecking", "ask");
    session.connect();
    session.setPortForwardingL(localPort, remoteHost, remotePort);
  } catch (java.lang.Exception e) {
    throw new Exception(e.getMessage()); 
  }
}
项目:ssh-proxy    文件:SshConfiguration.java   
public static SshConfiguration getConfiguration() throws IOException, JSchException {
    Assert.isTrue(Files.isRegularFile(getLocalSshConfigPath()), getLocalSshConfigPath() + " does not exist");
    return new SshConfiguration(OpenSSHConfig.parseFile(getLocalSshConfigPath().toString()));
}
项目:ant-ivy    文件:AbstractSshBasedRepository.java   
/**
 * get a new session using the default attributes if the given String is a full uri, use the
 * data from the uri instead
 *
 * @param pathOrUri
 *            might be just a path or a full ssh or sftp uri
 * @return matching Session
 * @throws IOException if something goes wrong
 */
protected Session getSession(String pathOrUri) throws IOException {
    URI uri = parseURI(pathOrUri);
    String host = getHost();
    int port = getPort();
    String user = getUser();
    String userPassword = getUserPassword();
    String sshConfig = getSshConfig();
    File keyFile = getKeyFile();
    if (uri != null && uri.getScheme() != null) {
        if (uri.getHost() != null) {
            host = uri.getHost();
        }
        if (uri.getPort() != -1) {
            port = uri.getPort();
        }
        if (uri.getUserInfo() != null) {
            String userInfo = uri.getUserInfo();
            if (!userInfo.contains(":")) {
                user = userInfo;
            } else {
                user = userInfo.substring(0, userInfo.indexOf(":"));
                userPassword = userInfo.substring(userInfo.indexOf(":") + 1);
            }
        }
    }

    if (sshConfig != null) {
        ConfigRepository configRepository = OpenSSHConfig.parseFile(sshConfig);
        Config config = configRepository.getConfig(host);
        host = config.getHostname();
        if (user == null) {
            user = config.getUser();
        }
        String keyFilePath = config.getValue("IdentityFile");
        if (keyFilePath != null && keyFile == null) {
            keyFile = new File(keyFilePath);
        }
    }

    if (host == null) {
        throw new IllegalArgumentException(
                "missing host information. host should be provided either "
                        + "directly on the repository or in the connection URI "
                        + ", or in the openssh config file specified by sshConfig");
    }
    if (user == null) {
        Credentials c = requestCredentials(host);
        if (c != null) {
            user = c.getUserName();
            userPassword = c.getPasswd();
        } else {
            Message.error("username is not set");
        }
    }
    return SshCache.getInstance().getSession(host, port, user, userPassword, keyFile,
        getKeyFilePassword(), getPassFile(), isAllowedAgentUse());
}