public Session startSshSessionAndObtainSession(){
  Session session=null;
  try {
    JSch jsch=new JSch();
    if (sshMeta.getSshLoginType() == SshLoginType.KEY) {
      String workingDir=System.getProperty("user.dir");
      String privKeyAbsPath=workingDir + "/" + sshMeta.getPrivKeyRelativePath();
      logger.debug("use privkey: path: " + privKeyAbsPath);
      if (!PcFileNetworkIoUtils.isFileExist(privKeyAbsPath)) {
        throw new RuntimeException("file not found at " + privKeyAbsPath);
      }
      if (sshMeta.isPrivKeyUsePassphrase() && sshMeta.getPassphrase() != null) {
        jsch.addIdentity(privKeyAbsPath,sshMeta.getPassphrase());
      }
 else {
        jsch.addIdentity(privKeyAbsPath);
      }
    }
    session=jsch.getSession(sshMeta.getUserName(),targetHost,sshMeta.getSshPort());
    if (sshMeta.getSshLoginType() == SshLoginType.PASSWORD) {
      session.setPassword(sshMeta.getPassword());
    }
    session.setConfig("StrictHostKeyChecking","no");
  }
 catch (  Exception t) {
    throw new RuntimeException(t);
  }
  return session;
}

public Session startSshSessionAndObtainSession ( ) { Session session = null ; try { JSch jsch = new JSch ( ) ; if ( sshMeta . getSshLoginType ( ) == SshLoginType . KEY ) { String workingDir = System . getProperty ( "user.dir" ) ; String privKeyAbsPath = workingDir + "/" + sshMeta . getPrivKeyRelativePath ( ) ; logger . debug ( "use privkey: path: " + privKeyAbsPath ) ; if ( ! PcFileNetworkIoUtils . isFileExist ( privKeyAbsPath ) ) { throw new RuntimeException ( "file not found at " + privKeyAbsPath ) ; } if ( sshMeta . isPrivKeyUsePassphrase ( ) && sshMeta . getPassphrase ( ) != null ) { jsch . addIdentity ( privKeyAbsPath , sshMeta . getPassphrase ( ) ) ; } else { jsch . addIdentity ( privKeyAbsPath ) ; } } session = jsch . getSession ( sshMeta . getUserName ( ) , targetHost , sshMeta . getSshPort ( ) ) ; if ( sshMeta . getSshLoginType ( ) == SshLoginType . PASSWORD ) { session . setPassword ( sshMeta . getPassword ( ) ) ; } session . setConfig ( "StrictHostKeyChecking" , "no" ) ; } catch ( Exception t ) { throw new RuntimeException ( t ) ; } return session ; }
start ssh session and obtain session .
