本文整理汇总了Java中net.schmizz.sshj.connection.channel.direct.Session.close方法的典型用法代码示例。如果您正苦于以下问题:Java Session.close方法的具体用法?Java Session.close怎么用?Java Session.close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.schmizz.sshj.connection.channel.direct.Session
的用法示例。
在下文中一共展示了Session.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: connectfromPItoServer
import net.schmizz.sshj.connection.channel.direct.Session; //导入方法依赖的package包/类
public static void connectfromPItoServer(int fotosAnzahl, int belichtungsDauer)
throws IOException {
final String ipServer = Inet4Address.getLocalHost().getHostAddress();
// System.out.println(Inet4Address.getAllByName("raspberrypi"));
// System.out.println(fotosAnzahl);
final SSHClient ssh = new SSHClient();
ssh.addHostKeyVerifier(new NullHostKeyVerifier());
// for(int i = 0; i < InetAddress.)
// ssh.connect("192.168.2.100", 22);
ssh.connect(Settings.raspberryIP, 22);
try {
ssh.authPassword("pi", "raspberry");
final Session session = ssh.startSession();
try {
System.out.println("BEGINNE MIt tray");
final Command cmd = session.exec("java -jar Desktop/Driver/Java/client.jar " + fotosAnzahl + " " + ipServer + " " + belichtungsDauer);
System.out.println(IOUtils.readFully(cmd.getInputStream()).toString());
cmd.join(5, TimeUnit.SECONDS);
System.out.println("\n** exit status: " + cmd.getExitStatus());
} finally {
session.close();
}
} finally {
ssh.disconnect();
ssh.close();
}
}
示例2: receiveFile
import net.schmizz.sshj.connection.channel.direct.Session; //导入方法依赖的package包/类
public void receiveFile(String lfile, String rfile) throws Exception {
final SSHClient ssh = getConnectedClient();
try {
final Session session = ssh.startSession();
try {
ssh.newSCPFileTransfer().download(rfile, new FileSystemFile(lfile));
} catch (SCPException e) {
if (e.getMessage().contains("No such file or directory"))
logger.warn("No file or directory `{}` found on {}.", rfile, ip);
else
throw e;
} finally {
session.close();
}
} finally {
ssh.disconnect();
ssh.close();
}
}
示例3: scpUpload
import net.schmizz.sshj.connection.channel.direct.Session; //导入方法依赖的package包/类
/**
* Upload one or more files via the same SSH/SCP connection to a remote host.
*/
public static void scpUpload(HostInfo hostInfo, List<FromTo> fromTos) throws IOException {
SSHClient ssh = getSshClient(hostInfo);
try {
Session session = ssh.startSession();
session.allocateDefaultPTY();
try {
for (FromTo ft: fromTos) {
System.out.format("SCP cp %s -> %s/%s%n", ft.from, hostInfo.host, ft.to);
ssh.newSCPFileTransfer().upload(ft.from, ft.to);
}
} finally {
session.close();
}
} finally {
ssh.disconnect();
ssh.close();
}
}
示例4: scpDownload
import net.schmizz.sshj.connection.channel.direct.Session; //导入方法依赖的package包/类
public static void scpDownload(HostInfo hostInfo, FromTo fromTo) throws IOException {
SSHClient ssh = getSshClient(hostInfo);
try {
Session session = ssh.startSession();
session.allocateDefaultPTY();
try {
ssh.newSCPFileTransfer().download(fromTo.from, fromTo.to);
} finally {
session.close();
}
} finally {
ssh.disconnect();
ssh.close();
}
}
示例5: executeCommand
import net.schmizz.sshj.connection.channel.direct.Session; //导入方法依赖的package包/类
public static int executeCommand(HostInfo hostInfo, String command, boolean debugOutputEnabled) throws IOException {
SSHClient ssh = getSshClient(hostInfo);
try {
Session session = ssh.startSession();
session.allocateDefaultPTY();
try {
if (debugOutputEnabled) {
System.out.println("About to run: " + command);
}
Command cmd = session.exec(command);
readCommandOutput(cmd);
cmd.join();
printExitCode(cmd.getExitStatus());
return cmd.getExitStatus();
} finally {
session.close();
}
} finally {
ssh.disconnect();
ssh.close();
}
}
示例6: executeCommand
import net.schmizz.sshj.connection.channel.direct.Session; //导入方法依赖的package包/类
private Command executeCommand(String command) throws TransportException, ConnectionException {
Session sshSession = null;
Command cmd = null;
try {
sshSession = sshClient.startSession();
cmd = sshSession.exec(command);
cmd.join(SSH_EXEC_TIMEOUT_IN_SEC, TimeUnit.SECONDS);
} catch (ConnectionException e) {
if (e.getCause() instanceof TimeoutException) {
return null;
}
throw e;
} finally {
if (sshSession != null) {
sshSession.close();
}
}
return cmd;
}
示例7: doInBackground
import net.schmizz.sshj.connection.channel.direct.Session; //导入方法依赖的package包/类
@Override
protected Boolean doInBackground(String... cmd) {
String ssh_command = cmd[0];
SSHClient ssh = new SSHClient();
HostKeyVerifier always_verify = new HostKeyVerifier() {
public boolean verify(String arg0, int arg1, PublicKey arg2) {
return true;
}
};
ssh.addHostKeyVerifier(always_verify);
Boolean success = false;
try {
ssh.connect(this.ssh_server);
ssh.authPassword(this.ssh_user, this.ssh_password);
Session session = ssh.startSession();
Command command = session.exec(ssh_command);
command.join();
Integer exit_status = command.getExitStatus();
if (exit_status == this.expected_exit_status) {
success = true;
}
session.close();
ssh.close();
} catch (IOException e) { }
return success;
}
示例8: executeSshCommand
import net.schmizz.sshj.connection.channel.direct.Session; //导入方法依赖的package包/类
private Pair<Integer, String> executeSshCommand(SSHClient ssh, String command) throws IOException {
Session session = null;
Command cmd = null;
try {
session = startSshSession(ssh);
cmd = session.exec(command);
String stdout = IOUtils.readFully(cmd.getInputStream()).toString();
cmd.join(10, TimeUnit.SECONDS);
return Pair.of(cmd.getExitStatus(), stdout);
} finally {
if (cmd != null) {
cmd.close();
}
if (session != null) {
session.close();
}
}
}
示例9: assertSshCommand
import net.schmizz.sshj.connection.channel.direct.Session; //导入方法依赖的package包/类
private void assertSshCommand(Machine machine, AdminAccess adminAccess, String bashCommand) throws IOException {
LOG.info("Checking return code for command '{}' on machine {}", bashCommand, machine.getExternalId());
SSHClient client = Ssh.newClient(machine, adminAccess);
try {
Session session = client.startSession();
try {
session.allocateDefaultPTY();
Session.Command command = session.exec(bashCommand);
command.join();
assertTrue("Exit code was " + command.getExitStatus() + " for command " + bashCommand,
command.getExitStatus() == 0);
} finally {
session.close();
}
} finally {
client.close();
}
}
示例10: testConnectToLocalhostAndCollectOutput
import net.schmizz.sshj.connection.channel.direct.Session; //导入方法依赖的package包/类
@Test
public void testConnectToLocalhostAndCollectOutput() throws IOException {
SSHClient client = Ssh.newClient(localhost, adminAccess, 1000);
try {
Session session = client.startSession();
try {
final Session.Command command = session.exec("echo 'stdout' && echo 'stderr' 1>&2");
String stdout = CharStreams.toString(new InputStreamReader(command.getInputStream()));
String stderr = CharStreams.toString(new InputStreamReader(command.getErrorStream()));
command.join();
assertThat(command.getExitStatus()).isEqualTo(0);
assertThat(command.getExitErrorMessage()).isNull();
assertThat(stdout).contains("stdout");
assertThat(stderr).contains("stderr");
} finally {
session.close();
}
} finally {
client.close();
}
}
示例11: sendFile
import net.schmizz.sshj.connection.channel.direct.Session; //导入方法依赖的package包/类
public void sendFile(String lfile, String rfile) throws Exception {
final SSHClient ssh = getConnectedClient();
try {
final Session session = ssh.startSession();
try {
ssh.newSCPFileTransfer().upload(new FileSystemFile(lfile), rfile);
} finally {
session.close();
}
} finally {
ssh.disconnect();
ssh.close();
}
}
示例12: main
import net.schmizz.sshj.connection.channel.direct.Session; //导入方法依赖的package包/类
public static void main(String... args)
throws IOException {
String privateKey = "-----BEGIN RSA PRIVATE KEY-----\n"
+ "MIIEogIBAAKCAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzI\n"
+ "w+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoP\n"
+ "kcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2\n"
+ "hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NO\n"
+ "Td0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcW\n"
+ "yLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQIBIwKCAQEA4iqWPJXtzZA68mKd\n"
+ "ELs4jJsdyky+ewdZeNds5tjcnHU5zUYE25K+ffJED9qUWICcLZDc81TGWjHyAqD1\n"
+ "Bw7XpgUwFgeUJwUlzQurAv+/ySnxiw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Mdvo6fxIB5i9ZfISgcJ/bbCUkFrhoH\n"
+ "+vq/5CIWxCPp0f85R4qxxQ5ihxJ0YDQT9Jpx4TMss4PSavPaBH3RXow5Ohe+bYoQ\n"
+ "NE5OgEXk2wVfZczCZpigBKbKZHNYcelXtTt/nP3rsCuGcM4h53s=\n"
+ "-----END RSA PRIVATE KEY-----";
String publicKey = "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key";
SSHClient client = new SSHClient();
client.addHostKeyVerifier(new PromiscuousVerifier());
KeyProvider keys = client.loadKeys(privateKey, publicKey, null);
client.connect("192.168.33.10", 22);
client.authPublickey("vagrant", keys);
try {
final Session session = client.startSession();
try {
session.allocateDefaultPTY();
final Shell shell = session.startShell();
new StreamCopier(shell.getInputStream(), System.out)
.bufSize(shell.getLocalMaxPacketSize())
.spawn("stdout");
new StreamCopier(shell.getErrorStream(), System.err)
.bufSize(shell.getLocalMaxPacketSize())
.spawn("stderr");
// Now make System.in act as stdin. To exit, hit Ctrl+D (since that results in an EOF on System.in)
// This is kinda messy because java only allows console input after you hit return
// But this is just an example... a GUI app could implement a proper PTY
new StreamCopier(System.in, shell.getOutputStream())
.bufSize(shell.getRemoteMaxPacketSize())
.copy();
} finally {
session.close();
}
} finally {
client.disconnect();
}
}
示例13: runCommandAndWrapInProcessAdapter
import net.schmizz.sshj.connection.channel.direct.Session; //导入方法依赖的package包/类
private ProcessAdapter runCommandAndWrapInProcessAdapter(List<String> command, SSHClient ssh)
throws IOException {
String cdCommand = "cd " + mRemoteHomePath + ";";
Session session = ssh.startSession();
try {
Command cmd = session.exec(cdCommand + StringUtils.join(command, ' '));
return new ProcessAdapter(ssh, session, cmd);
} catch (IOException e) {
session.close();
throw e;
}
}
示例14: main
import net.schmizz.sshj.connection.channel.direct.Session; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException {
SSHClient ssh = new SSHClient();
ssh.addHostKeyVerifier(
new HostKeyVerifier() {
@Override
public boolean verify(String s, int i, PublicKey publicKey) {
return true;
}
});
ssh.connect("sdf.org");
ssh.authPassword("new", "");
Session session = ssh.startSession();
session.allocateDefaultPTY();
Shell shell = session.startShell();
Expect expect = new ExpectBuilder()
.withOutput(shell.getOutputStream())
.withInputs(shell.getInputStream(), shell.getErrorStream())
.withEchoInput(System.out)
.withEchoOutput(System.err)
.withInputFilters(removeColors(), removeNonPrintable())
.withExceptionOnFailure()
.build();
try {
expect.expect(contains("[RETURN]"));
expect.sendLine();
String ipAddress = expect.expect(regexp("Trying (.*)\\.\\.\\.")).group(1);
System.out.println("Captured IP: " + ipAddress);
expect.expect(contains("login:"));
expect.sendLine("new");
expect.expect(contains("(Y/N)"));
expect.send("N");
expect.expect(regexp(": $"));
expect.send("\b");
expect.expect(regexp("\\(y\\/n\\)"));
expect.sendLine("y");
expect.expect(contains("Would you like to sign the guestbook?"));
expect.send("n");
expect.expect(contains("[RETURN]"));
expect.sendLine();
} finally {
expect.close();
session.close();
ssh.close();
}
}
示例15: closeSession
import net.schmizz.sshj.connection.channel.direct.Session; //导入方法依赖的package包/类
public void closeSession(Session session) {
if (session != null) {
try {
session.close();
} catch (Exception e) {
log.warn("Cannot Close SSH Session");
}
}
}