本文整理汇总了Java中net.schmizz.sshj.connection.channel.direct.Session.Command方法的典型用法代码示例。如果您正苦于以下问题:Java Session.Command方法的具体用法?Java Session.Command怎么用?Java Session.Command使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.schmizz.sshj.connection.channel.direct.Session
的用法示例。
在下文中一共展示了Session.Command方法的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: 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();
}
}
示例3: 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);
}
}
}
示例4: 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();
}
}
示例5: 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();
}
}
示例6: run
import net.schmizz.sshj.connection.channel.direct.Session; //导入方法依赖的package包/类
@Override
public String run() throws RaspiQueryException {
LOGGER.debug("Querying firmware version, vcgencmd path={}", this.vcgencmdPath);
try {
Session session = getSSHClient().startSession();
String cmdString = vcgencmdPath + " version";
final Session.Command cmd = session.exec(cmdString);
cmd.join(30, TimeUnit.SECONDS);
String output = IOUtils.readFully(cmd.getInputStream())
.toString();
final String result = this.parseFirmwareVersion(output);
LOGGER.debug("Firmware version: {}", result);
return result;
} catch (IOException e) {
throw RaspiQueryException.createTransportFailure(e);
}
}
示例7: run
import net.schmizz.sshj.connection.channel.direct.Session; //导入方法依赖的package包/类
@Override
public String run() throws RaspiQueryException {
LOGGER.debug("Querying system time via 'date --rfc-2822'.");
try {
Session session = getSSHClient().startSession();
String cmdString = "date --rfc-2822";
final Session.Command cmd = session.exec(cmdString);
cmd.join(30, TimeUnit.SECONDS);
String output = IOUtils.readFully(cmd.getInputStream())
.toString();
final String result = output.trim();
LOGGER.debug("System time: {}", result);
return result;
} catch (IOException e) {
throw RaspiQueryException.createTransportFailure(e);
}
}
示例8: queryWirelessInterfaceWithProcNetWireless
import net.schmizz.sshj.connection.channel.direct.Session; //导入方法依赖的package包/类
/**
* Queries the link level and signal quality of the wireless interfaces via
* "cat /proc/net/wireless".
*
* @param interfaceName name of the wireless interface
* @throws RaspiQueryException if something goes wrong
*/
private WlanBean queryWirelessInterfaceWithProcNetWireless(String interfaceName)
throws RaspiQueryException {
LOGGER.info("Querying wireless interface {} from /proc/net/wireless ...", interfaceName);
Session session;
try {
session = getSSHClient().startSession();
final String cmdString = "cat /proc/net/wireless";
final Session.Command cmd = session.exec(cmdString);
cmd.join(30, TimeUnit.SECONDS);
String output = IOUtils.readFully(cmd.getInputStream()).toString();
LOGGER.debug("Real output of /proc/net/wireless: \n{}",
output);
return this.parseProcNetWireless(output, interfaceName);
} catch (IOException e) {
throw RaspiQueryException.createTransportFailure(e);
}
}
示例9: queryWirelessInterfaceWithIwconfig
import net.schmizz.sshj.connection.channel.direct.Session; //导入方法依赖的package包/类
private WlanBean queryWirelessInterfaceWithIwconfig(String interfaceName, String iwconfigPath) throws RaspiQueryException {
LOGGER.info("Executing {} to query wireless interface '{}'...", iwconfigPath, interfaceName);
Session session;
try {
session = getSSHClient().startSession();
session.allocateDefaultPTY();
final String cmdString = "LC_ALL=C " + iwconfigPath + " " + interfaceName;
final Session.Command cmd = session.exec(cmdString);
cmd.join(30, TimeUnit.SECONDS);
String output = IOUtils.readFully(cmd.getInputStream())
.toString();
LOGGER.debug("Output of '{}': \n{}", cmdString, output);
return this.parseIwconfigOutput(output);
} catch (IOException e) {
throw RaspiQueryException.createTransportFailure(e);
}
}
示例10: checkCarrier
import net.schmizz.sshj.connection.channel.direct.Session; //导入方法依赖的package包/类
/**
* Checks if the specified interface has a carrier up and running via
* "cat /sys/class/net/[interface]/carrier".
*
* @param interfaceName the interface to check
* @return true, when the interface has a carrier up and running
* @throws RaspiQueryException if something goes wrong
*/
private boolean checkCarrier(String interfaceName)
throws RaspiQueryException {
LOGGER.info("Checking carrier of {}...", interfaceName);
Session session;
try {
session = getSSHClient().startSession();
final String cmdString = "cat /sys/class/net/" + interfaceName + "/carrier";
final Session.Command cmd = session.exec(cmdString);
cmd.join(30, TimeUnit.SECONDS);
final String output = IOUtils.readFully(cmd.getInputStream()).toString();
if (output.contains("1")) {
LOGGER.debug("{} has a carrier up and running.",
interfaceName);
return true;
} else {
LOGGER.debug("{} has no carrier.", interfaceName);
return false;
}
} catch (IOException e) {
throw RaspiQueryException.createTransportFailure(e);
}
}
示例11: queryInterfaceList
import net.schmizz.sshj.connection.channel.direct.Session; //导入方法依赖的package包/类
/**
* Queries which interfaces are available via "/sys/class/net". Loopback
* interfaces are excluded.
*
* @return a List with all interface names (eth0, wlan0,...).
* @throws RaspiQueryException if something goes wrong
*/
private List<String> queryInterfaceList() throws RaspiQueryException {
LOGGER.info("Querying network interfaces...");
Session session;
try {
session = getSSHClient().startSession();
final String cmdString = "ls -1 /sys/class/net";
final Session.Command cmd = session.exec(cmdString);
cmd.join(30, TimeUnit.SECONDS);
final String output = IOUtils.readFully(
cmd.getInputStream()).toString();
final String[] lines = output.split("\n");
final List<String> interfaces = new ArrayList<String>();
for (String interfaceLine : lines) {
if (!interfaceLine.startsWith("lo")) {
LOGGER.debug("Found interface {}.", interfaceLine);
interfaces.add(interfaceLine);
}
}
return interfaces;
} catch (IOException e) {
throw RaspiQueryException.createTransportFailure(e);
}
}
示例12: execCommand
import net.schmizz.sshj.connection.channel.direct.Session; //导入方法依赖的package包/类
ShellResponse execCommand(Session session, String command) throws IOException {
ShellResponse response;
Session.Command cmd = session.exec(command);
String output = IOUtils.readFully(cmd.getInputStream()).toString();
response = new ShellResponse(exitStatus(cmd), output);
return response;
}
示例13: exitStatus
import net.schmizz.sshj.connection.channel.direct.Session; //导入方法依赖的package包/类
private Integer exitStatus(Session.Command cmd) {
if (cmd != null && cmd.getExitStatus() != null) {
return cmd.getExitStatus();
} else {
return 0;
}
}
示例14: runMySSH
import net.schmizz.sshj.connection.channel.direct.Session; //导入方法依赖的package包/类
private void runMySSH() throws IOException {
ProgressHandle handle = ProgressHandleFactory.createHandle("Test SSH Connection");
handle.start(6);
try {
handle.progress("SSH connect", 1);
SSHProvider sshProvider = SSHProvider.getDefault();
SSHClient ssh = sshProvider.connect(context.getServer(), context.getPort());
try {
handle.progress("Start session", 2);
Session session = ssh.startSession();
try {
handle.progress("Send command: ping -c 1 google.com", 3);
final Session.Command cmd = session.exec("ping -c 1 google.com");
System.out.println(IOUtils.readFully(cmd.getInputStream()).toString());
handle.progress("Join on command", 4);
cmd.join(5, TimeUnit.SECONDS);
System.out.println("\n** exit status: " + cmd.getExitStatus());
} finally {
handle.progress("Closing session", 5);
session.close();
}
} finally {
handle.progress("SSH disconnect", 6);
ssh.disconnect();
}
} finally {
handle.finish();
}
NotifyDescriptor nd = new NotifyDescriptor.Message("SSH test connection succeeded!", NotifyDescriptor.INFORMATION_MESSAGE);
DialogDisplayer.getDefault().notify(nd);
}
示例15: logCommandOutput
import net.schmizz.sshj.connection.channel.direct.Session; //导入方法依赖的package包/类
/**
* Stream command output as log message for easy debugging
*/
public static void logCommandOutput(Logger logger, String instanceId, Session.Command command) {
final Marker marker = MarkerFactory.getMarker("ssh-" + instanceId);
new InfoStreamLogger(command.getInputStream(), logger, marker)
.start();
new ErrorStreamLogger(command.getErrorStream(), logger, marker)
.start();
}