本文整理汇总了Java中com.jcraft.jsch.Session.disconnect方法的典型用法代码示例。如果您正苦于以下问题:Java Session.disconnect方法的具体用法?Java Session.disconnect怎么用?Java Session.disconnect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.jcraft.jsch.Session
的用法示例。
在下文中一共展示了Session.disconnect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: fileFetch
import com.jcraft.jsch.Session; //导入方法依赖的package包/类
public static void fileFetch(String host, String user, String keyLocation, String sourceDir, String destDir) {
JSch jsch = new JSch();
Session session = null;
try {
// set up session
session = jsch.getSession(user,host);
// use private key instead of username/password
session.setConfig(
"PreferredAuthentications",
"publickey,gssapi-with-mic,keyboard-interactive,password");
jsch.addIdentity(keyLocation);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
// copy remote log file to localhost.
ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
channelSftp.get(sourceDir, destDir);
channelSftp.exit();
} catch (Exception e) {
e.printStackTrace();
} finally {
session.disconnect();
}
}
示例2: testTestCommand
import com.jcraft.jsch.Session; //导入方法依赖的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
示例3: connectAndExecute
import com.jcraft.jsch.Session; //导入方法依赖的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;
}
示例4: testJschConnection
import com.jcraft.jsch.Session; //导入方法依赖的package包/类
@Test
public void testJschConnection() throws InterruptedException, SftpException, JSchException, IOException {
JSch jsch = new JSch();
String passphrase = "";
jsch.addIdentity(privateKey,
StringUtil.isEmpty(passphrase) ?
null : passphrase);
Session session = jsch.getSession(user, host, port);
System.out.println("session created.");
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
config.put("PreferredAuthentications",
"publickey,keyboard-interactive,password");
session.setConfig(config);
session.connect();
Thread.sleep(500);
session.disconnect();
}
示例5: close
import com.jcraft.jsch.Session; //导入方法依赖的package包/类
public synchronized void close() throws IOException {
if (closed) {
return;
}
super.close();
closed = true;
if (!channel.isConnected()) {
throw new IOException(E_CLIENT_NOTCONNECTED);
}
try {
Session session = channel.getSession();
channel.disconnect();
session.disconnect();
} catch (JSchException e) {
throw new IOException(StringUtils.stringifyException(e));
}
}
示例6: validateCredentials
import com.jcraft.jsch.Session; //导入方法依赖的package包/类
public Message validateCredentials(String host, String user, String password) {
JSch jsch = new JSch();
Session session;
try {
session = jsch.getSession(user, host, SSH_PORT);
session.setPassword(password);
java.util.Properties config = new java.util.Properties();
config.put(STRICT_HOST_KEY_CHECKING, STRICT_HOST_KEY_CHECKING_DEFAULT_VALUE);
session.setConfig(config);
session.setConfig(PREFERRED_AUTHENTICATIONS, PREFERRED_AUTHENTICATIONS_DEFAULT_VALUES);
session.connect();
session.disconnect();
} catch (Exception e) {
return getErrorMessage(e);
}
return new Message(MessageType.SUCCESS, GENERAL_SUCCESS_MESSAGE);
}
示例7: scpFileFromRemoteServer
import com.jcraft.jsch.Session; //导入方法依赖的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();
}
示例8: deleteFile
import com.jcraft.jsch.Session; //导入方法依赖的package包/类
/**
* @param host
* @param user
* @param pwd
* @param remoteFile
*/
public void deleteFile(String host, String user, String pwd,
String remoteFile) {
try {
JSch ssh = new JSch();
Session session = ssh.getSession(user, host, 22);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setPassword(pwd);
session.connect();
Channel channel = session.openChannel("exec");
channel.connect();
String command = "rm -rf " + remoteFile;
System.out.println("command: " + command);
// ((ChannelExec) channel).setCommand(command);
channel.disconnect();
session.disconnect();
} catch (JSchException e) {
System.out.println(e.getMessage().toString());
e.printStackTrace();
}
}
示例9: executeSSH
import com.jcraft.jsch.Session; //导入方法依赖的package包/类
private boolean executeSSH(){
//get deployment descriptor, instead of this hard coded.
// or execute a script on the target machine which download artifact from nexus
String command ="nohup java -jar -Dserver.port=8091 ./work/codebox/chapter6/chapter6.search/target/search-1.0.jar &";
try{
System.out.println("Executing "+ command);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
Session session=jsch.getSession("rajeshrv", "localhost", 22);
session.setPassword("rajeshrv");
session.setConfig(config);
session.connect();
System.out.println("Connected");
ChannelExec channelExec = (ChannelExec)session.openChannel("exec");
InputStream in = channelExec.getInputStream();
channelExec.setCommand(command);
channelExec.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
int index = 0;
while ((line = reader.readLine()) != null) {
System.out.println(++index + " : " + line);
}
channelExec.disconnect();
session.disconnect();
System.out.println("Done!");
}catch(Exception e){
e.printStackTrace();
return false;
}
return true;
}
示例10: sshCall
import com.jcraft.jsch.Session; //导入方法依赖的package包/类
protected void sshCall(String username, String password, SshExecutor executor, String channelType) {
try {
JSch jsch = new JSch();
Session session = jsch.getSession(username, props.getShell().getHost(), props.getShell().getPort());
session.setPassword(password);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel = session.openChannel(channelType);
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream();
channel.setInputStream(new PipedInputStream(pos));
channel.setOutputStream(new PipedOutputStream(pis));
channel.connect();
try {
executor.execute(pis, pos);
} finally {
pis.close();
pos.close();
channel.disconnect();
session.disconnect();
}
} catch(JSchException | IOException ex) {
fail(ex.toString());
}
}
示例11: destroySession
import com.jcraft.jsch.Session; //导入方法依赖的package包/类
/**
* Destroy ssh Session once completed
*
* @param session
* @throws Exception
*/
public static void destroySession( Session session )
{
try
{
if ( session != null )
{
logger.debug( "HddInfoHelper: Destroying the ssh Session begin..." );
session.disconnect();
logger.debug( "HddInfoHelper: Destroyed the ssh Session..." );
}
session = null;
}
catch ( Exception e )
{
logger.error( "Unable to destroy SSH Session: {} ", session, e );
}
}
示例12: exec
import com.jcraft.jsch.Session; //导入方法依赖的package包/类
@SneakyThrows
public static int exec(@Nonnull String host,
int port,
@Nonnull String username,
@Nonnull String password,
@Nonnull String cmd) {
JSch jSch = new JSch();
Session session = jSch.getSession(username, host, port);
session.setPassword(password);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(cmd);
((ChannelExec) channel).setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
InputStream inputStream = channel.getInputStream();
channel.connect();
log.info("# successful connect to server [{}:{}]", host, port);
log.info("# exec cmd [{}]", cmd);
StringBuilder sb = new StringBuilder();
byte[] bytes = new byte[1024];
int exitStatus;
while (true) {
while (inputStream.available() > 0) {
int i = inputStream.read(bytes, 0, 1024);
if (i < 0) {
break;
}
sb.append(new String(bytes, 0, i, StandardCharsets.UTF_8));
}
if (channel.isClosed()) {
if (inputStream.available() > 0) {
continue;
}
exitStatus = channel.getExitStatus();
break;
}
Thread.sleep(1000);
}
if (StringUtils.isNotEmpty(sb)) {
log.info("# cmd-rs \n" + sb);
}
channel.disconnect();
session.disconnect();
log.info("# successful disconnect to server [{}:{}]", host, port);
return exitStatus;
}
示例13: close
import com.jcraft.jsch.Session; //导入方法依赖的package包/类
@Override
public void close() {
if (!sshSessions.isEmpty()) {
log.debug("closing SSH sessions");
}
while (!sshSessions.isEmpty()) {
Session session = sshSessions.pop();
deletePortForwarding(session);
try {
session.disconnect();
} catch (Exception e) {
log.error("Failed to disconnect SSH session", e);
}
}
Assert.isTrue(portForwardings.isEmpty(), "port forwardings must be empty at this point");
}
示例14: disconnect
import com.jcraft.jsch.Session; //导入方法依赖的package包/类
void disconnect(ChannelSftp channel) throws IOException {
if (channel != null) {
// close connection if too many active connections
boolean closeConnection = false;
synchronized (this) {
if (liveConnectionCount > maxConnection) {
--liveConnectionCount;
con2infoMap.remove(channel);
closeConnection = true;
}
}
if (closeConnection) {
if (channel.isConnected()) {
try {
Session session = channel.getSession();
channel.disconnect();
session.disconnect();
} catch (JSchException e) {
throw new IOException(StringUtils.stringifyException(e));
}
}
} else {
returnToPool(channel);
}
}
}
示例15: destroyObject
import com.jcraft.jsch.Session; //导入方法依赖的package包/类
@Override
public void destroyObject(ConnectionDetails connectionDetails, PooledObject<Session> sessionObject) {
log.debug("Destroying session for "+connectionDetails);
if (sessionObject != null) {
Session session = sessionObject.getObject();
if (session != null) {
session.disconnect();
}
}
}