本文整理汇总了Java中com.jcraft.jsch.Channel.getInputStream方法的典型用法代码示例。如果您正苦于以下问题:Java Channel.getInputStream方法的具体用法?Java Channel.getInputStream怎么用?Java Channel.getInputStream使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.jcraft.jsch.Channel
的用法示例。
在下文中一共展示了Channel.getInputStream方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: executeCommand
import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
/**
* Executes a given command. It opens exec channel for the command and closes
* the channel when it's done.
*
* @param session ssh connection to a remote server
* @param command command to execute
* @return command output string if the command succeeds, or null
*/
private static String executeCommand(Session session, String command) {
if (session == null || !session.isConnected()) {
return null;
}
log.trace("Execute command {} to {}", command, session.getHost());
try {
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
channel.setInputStream(null);
InputStream output = channel.getInputStream();
channel.connect();
String result = CharStreams.toString(new InputStreamReader(output));
channel.disconnect();
log.trace("Result of command {} on {}: {}", command, session.getHost(), result);
return result;
} catch (JSchException | IOException e) {
log.error("Failed to execute command {} on {} due to {}", command, session.getHost(), e.toString());
return null;
}
}
示例2: executeCommand
import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
public void executeCommand(final String command) throws IOException { // Cliente SSH final
JSch jsch = new JSch();
Properties props = new Properties();
props.put("StrictHostKeyChecking", "no");
try {
Session session = jsch.getSession(user, host, 22);
session.setConfig(props);
session.setPassword(password);
session.connect();
java.util.logging.Logger.getLogger(RemoteShell.class.getName())
.log(Level.INFO, session.getServerVersion());
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
// Daqui para baixo é somente para imprimir a saida
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
InputStream in = channel.getInputStream();
channel.connect();
byte[] tmp = new byte[1024];
while (true) {
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0) {
break;
}
System.out.print(new String(tmp, 0, i));
}
if (channel.isClosed()) {
if (in.available() > 0) {
continue;
}
System.out
.println("exit-status: " + channel.getExitStatus());
break;
}
try {
Thread.sleep(1000);
} catch (Exception ee) {
}
}
channel.disconnect();
session.disconnect();
} catch (JSchException ex) {
java.util.logging.Logger.getLogger(RemoteShell.class.getName())
.log(Level.SEVERE, null, ex);
}
}
示例3: executeCommandReader
import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
/**
* Execute an ssh command on the server, without closing the session
* so that a Reader can be returned with streaming data from the server.
*
* @param command the command to execute.
* @return a Reader with streaming data from the server.
* @throws IOException if it is so.
* @throws SshException if there are any ssh problems.
*/
@Override
public synchronized Reader executeCommandReader(String command) throws SshException, IOException {
if (!isConnected()) {
throw new IllegalStateException("Not connected!");
}
try {
Channel channel = connectSession.openChannel("exec");
((ChannelExec)channel).setCommand(command);
InputStreamReader reader = new InputStreamReader(channel.getInputStream(), "utf-8");
channel.connect();
return reader;
} catch (JSchException ex) {
throw new SshException(ex);
}
}
示例4: connectAndExecute
import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
public static String connectAndExecute(String user, String host, String password, String command1) {
String CommandOutput = null;
try {
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, 22);
session.setPassword(password);
session.setConfig(config);
session.connect();
// System.out.println("Connected");
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command1);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
InputStream in = channel.getInputStream();
channel.connect();
byte[] tmp = new byte[1024];
while (true) {
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0)
break;
// System.out.print(new String(tmp, 0, i));
CommandOutput = new String(tmp, 0, i);
}
if (channel.isClosed()) {
// System.out.println("exit-status: " +
// channel.getExitStatus());
break;
}
try {
Thread.sleep(1000);
} catch (Exception ee) {
}
}
channel.disconnect();
session.disconnect();
// System.out.println("DONE");
} catch (Exception e) {
e.printStackTrace();
}
return CommandOutput;
}
示例5: testWrite
import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
@Test
public void testWrite() throws Exception {
termHandler = term -> {
term.write("hello");
};
startShell();
Session session = createSession("paulo", "secret", false);
session.connect();
Channel channel = session.openChannel("shell");
channel.connect();
Reader in = new InputStreamReader(channel.getInputStream());
int count = 5;
StringBuilder sb = new StringBuilder();
while (count > 0) {
int code = in.read();
if (code == -1) {
count = 0;
} else {
count--;
sb.append((char)code);
}
}
assertEquals("hello", sb.toString());
channel.disconnect();
session.disconnect();
}
示例6: testDifferentCharset
import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
@Test
public void testDifferentCharset(TestContext context) throws Exception {
termHandler = term -> {
term.write("\u20AC");
term.close();
};
startShell(new SSHTermOptions().setDefaultCharset("ISO_8859_1").setPort(5000).setHost("localhost").setKeyPairOptions(
new JksOptions().setPath("src/test/resources/server-keystore.jks").setPassword("wibble")).
setAuthOptions(new ShiroAuthOptions().setType(ShiroAuthRealmType.PROPERTIES).setConfig(
new JsonObject().put("properties_path", "classpath:test-auth.properties"))));
Session session = createSession("paulo", "secret", false);
session.connect();
Channel channel = session.openChannel("shell");
channel.connect();
InputStream in = channel.getInputStream();
int b = in.read();
context.assertEquals(63, b);
}
示例7: testAuthenticate
import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
@Test
public void testAuthenticate() throws Exception {
startShell();
for (boolean interactive : new boolean[]{false, true}) {
Session session = createSession("paulo", "secret", interactive);
session.connect();
Channel channel = session.openChannel("shell");
channel.connect();
InputStream in = channel.getInputStream();
byte[] out = new byte[2];
assertEquals(2, in.read(out));
assertEquals("% ", new String(out));
channel.disconnect();
session.disconnect();
}
}
示例8: sendCommand
import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
public String sendCommand(String command) throws JSchException, IOException {
StringBuilder outputBuffer = new StringBuilder();
Channel channel = sesConnection.openChannel("exec");
((ChannelExec)channel).setCommand(command);
InputStream commandOutput = channel.getInputStream();
channel.connect();
int readByte = commandOutput.read();
while(readByte != 0xffffffff)
{
outputBuffer.append((char)readByte);
readByte = commandOutput.read();
}
channel.disconnect();
return outputBuffer.toString();
}
示例9: doSingleTransfer
import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
private void doSingleTransfer() throws IOException, JSchException {
StringBuilder sb = new StringBuilder("scp -t ");
if (getPreserveLastModified()) {
sb.append("-p ");
}
if (getCompressed()) {
sb.append("-C ");
}
sb.append(remotePath);
final String cmd = sb.toString();
final Channel channel = openExecChannel(cmd);
try {
final OutputStream out = channel.getOutputStream();
final InputStream in = channel.getInputStream();
channel.connect();
waitForAck(in);
sendFileToRemote(localFile, in, out);
} finally {
if (channel != null) {
channel.disconnect();
}
}
}
示例10: doMultipleTransfer
import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
private void doMultipleTransfer() throws IOException, JSchException {
StringBuilder sb = new StringBuilder("scp -r -d -t ");
if (getPreserveLastModified()) {
sb.append("-p ");
}
if (getCompressed()) {
sb.append("-C ");
}
sb.append(remotePath);
final Channel channel = openExecChannel(sb.toString());
try {
final OutputStream out = channel.getOutputStream();
final InputStream in = channel.getInputStream();
channel.connect();
waitForAck(in);
for (Directory current : directoryList) {
sendDirectory(current, in, out);
}
} finally {
if (channel != null) {
channel.disconnect();
}
}
}
示例11: getPartitionStats
import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
/**
* Gets the list of partitions on the disk.
*
* @param session
* the Session
* @return the list of partitions on the disk
* @throws JSchException
* the j sch exception
* @throws IOException
* Signals that an I/O exception has occurred.
*/
private List<DiskPartitionInfo> getPartitionStats(Session session) throws JSchException, IOException {
Channel channel = getChannel(session, PARTITION_LIST_COMMAND);
InputStream in = null;
List<DiskPartitionInfo> partitions = null;
try {
in = channel.getInputStream();
partitions = new ArrayList<DiskPartitionInfo>();
ResultParser resultParser = new ResultParser();
resultParser.parseDiskPartitionResult(in, partitions);
} finally {
if (in != null) {
in.close();
}
channel.disconnect();
}
getVmStatsForPartition(session, partitions);
return partitions;
}
示例12: onOutput
import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
public void onOutput(Channel channel) {
try {
StringBuffer pbsOutput = new StringBuffer("");
InputStream inputStream = channel.getInputStream();
byte[] tmp = new byte[1024];
do {
while (inputStream.available() > 0) {
int i = inputStream.read(tmp, 0, 1024);
if (i < 0) break;
pbsOutput.append(new String(tmp, 0, i));
}
} while (!channel.isClosed()) ;
String output = pbsOutput.toString();
this.setStdOutputString(output);
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
示例13: runCommand
import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
public String runCommand(String command) throws Exception {
Log.i(TAG + " runCommand", command);
StringBuilder outputBuffer = new StringBuilder();
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
channel.connect();
InputStream commandOutput = channel.getInputStream();
int readByte = commandOutput.read();
while (readByte != 0xffffffff) {
outputBuffer.append((char) readByte);
readByte = commandOutput.read();
}
channel.disconnect();
String output = outputBuffer.toString();
Log.i(TAG + " runCommand", command + ", output: " + output);
return output;
}
示例14: scpFileFromRemoteServer
import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
/**
*
* Scp file from remote server
*
* @param host
* @param user
* @param password
* @param remoteFile
* @param localFile
* @throws JSchException
* @throws IOException
*/
public void scpFileFromRemoteServer(String host, String user, String password, String remoteFile, String localFile) throws JSchException, IOException {
String prefix = null;
if (new File(localFile).isDirectory()) {
prefix = localFile + File.separator;
}
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, 22);
// username and password will be given via UserInfo interface.
UserInfo userInfo = new UserInformation(password);
session.setUserInfo(userInfo);
session.connect();
// exec 'scp -f remoteFile' remotely
String command = "scp -f " + remoteFile;
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
// get I/O streams for remote scp
OutputStream out = channel.getOutputStream();
InputStream in = channel.getInputStream();
channel.connect();
byte[] buf = new byte[1024];
// send '\0'
buf[0] = 0;
out.write(buf, 0, 1);
out.flush();
readRemoteFileAndWriteToLocalFile(localFile, prefix, out, in, buf);
session.disconnect();
}
示例15: runShellCommand
import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
@Override
public RemoteCommandReturnInfo runShellCommand(RemoteSystemConnection remoteSystemConnection, String command, long timeout) {
final ChannelSessionKey channelSessionKey = new ChannelSessionKey(remoteSystemConnection, ChannelType.SHELL);
LOGGER.debug("channel session key = {}", channelSessionKey);
Channel channel = null;
try {
channel = getChannelShell(channelSessionKey);
final InputStream in = channel.getInputStream();
final OutputStream out = channel.getOutputStream();
LOGGER.debug("Executing command \"{}\"", command);
out.write(command.getBytes(StandardCharsets.UTF_8));
out.write(CRLF.getBytes(StandardCharsets.UTF_8));
out.write("echo 'EXIT_CODE='$?***".getBytes(StandardCharsets.UTF_8));
out.write(CRLF.getBytes(StandardCharsets.UTF_8));
out.write("echo -n -e '\\xff'".getBytes(StandardCharsets.UTF_8));
out.write(CRLF.getBytes(StandardCharsets.UTF_8));
out.flush();
return getShellRemoteCommandReturnInfo(command, in, timeout);
} catch (final Exception e) {
final String errMsg = MessageFormat.format("Failed to run the following command: {0}", command);
LOGGER.error(errMsg, e);
throw new JschServiceException(errMsg, e);
} finally {
if (channel != null) {
channelPool.returnObject(channelSessionKey, channel);
LOGGER.debug("channel {} returned", channel.getId());
}
}
}