本文整理汇总了Java中com.jcraft.jsch.Channel.disconnect方法的典型用法代码示例。如果您正苦于以下问题:Java Channel.disconnect方法的具体用法?Java Channel.disconnect怎么用?Java Channel.disconnect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.jcraft.jsch.Channel
的用法示例。
在下文中一共展示了Channel.disconnect方法的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: getRemoteFileList
import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public static Vector<LsEntry> getRemoteFileList(String user, String password, String addr, int port, String cwd) throws JSchException,
SftpException, Exception {
Session session = getSession(user, password, addr, port);
Vector<LsEntry> lsVec=null;
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
try {
lsVec=(Vector<LsEntry>)sftpChannel.ls(cwd); //sftpChannel.lpwd()
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
sftpChannel.exit();
channel.disconnect();
session.disconnect();
}
return lsVec;
}
示例3: forceStopJavaApplication
import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
/**
* Stops java application that needs to, this should be called when the user wants to manually stop the application
* so that it kills the remote java process
*
* @param session
* @param isRunningAsRoot
* @param mainClass
* @throws JSchException
* @throws IOException
*/
public void forceStopJavaApplication(@Nullable Session session, @NotNull boolean isRunningAsRoot, @NotNull String mainClass) throws JSchException, IOException {
if (session == null) {
return;
}
String javaKillCmd = String.format("%s kill -9 $(ps -efww | grep \"%s\"| grep -v grep | tr -s \" \"| cut -d\" \" -f2)",
isRunningAsRoot ? "sudo" : "", mainClass);
Channel channel = session.openChannel("shell");
OutputStream inputstream_for_the_channel = channel.getOutputStream();
PrintStream commander = new PrintStream(inputstream_for_the_channel, true);
channel.connect();
commander.println(javaKillCmd);
commander.close();
channel.disconnect();
session.disconnect();
}
示例4: rename
import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
@Override
public boolean rename(String newName){
Channel channel = null;
try {
channel = SFTPSession.getInstance().getSFTPChannel(mUri);
((ChannelSftp)channel).rename(mUri.getPath(), new File(new File(mUri.getPath()).getParentFile(), newName).getAbsolutePath());
channel.disconnect();
return true;
} catch (Exception e) {
e.printStackTrace();
}finally {
if(channel!=null&&channel.isConnected())
channel.disconnect();
}
return false;
}
示例5: move
import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
@Override
public boolean move(Uri uri) {
if(!mUri.getScheme().equals(uri.getScheme())|| !mUri.getHost().equals(uri.getHost())||mUri.getPort()!=uri.getPort())
return false;
Channel channel = null;
try {
channel = SFTPSession.getInstance().getSFTPChannel(mUri);
((ChannelSftp)channel).rename(mUri.getPath(),uri.getPath());
channel.disconnect();
return true;
} catch (Exception e) {
e.printStackTrace();
}finally {
if(channel!=null&&channel.isConnected())
channel.disconnect();
}
return false;
}
示例6: exists
import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
@Override
public boolean exists() {
Channel channel = null;
try {
channel = SFTPSession.getInstance().getSFTPChannel(mUri);
SftpATTRS attrs = ((ChannelSftp)channel).stat(mUri.getPath());
channel.disconnect();
return attrs !=null;
} catch (Exception e) {
if(channel!=null&&channel.isConnected())
channel.disconnect();
e.printStackTrace();
}finally {
if(channel!=null&&channel.isConnected())
channel.disconnect();
}
return false;
}
示例7: 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);
}
}
示例8: 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;
}
示例9: put
import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
/**
* 本地檔案放到遠端SFTP上
*
* @param user
* @param password
* @param addr
* @param port
* @param localFile
* @param remoteFile
* @throws JSchException
* @throws SftpException
* @throws Exception
*/
public static void put(String user, String password, String addr, int port,
List<String> localFile, List<String> remoteFile) throws JSchException, SftpException, Exception {
Session session = getSession(user, password, addr, port);
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
try {
for (int i=0; i<localFile.size(); i++) {
String rf=remoteFile.get(i);
String lf=localFile.get(i);
logger.info("put local file: " + lf + " write to " + addr + " :" + rf );
sftpChannel.put(lf, rf);
logger.info("success write to " + addr + " :" + rf);
}
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
sftpChannel.exit();
channel.disconnect();
session.disconnect();
}
}
示例10: testRead
import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
@Test
public void testRead(TestContext context) throws Exception {
Async async = context.async();
termHandler = term -> {
term.stdinHandler(s -> {
context.assertEquals("hello", s);
async.complete();
});
};
startShell();
Session session = createSession("paulo", "secret", false);
session.connect();
Channel channel = session.openChannel("shell");
channel.connect();
OutputStream out = channel.getOutputStream();
out.write("hello".getBytes());
out.flush();
channel.disconnect();
session.disconnect();
}
示例11: 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();
}
示例12: 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();
}
}
}
示例13: 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();
}
}
}
示例14: 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;
}
示例15: getNetworkLatency
import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
/**
* Returns the network latency between two nodes.
*
* @param session
* the Session to node-1
* @param node2
* the node2
* @return the network latency
* @throws JSchException
* the j sch exception
* @throws IOException
* Signals that an I/O exception has occurred.
*/
private float getNetworkLatency(Session session, String node2) throws JSchException, IOException {
Channel channel = getChannel(session, NETWORK_LATENCY_COMMAND + " " + node2);
InputStream in = null;
float latency = 0.0f;
try {
in = channel.getInputStream();
ResultParser resultParser = new ResultParser();
latency = resultParser.parseNetworkLatencyResult(in);
} finally {
if (in != null) {
in.close();
}
channel.disconnect();
}
return latency;
}