public static Session getSession ( final ICommandLine cl ) throws Exception { JSch jsch = new JSch ( ) ; Session session = null ; int timeout = DEFAULT_TIMEOUT ; int port = cl . hasOption ( "port" ) ? Integer . parseInt ( cl . getOptionValue ( "port" ) ) : + DEFAULT_PORT ; String hostname = cl . getOptionValue ( "hostname" ) ; String username = cl . getOptionValue ( "username" ) ; String password = cl . getOptionValue ( "password" ) ; String key = cl . getOptionValue ( "key" ) ; if ( cl . hasOption ( "timeout" ) ) { try { timeout = Integer . parseInt ( cl . getOptionValue ( "timeout" ) ) ; } catch ( NumberFormatException e ) { throw new MetricGatheringException ( "Invalid numeric value for timeout." , Status . CRITICAL , e ) ; } } session = jsch . getSession ( username , hostname , port ) ; if ( key == null ) { session . setConfig ( "StrictHostKeyChecking" , "no" ) ; session . setPassword ( password ) ; } else { jsch . addIdentity ( key ) ; } session . connect ( timeout * 1000 ) ; return session ; }
Starts an ssh session