/** * Creates SSHShell. * * @param host the host name * @param port the ssh port * @param userName the ssh user name * @param sshPrivateKey the ssh password * @return the shell * @throws JSchException * @throws IOException */ private SSHShell(String host, int port, String userName, byte[] sshPrivateKey) throws JSchException, IOException { Closure expectClosure = getExpectClosure(); for (String linuxPromptPattern : new String[]{"\\>", "#", "~#", "~\\$"}) { try { Match match = new RegExpMatch(linuxPromptPattern, expectClosure); linuxPromptMatches.add(match); } catch (MalformedPatternException malformedEx) { throw new RuntimeException(malformedEx); } } JSch jsch = new JSch(); jsch.setKnownHosts(System.getProperty("user.home") + "/.ssh/known_hosts"); jsch.addIdentity(host, sshPrivateKey, (byte[]) null, (byte[]) null); this.session = jsch.getSession(userName, host, port); this.session.setConfig("StrictHostKeyChecking", "no"); this.session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password"); session.connect(60000); this.channel = (ChannelShell) session.openChannel("shell"); this.expect = new Expect4j(channel.getInputStream(), channel.getOutputStream()); channel.connect(); }
/** * Creates SSHShell. * * @param host the host name * @param port the ssh port * @param userName the ssh user name * @param password the ssh password * @return the shell * @throws JSchException * @throws IOException */ private SSHShell(String host, int port, String userName, String password) throws JSchException, IOException { Closure expectClosure = getExpectClosure(); for (String linuxPromptPattern : new String[]{"\\>", "#", "~#", "~\\$"}) { try { Match match = new RegExpMatch(linuxPromptPattern, expectClosure); linuxPromptMatches.add(match); } catch (MalformedPatternException malformedEx) { throw new RuntimeException(malformedEx); } } JSch jsch = new JSch(); this.session = jsch.getSession(userName, host, port); session.setPassword(password); Hashtable<String, String> config = new Hashtable<>(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); session.connect(60000); this.channel = (ChannelShell) session.openChannel("shell"); this.expect = new Expect4j(channel.getInputStream(), channel.getOutputStream()); channel.connect(); }
/** * Creates SSHShell. * * @param host the host name * @param port the ssh port * @param userName the ssh user name * @param password the ssh password * @return the shell * @throws JSchException * @throws IOException */ private SshShell(String host, int port, String userName, String password) throws JSchException, IOException { Closure expectClosure = getExpectClosure(); for (String linuxPromptPattern : new String[]{"\\>","#", "~#", "~\\$"}) { try { Match match = new RegExpMatch(linuxPromptPattern, expectClosure); linuxPromptMatches.add(match); } catch (MalformedPatternException malformedEx) { throw new RuntimeException(malformedEx); } } JSch jsch = new JSch(); this.session = jsch.getSession(userName, host, port); session.setPassword(password); Hashtable<String,String> config = new Hashtable<>(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); session.connect(60000); this.channel = (ChannelShell) session.openChannel("shell"); this.expect = new Expect4j(channel.getInputStream(), channel.getOutputStream()); channel.connect(); }
/** * Get a {@link ChannelShell} * * @param channelSessionKey the session key that identifies the channel * @return {@link ChannelShell} * @throws Exception thrown by borrowObject and invalidateObject */ private ChannelShell getChannelShell(final ChannelSessionKey channelSessionKey) throws Exception { final long startTime = System.currentTimeMillis(); Channel channel; do { LOGGER.debug("borrowing a channel..."); channel = channelPool.borrowObject(channelSessionKey); if (channel != null) { LOGGER.debug("channel {} borrowed", channel.getId()); if (!channel.isConnected()) { try { LOGGER.debug("channel {} connecting...", channel.getId()); channel.connect(CHANNEL_CONNECT_TIMEOUT); LOGGER.debug("channel {} connected!", channel.getId()); } catch (final JSchException jsche) { LOGGER.error("Borrowed channel {} connection failed! Invalidating the channel...", channel.getId(), jsche); channelPool.invalidateObject(channelSessionKey, channel); } } else { LOGGER.debug("Channel {} already connected!", channel.getId()); } } if ((channel == null || !channel.isConnected()) && (System.currentTimeMillis() - startTime) > CHANNEL_BORROW_LOOP_WAIT_TIME) { final String errMsg = MessageFormat.format("Failed to get a channel within {0} ms! Aborting channel acquisition!", CHANNEL_BORROW_LOOP_WAIT_TIME); LOGGER.error(errMsg); throw new JschServiceException(errMsg); } } while (channel == null || !channel.isConnected()); return (ChannelShell) channel; }
@Test public void testTestCommand() throws JSchException, IOException { JSch jsch = new JSch(); Session session = jsch.getSession("admin", "localhost", properties.getShell().getPort()); jsch.addIdentity("src/test/resources/id_rsa"); Properties config = new Properties(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); session.connect(); ChannelShell channel = (ChannelShell) session.openChannel("shell"); PipedInputStream pis = new PipedInputStream(); PipedOutputStream pos = new PipedOutputStream(); channel.setInputStream(new PipedInputStream(pos)); channel.setOutputStream(new PipedOutputStream(pis)); channel.connect(); pos.write("test run bob\r".getBytes(StandardCharsets.UTF_8)); pos.flush(); verifyResponse(pis, "test run bob"); pis.close(); pos.close(); channel.disconnect(); session.disconnect(); }
@Test public void testResizeHandler(TestContext context) throws Exception { Async async = context.async(); termHandler = term -> { term.resizehandler(v -> { context.assertEquals(20, term.width()); context.assertEquals(10, term.height()); async.complete(); }); }; startShell(); Session session = createSession("paulo", "secret", false); session.connect(); ChannelShell channel = (ChannelShell) session.openChannel("shell"); channel.connect(); OutputStream out = channel.getOutputStream(); channel.setPtySize(20, 10, 20 * 8, 10 * 8); out.flush(); channel.disconnect(); session.disconnect(); }
public void connect(Connection connection, TerminalPanel remoteTerminalPanel) throws JSchException, IOException { String newKey = connection.getUser() + connection.getRemoteHost(); if (newKey.equals(key)) { return; } key = newKey; disconnect(); JSch jsch = new JSch(); session = jsch.getSession(connection.getUser(), connection.getRemoteHost(), 22); session.setPassword(connection.getPassword()); session.setConfig("StrictHostKeyChecking", "no"); session.connect(5000); ChannelShell channel = (ChannelShell) session.openChannel("shell"); channel.setPtyType("vt102"); OutputStream inputToChannel = channel.getOutputStream(); PrintStream printStream = new PrintStream(inputToChannel, true); remoteTerminalPanel.setPrintStream(printStream); InputStream outFromChannel = channel.getInputStream(); Runnable run = new TerminalWatcher(outFromChannel, remoteTerminalPanel.getTextArea()); thread = new Thread(run); thread.start(); channel.connect(); }
public static ChannelStreams startLoginShellSession(final ExecutionEnvironment env) throws IOException, JSchException, InterruptedException { JSchWorker<ChannelStreams> worker = new JSchWorker<ChannelStreams>() { @Override public ChannelStreams call() throws InterruptedException, JSchException, IOException { ChannelShell shell = (ChannelShell) ConnectionManagerAccessor.getDefault().openAndAcquireChannel(env, "shell", true); // NOI18N if (shell == null) { throw new IOException("Cannot open shell channel on " + env); // NOI18N } shell.setPty(false); InputStream is = shell.getInputStream(); InputStream es = new ByteArrayInputStream(new byte[0]); OutputStream os = shell.getOutputStream(); Authentication auth = Authentication.getFor(env); shell.connect(auth.getTimeout() * 1000); return new ChannelStreams(shell, is, es, os); } @Override public String toString() { return "shell session for " + env.getDisplayName(); // NOI18N } }; return start(worker, env, 2); }
public ScpClient(String host,String user,String password,boolean su,Notify notify) throws Exception{ this.hosts = host; this.user = user; session = connect(host, user, password); exec = (ChannelShell) session.openChannel("shell"); exec.setPtySize(160, 50, 1280, 800); input = exec.getInputStream(); output = exec.getOutputStream(); exec.connect(); this.notify = notify; get(); if(su){ getSU(password); } }
public ShellClient(String host, String user, String password, Notify notify) throws Exception{ this.hosts = host; this.user = user; this.password = password; session = this.connect(); shell = (ChannelShell) session.openChannel("shell"); shell.setPtySize(160, 50, 1280, 800); input = shell.getInputStream(); output = shell.getOutputStream(); shell.connect(); this.notify = notify; get(30); }
private Connection createJschChannel(Session session, InputStream jsch_in, OutputStream out) throws JSchException, IOException { final InputStream in; Channel channel; // open shell channel with vt100 term channel = session.openChannel("shell"); ((ChannelShell) channel).setPtyType("vt100"); ((ChannelShell) channel).setPty(true); channel.setInputStream(jsch_in); in = channel.getInputStream(); channel.connect(10000); // sleep 500s in case we don't have open channel permission and server // close the channel. try { Thread.sleep(500); } catch (InterruptedException e) { } if (!channel.isConnected()) { throw new NemoException( "channel has been closed by peer. maybe you don't have permission"); } final Session _session = session; final InputStream _in = this.createVT100Filter(in); final OutputStream _out = out; return new Connection() { boolean _active = true; @Override public void close() { _active = false; _session.disconnect(); } @Override public OutputStream getOutputStream() { return _out; } @Override public InputStream getInputStream() { return _in; } @Override public boolean isActive() { return _active && _session.isConnected(); } }; }
public ChannelShell getShell() throws JSchException { ChannelShell shell = (ChannelShell) session.openChannel("shell"); shell.setInputStream(System.in); shell.setOutputStream(System.out); shell.connect(); return shell; }
public boolean connect() { boolean ret = super.connect(); if (ret == false) { Log.d(log, "failed to connect..."); return ret; } try { Session session = super.getSession(); if((session != null) && (_state == CONNECTION_STATE.DISCONNECTED)) { Log.d(log, "SSH shell Connecting..."); _state = CONNECTION_STATE.CONNECTING; session.connect(5000); _channel = session.openChannel("shell"); _channel.setInputStream(_localIn, true); _channel.setOutputStream(_localOut, true); _channel.connect(5000); _channelShell = (ChannelShell)_channel; _state = CONNECTION_STATE.CONNECTED; Log.d(log, "SSH shell Connected"); ret = true; } } catch(JSchException e) { Log.e(log, "Exception caught while initiating shell connection", e); ret = false; _state = CONNECTION_STATE.DISCONNECTED; getUserInfo().handleException(e); } return ret; }
public ChannelShell getShellChannel(boolean waitIfNoAvailable) throws JSchException, IOException, InterruptedException { return (ChannelShell) acquireChannel("shell", waitIfNoAvailable); // NOI18N }
@Action(value = "/admin/setPtyType") public String setPtyType() { Long sessionId = AuthUtil.getSessionId(servletRequest.getSession()); if (SecureShellAction.getUserSchSessionMap() != null) { UserSchSessions userSchSessions = SecureShellAction.getUserSchSessionMap().get(sessionId); if (userSchSessions != null && userSchSessions.getSchSessionMap() != null) { SchSession schSession = userSchSessions.getSchSessionMap().get(id); ChannelShell channel = (ChannelShell) schSession.getChannel(); channel.setPtySize((int) Math.floor(userSettings.getPtyWidth() / 7.2981), (int) Math.floor(userSettings.getPtyHeight() / 14.4166), userSettings.getPtyWidth(), userSettings.getPtyHeight()); schSession.setChannel(channel); } } return null; }
@Override protected ChannelShell openChannel(Session session) throws JSchException { return (ChannelShell) session.openChannel("shell"); }
@Override protected void configureChannelShell(ChannelShell channel) { String lang = System.getenv().get("LANG"); channel.setEnv("LANG", lang != null ? lang : "en_US.UTF-8"); channel.setPtyType("xterm"); }
@Override protected void setPtySize(ChannelShell channel, int col, int row, int wp, int hp) { channel.setPtySize(col, row, wp, hp); }
void arrangeShellCommand(ShellCommand<ChannelShell> shellCommand, String errorMsg);