本文整理汇总了Java中net.schmizz.sshj.connection.channel.direct.Session类的典型用法代码示例。如果您正苦于以下问题:Java Session类的具体用法?Java Session怎么用?Java Session使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Session类属于net.schmizz.sshj.connection.channel.direct包,在下文中一共展示了Session类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: executeCommand
import net.schmizz.sshj.connection.channel.direct.Session; //导入依赖的package包/类
@Override
public String executeCommand(ShellCommand aCommand) throws IOException {
SSHClient ssh = new SSHClient();
ssh.loadKnownHosts();
ssh.connect("localhost", 2222);
try {
ssh.authPublickey("root");
try (Session session = ssh.startSession()) {
Session.Command command = session.exec(aCommand.getCommand());
String text = IOUtils.readFully(command.getInputStream()).toString();
command.join(5, TimeUnit.SECONDS);
return text;
}
} finally {
ssh.disconnect();
}
}
示例2: 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();
}
}
示例3: connect_to_virtual_box
import net.schmizz.sshj.connection.channel.direct.Session; //导入依赖的package包/类
@Test
public void connect_to_virtual_box() throws IOException {
SSHClient ssh = new SSHClient();
ssh.loadKnownHosts();
ssh.connect("localhost", 2222);
try {
ssh.authPublickey("root");
try (Session session = ssh.startSession()) {
Session.Command command = session.exec("ls -l");
System.out.println(IOUtils.readFully(command.getInputStream()).toString());
command.join(5, TimeUnit.SECONDS);
}
} finally {
ssh.disconnect();
}
}
示例4: 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();
}
}
示例5: 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();
}
}
示例6: 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();
}
}
示例7: 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();
}
}
示例8: testStartShell
import net.schmizz.sshj.connection.channel.direct.Session; //导入依赖的package包/类
@Test
public void testStartShell() throws IOException {
SshConfig config = new SshConfig("localhost", "me");
Session session = mock(Session.class);
when(client.startSession()).thenReturn(session);
Shell shell = mock(Shell.class);
when(session.startShell()).thenReturn(shell);
OutputStream shellOutput = new ByteArrayOutputStream();
when(shell.getOutputStream()).thenReturn(shellOutput);
InputStream shellInput = new ByteArrayInputStream(new byte[1000]);
when(shell.getInputStream()).thenReturn(shellInput);
InputStream shellError = new ByteArrayInputStream(new byte[1000]);
when(shell.getErrorStream()).thenReturn(shellError);
@SuppressWarnings("resource")
InteractiveSession shellSession = new SshConnection(config, client).startShell();
verify(session).allocateDefaultPTY();
assertThat(shellSession.getInputStream(), is(shellInput));
assertThat(shellSession.getOutputStream(), is(shellOutput));
assertThat(shellSession.getErrorStream(), is(shellError));
}
示例9: testTryExecute
import net.schmizz.sshj.connection.channel.direct.Session; //导入依赖的package包/类
@Test
public void testTryExecute() throws IOException {
SshConfig config = new SshConfig("localhost", "me");
Session session = mock(Session.class);
when(client.startSession()).thenReturn(session);
Command command = mock(Command.class);
when(session.exec("\"test command\" \"param\"")).thenReturn(command);
when(command.getExitStatus()).thenReturn(1);
when(command.getInputStream()).thenReturn(IOUtils.toInputStream("output\n"));
when(command.getErrorStream()).thenReturn(IOUtils.toInputStream("error line\n"));
@SuppressWarnings("resource")
ExecutionResult result = new SshConnection(config, client).tryExecute(
"test command", "param");
// TODO test escaping
verify(command).join();
assertThat(result.getOutput(), is("output\n"));
assertThat(result.getErrorOutput(), is("error line\n"));
assertThat(result.getExitCode(), is(1));
}
示例10: testTryExecuteWithDirectory
import net.schmizz.sshj.connection.channel.direct.Session; //导入依赖的package包/类
@SuppressWarnings("resource")
@Test
public void testTryExecuteWithDirectory() throws IOException {
Session session = mock(Session.class);
when(client.startSession()).thenReturn(session);
Command command = mock(Command.class);
when(session.exec("\"cd\" \"/test/dir\" && \"test command\" \"param\""))
.thenReturn(command);
when(command.getExitStatus()).thenReturn(1);
when(command.getInputStream()).thenReturn(IOUtils.toInputStream("output\n"));
when(command.getErrorStream()).thenReturn(IOUtils.toInputStream("error line\n"));
SshConfig config = new SshConfig("localhost", "me");
new SshConnection(config, client)
.tryExecute(Paths.get("/test/dir"), "test command", "param");
}
示例11: testTryExecuteIn
import net.schmizz.sshj.connection.channel.direct.Session; //导入依赖的package包/类
@SuppressWarnings("resource")
@Test
public void testTryExecuteIn() throws IOException {
Session session = mock(Session.class);
when(client.startSession()).thenReturn(session);
Command command = mock(Command.class);
when(session.exec("\"cd\" \"/test/dir\" && \"test command\" \"param\""))
.thenReturn(command);
when(command.getExitStatus()).thenReturn(1);
when(command.getInputStream()).thenReturn(IOUtils.toInputStream("output\n"));
when(command.getErrorStream()).thenReturn(IOUtils.toInputStream("error line\n"));
SshConfig config = new SshConfig("localhost", "me");
new SshConnection(config, client)
.tryExecuteIn("/test/dir", "test command", "param");
}
示例12: testTryExecuteThrowsWhenExitCodeNotRetrieved
import net.schmizz.sshj.connection.channel.direct.Session; //导入依赖的package包/类
@SuppressWarnings("resource")
@Test(expected = IOException.class)
public void testTryExecuteThrowsWhenExitCodeNotRetrieved() throws IOException {
Session session = mock(Session.class);
when(client.startSession()).thenReturn(session);
Command command = mock(Command.class);
when(session.exec("\"test command\" \"param\"")).thenReturn(command);
when(command.getInputStream()).thenReturn(IOUtils.toInputStream("output\n"));
when(command.getErrorStream()).thenReturn(IOUtils.toInputStream("error line\n"));
SshConfig config = new SshConfig("localhost", "me");
// This is the critical line
when(command.getExitStatus()).thenReturn(null);
new SshConnection(config, client).tryExecute("test command", "param");
}
示例13: execute
import net.schmizz.sshj.connection.channel.direct.Session; //导入依赖的package包/类
@Override
public OperationFuture<ShellResponse> execute() throws SshException {
ShellResponse response = null;
Session session = null;
try {
ssh.addHostKeyVerifier(new PromiscuousVerifier());
withTimeout(ofMinutes(CONNECTION_TIMEOUT), () -> ssh.connect(host));
ssh.authPassword(credentials.getUserName(), credentials.getPassword());
for (String command : commandList) {
session = ssh.startSession();
response = execCommand(session, command);
}
} catch (IOException e) {
throw new SshException(e);
} finally {
closeQuietly(ssh, session);
}
return new OperationFuture<>(response, new NoWaitingJobFuture());
}
示例14: 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;
}
示例15: prepareUpload
import net.schmizz.sshj.connection.channel.direct.Session; //导入依赖的package包/类
private void prepareUpload(Session session, String command) {
try (Session.Command sshCommand = session.exec(command)) {
sshCommand.join();
if (sshCommand.getExitStatus() != 0) {
throw new ArtifactExecutionException("Command " + command + " failed with exit status " + sshCommand.getExitStatus());
} else {
log.info("Prepare upload finished normally with standard output [{}] and error output [{}]",
command,
new String(IOUtils.readFully(sshCommand.getInputStream()).toByteArray()),
new String(IOUtils.readFully(sshCommand.getErrorStream()).toByteArray()));
}
} catch (IOException e) {
if (ExceptionUtils.indexOfType(e, InterruptedException.class) >= 0) {
throw new ArtifactInterruptedException("Execution has been interrupted", e);
} else {
throw new ArtifactExecutionException("Command " + command + " has failed", e);
}
}
}