本文整理汇总了Java中com.jcraft.jsch.ChannelShell类的典型用法代码示例。如果您正苦于以下问题:Java ChannelShell类的具体用法?Java ChannelShell怎么用?Java ChannelShell使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ChannelShell类属于com.jcraft.jsch包,在下文中一共展示了ChannelShell类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testTestCommand
import com.jcraft.jsch.ChannelShell; //导入依赖的package包/类
@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();
}
开发者ID:anand1st,项目名称:sshd-shell-spring-boot,代码行数:24,代码来源:SshdShellAutoConfigurationWithPublicKeyAndBannerImageTest.java
示例2: SSHShell
import com.jcraft.jsch.ChannelShell; //导入依赖的package包/类
/**
* 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();
}
示例3: shell
import com.jcraft.jsch.ChannelShell; //导入依赖的package包/类
/**
* Open an SSH shell session and execute the specified script, using the provided stream for output.
*
* <pre><code> shell("ssh://user:[email protected]/work/dir/path", "ls", System.out);</code></pre>
*
* @param connectUri SSH connection URI
* @param script shell command string
* @param out output stream object
*/
public static void shell(String connectUri, String script, OutputStream out) {
try (SessionHolder<ChannelShell> session = new SessionHolder<>(ChannelType.SHELL, URI.create(connectUri));
PipedOutputStream pipe = new PipedOutputStream();
PipedInputStream in = new PipedInputStream(pipe);
PrintWriter pw = new PrintWriter(pipe)) {
if (session.getWorkDir() != null) {
pw.println("cd " + session.getWorkDir());
}
pw.println(script);
pw.println("exit");
pw.flush();
shell(session, in, out);
} catch (IOException e) {
throw new RemoteInputStreamInstantiationException(e);
}
}
示例4: SshShell
import com.jcraft.jsch.ChannelShell; //导入依赖的package包/类
/**
* 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();
}
示例5: SSHShell
import com.jcraft.jsch.ChannelShell; //导入依赖的package包/类
/**
* 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();
}
示例6: getChannelShell
import com.jcraft.jsch.ChannelShell; //导入依赖的package包/类
/**
* 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;
}
示例7: testResizeHandler
import com.jcraft.jsch.ChannelShell; //导入依赖的package包/类
@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();
}
示例8: connect
import com.jcraft.jsch.ChannelShell; //导入依赖的package包/类
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();
}
示例9: getExpect
import com.jcraft.jsch.ChannelShell; //导入依赖的package包/类
private Expect4j getExpect() {
try {
log.debug(String.format("Start logging to %[email protected]%s:%s",user,ip,port));
JSch jsch = new JSch();
session = jsch.getSession(user, ip, port);
session.setPassword(password);
Hashtable<String, String> config = new Hashtable<String, String>();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
localUserInfo ui = new localUserInfo();
session.setUserInfo(ui);
session.connect();
channel = (ChannelShell) session.openChannel("shell");
Expect4j expect = new Expect4j(channel.getInputStream(), channel
.getOutputStream());
channel.connect();
log.debug(String.format("Logging to %[email protected]%s:%s successfully!",user,ip,port));
return expect;
} catch (Exception ex) {
log.error("Connect to "+ip+":"+port+"failed,please check your username and password!");
ex.printStackTrace();
}
return null;
}
示例10: startLoginShellSession
import com.jcraft.jsch.ChannelShell; //导入依赖的package包/类
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);
}
示例11: ScpClient
import com.jcraft.jsch.ChannelShell; //导入依赖的package包/类
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);
}
}
示例12: ShellClient
import com.jcraft.jsch.ChannelShell; //导入依赖的package包/类
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);
}
示例13: createJschChannel
import com.jcraft.jsch.ChannelShell; //导入依赖的package包/类
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();
}
};
}
示例14: getShell
import com.jcraft.jsch.ChannelShell; //导入依赖的package包/类
public ChannelShell getShell() throws JSchException {
ChannelShell shell = (ChannelShell) session.openChannel("shell");
shell.setInputStream(System.in);
shell.setOutputStream(System.out);
shell.connect();
return shell;
}
示例15: connect
import com.jcraft.jsch.ChannelShell; //导入依赖的package包/类
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;
}