com.jcraft.jsch.ChannelShell Java Examples
The following examples show how to use
com.jcraft.jsch.ChannelShell.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SSHShell.java From azure-libraries-for-java with MIT License | 7 votes |
/** * 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(); }
Example #2
Source File: JschSupport.java From netbeans with Apache License 2.0 | 6 votes |
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); }
Example #3
Source File: SshShell.java From azure-libraries-for-java with MIT License | 6 votes |
/** * 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(); }
Example #4
Source File: SSHShell.java From azure-libraries-for-java with MIT License | 6 votes |
/** * 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(); }
Example #5
Source File: SshdShellAutoConfigurationWithPublicKeyAndBannerImageTest.java From sshd-shell-spring-boot with Apache License 2.0 | 6 votes |
@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"); try (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(); verifyResponseContains(pis, "test run bob"); } channel.disconnect(); session.disconnect(); }
Example #6
Source File: SSHServerTest.java From vertx-shell with Apache License 2.0 | 6 votes |
@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(); }
Example #7
Source File: WebSSHServiceImpl.java From mcg-helper with Apache License 2.0 | 5 votes |
private void connectToSSH(SSHConnectInfo sshConnectInfo, WebSSHData webSSHData, ServerSource serverSource, Session webSocketSession, String httpSessionId) throws JSchException, IOException { Properties config = new Properties(); config.put("StrictHostKeyChecking", "no"); com.jcraft.jsch.Session session = sshConnectInfo.getjSch().getSession(serverSource.getUserName(), serverSource.getIp(), serverSource.getPort()); session.setConfig(config); UserInfo ui = null; if(StringUtils.isEmpty(serverSource.getSecretKey())) { ui = new SSHUserInfo(serverSource.getPwd()); } else { ui = new SSHGoogleAuthUserInfo(serverSource.getSecretKey(), serverSource.getPwd()); } session.setUserInfo(ui); session.connect(6000); Channel channel = session.openChannel("shell"); ((ChannelShell)channel).setPtyType("vt100", webSSHData.getCols(), webSSHData.getRows(), webSSHData.getTerminalWidth(), webSSHData.getTerminalHeight()); channel.connect(5000); sshConnectInfo.setChannel(channel); transToSSH(channel, Constants.LINUX_ENTER); InputStream inputStream = channel.getInputStream(); try { byte[] buffer = new byte[1024]; int i = 0; while ((i = inputStream.read(buffer)) != -1) { sendMessage(webSocketSession, Arrays.copyOfRange(buffer, 0, i), httpSessionId); } } finally { session.disconnect(); channel.disconnect(); if (inputStream != null) { inputStream.close(); } } }
Example #8
Source File: JschServiceImpl.java From jwala with Apache License 2.0 | 5 votes |
/** * 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; }
Example #9
Source File: SSHShellUtil.java From mcg-helper with Apache License 2.0 | 4 votes |
public static String execute(String ip, int port, String userName, String password, String secretKey, String shell) throws JSchException, IOException { String response = null; JSch.setLogger(new ShellLogger()); JSch jsch = new JSch(); Session session = jsch.getSession(userName, ip, port); UserInfo ui = null; if(StringUtils.isEmpty(secretKey)) { ui = new SSHUserInfo(password); } else { ui = new SSHGoogleAuthUserInfo(secretKey, password); } session.setUserInfo(ui); session.connect(6000); Channel channel = session.openChannel("shell"); PipedInputStream pipedInputStream = new PipedInputStream(); PipedOutputStream pipedOutputStream = new PipedOutputStream(); pipedOutputStream.connect(pipedInputStream); Thread thread = new Thread(new MonitorShellUser(channel, shell, pipedOutputStream)); thread.start(); channel.setInputStream(pipedInputStream); PipedOutputStream shellPipedOutputStream = new PipedOutputStream(); PipedInputStream receiveStream = new PipedInputStream(); shellPipedOutputStream.connect(receiveStream); channel.setOutputStream(shellPipedOutputStream); ((ChannelShell)channel).setPtyType("vt100", 160, 24, 1000, 480); // dumb //((ChannelShell)channel).setTerminalMode("binary".getBytes(Constants.CHARSET)); // ((ChannelShell)channel).setEnv("LANG", "zh_CN.UTF-8"); try { channel.connect(); response = IOUtils.toString(receiveStream, "UTF-8"); }finally { // if(channel.isClosed()) { pipedOutputStream.close(); pipedInputStream.close(); shellPipedOutputStream.close(); receiveStream.close(); channel.disconnect(); session.disconnect(); } // } return response; }
Example #10
Source File: JSchChannelsSupport.java From netbeans with Apache License 2.0 | 4 votes |
public ChannelShell getShellChannel(boolean waitIfNoAvailable) throws JSchException, IOException, InterruptedException { return (ChannelShell) acquireChannel("shell", waitIfNoAvailable); // NOI18N }