protected static Session openConnectionSession(Connection conn) throws IOException, InterruptedException { Session sess = conn.openSession(); return sess; }
Session sshSession = null; try { sshSession = sshConnection.openSession();
public NetconfHelper(String ip, String username, String password) throws CloudRuntimeException { _connection = SSHCmdHelper.acquireAuthorizedConnection(ip, username, password); if (_connection == null) { throw new CloudRuntimeException("Error opening ssh connection."); } try { _session = _connection.openSession(); _session.startSubSystem("xmlagent"); exchangeHello(); } catch (final Exception e) { disconnect(); s_logger.error("Failed to connect to device SSH server: " + e.getMessage()); throw new CloudRuntimeException("Failed to connect to SSH server: " + _connection.getHostname()); } }
@Test public void openConnectionSessionTest() throws IOException, InterruptedException { Connection conn = Mockito.mock(Connection.class); PowerMockito.mockStatic(Thread.class); SshHelper.openConnectionSession(conn); Mockito.verify(conn).openSession(); PowerMockito.verifyStatic(); } }
session = data.conn.openSession(); if ( log.isDebug() ) { logDebug( BaseMessages.getString( PKG, "SSH.Log.SessionOpened" ) );
protected static Session openConnectionSession(final Connection conn) throws IOException { return conn.openSession(); }
public SshSession openSession() throws IOException { Session session = myConnection.openSession(); if (session != null) { mySessionCount++; return new SshSession(this, session); } return null; }
private Session getSession() { try { Session session = conn.openSession(); session.requestPTY("dumb"); session.startShell(); return session; } catch (Exception e) { String msg = "\nOpen SSH2 Session Error !"; logger.error(msg, e); throw new RuntimeException(msg, e); } }
public int executeRemoteCommand(String cmd, OutputStream os) { Session session = null; try { session = connection.openSession(); int status = connection.exec(cmd, os); if (status != 0) { throw new RuntimeException("Failed to execute command: " + cmd + ", exit code = " + status); } return status; } catch (InterruptedException | IOException e) { throw new AssertionError(e); } finally { if (session != null) { session.close(); } } }
public Session openSession() throws IOException { synchronized (SVNSSHSession.class) { Session session = myConnection.openSession(); if (session != null) { mySessionCount++; } SVNDebugLog.getDefaultLog().logFine(SVNLogType.NETWORK, ourRequestor + ": SESSION OPENED: " + this + "." + mySessionCount); return session; } }
private void initSession() { try { session = conn.openSession(); session.requestDumbPTY(); session.startShell(); stdoutReader = new BufferedReader(new InputStreamReader( session.getStdout())); stderrReader = new BufferedReader(new InputStreamReader( session.getStderr())); out = new PrintWriter(session.getStdin()); } catch (Exception e) { String msg = "\nOpen SSH2 Session Error !"; logger.error(msg, e); throw new RuntimeException(msg, e); } }
/** * Create a SFTP v3 client. * * @param conn The underlying SSH-2 connection to be used. * @param debug * @throws IOException * * @deprecated this constructor (debug version) will disappear in the future, * use {@link #SFTPv3Client(Connection)} instead. */ public SFTPv3Client(Connection conn, PrintStream debug) throws IOException { if (conn == null) throw new IllegalArgumentException("Cannot accept null argument!"); this.conn = conn; this.debug = debug; if (debug != null) debug.println("Opening session and starting SFTP subsystem."); sess = conn.openSession(); sess.startSubSystem("sftp"); is = sess.getStdout(); os = new BufferedOutputStream(sess.getStdin(), 2048); if ((is == null) || (os == null)) throw new IOException("There is a problem with the streams of the underlying channel."); init(); }
public Session openSession() throws IOException { lock(Thread.currentThread()); try { Session session = myConnection.openSession(); if (session != null) { mySessionCount++; } SVNDebugLog.getDefaultLog().logFine(SVNLogType.NETWORK, ourRequestor + ": SESSION OPENED: " + this + "." + mySessionCount); return session; } finally { unlock(); } }
/** * Create a SFTP v3 client. * * @param conn The underlying SSH-2 connection to be used. * @param debug * @throws IOException * * @deprecated this constructor (debug version) will disappear in the future, * use {@link #SFTPv3Client(Connection)} instead. */ public SFTPv3Client(Connection conn, PrintStream debug) throws IOException { if (conn == null) throw new IllegalArgumentException("Cannot accept null argument!"); this.conn = conn; this.debug = debug; if (debug != null) debug.println("Opening session and starting SFTP subsystem."); sess = conn.openSession(); sess.startSubSystem("sftp"); is = sess.getStdout(); os = new BufferedOutputStream(sess.getStdin(), 2048); if ((is == null) || (os == null)) throw new IOException("There is a problem with the streams of the underlying channel."); init(); }
@Override public void execCmd(RemoteCmd cmd) { List<String> cmds = cmd.getCmds(); String ip = remoteCmdClientConfig.getIp(); Session session = null; for (String commandLine : cmds) { try { session = connection.openSession(); session.execCommand(commandLine); } catch (IOException e) { log.error(String.format("Close session %s failed!", ip)); } finally { if (session != null) { session.close(); } } } }
public Channel launchChannel(String[] cmd, OutputStream out, FilePath _workDir, Map<String, String> envVars) throws IOException, InterruptedException { printCommandLine(cmd, _workDir); final Session session = connection.openSession(); session.execCommand(makeCommandLine(Arrays.asList(cmd), _workDir)); return new Channel("channel over ssh on "+connection.getHostname()+":"+connection.getPort(), Computer.threadPoolForRemoting, session.getStdout(), new BufferedOutputStream(session.getStdin())); }
@Override public RemoteCmdClientStream execCmdGetStream(RemoteCmd cmd) { Session session = null; String ip = remoteCmdClientConfig.getIp(); if (!isReady()) { log.error(String.format("remoteCmdClient %s is not ready!", ip)); return new RemoteCmdClientStream(null, ip); } try { session = connection.openSession(); } catch (IOException e) { log.error(String.format("Open session %s failed!", ip)); return new RemoteCmdClientStream(null, ip); } try { session.execCommand(cmd.getCmd()); } catch (IOException e) { log.error(String.format("execCommand %s failed!", ip)); return new RemoteCmdClientStream(null, ip); } return new RemoteCmdClientStream(session, ip); }
public Channel launchChannel(String[] cmd, OutputStream out, FilePath _workDir, Map<String, String> envVars) throws IOException, InterruptedException { printCommandLine(cmd, _workDir); final Session session = connection.openSession(); session.execCommand(makeCommandLine(Arrays.asList(cmd), _workDir)); return new Channel("channel over ssh on "+connection.getHostname()+":"+connection.getPort(), Computer.threadPoolForRemoting, session.getStdout(), new BufferedOutputStream(session.getStdin())); }
@Test public void openConnectionSessionTest() throws IOException, InterruptedException { final Connection conn = Mockito.mock(Connection.class); PowerMockito.mockStatic(Thread.class); SshHelper.openConnectionSession(conn); Mockito.verify(conn).openSession(); PowerMockito.verifyStatic(); } }
/** * Executes a process remotely and blocks until its completion. * * @param command the command * @param output The stdout/stderr will be sent to this stream. * @return the int * @throws IOException the io exception * @throws InterruptedException the interrupted exception */ public int exec(String command, OutputStream output) throws IOException, InterruptedException { Session session = openSession(); try { session.execCommand(command); PumpThread t1 = new PumpThread(session.getStdout(), output); t1.start(); PumpThread t2 = new PumpThread(session.getStderr(), output); t2.start(); session.getStdin().close(); t1.join(); t2.join(); // wait for some time since the delivery of the exit status often gets delayed session.waitForCondition(ChannelCondition.EXIT_STATUS,3000); Integer r = session.getExitStatus(); if(r!=null) return r.intValue(); return -1; } finally { session.close(); } }