本文整理汇总了Java中net.schmizz.sshj.connection.channel.direct.Session.exec方法的典型用法代码示例。如果您正苦于以下问题:Java Session.exec方法的具体用法?Java Session.exec怎么用?Java Session.exec使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.schmizz.sshj.connection.channel.direct.Session
的用法示例。
在下文中一共展示了Session.exec方法的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: 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();
}
}
示例3: 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;
}
示例4: 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);
}
}
}
示例5: 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;
}
示例6: 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();
}
}
}
示例7: 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();
}
}
示例8: 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();
}
}
示例9: 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);
}
}
示例10: queryDiskUsage
import net.schmizz.sshj.connection.channel.direct.Session; //导入方法依赖的package包/类
@Override
public final List<DiskUsageBean> queryDiskUsage()
throws RaspiQueryException {
LOGGER.info("Querying disk usage...");
if (client != null) {
if (client.isConnected() && client.isAuthenticated()) {
Session session;
try {
session = client.startSession();
final Command cmd = session.exec(DISK_USAGE_CMD);
cmd.join(30, TimeUnit.SECONDS);
return this.parseDiskUsage(IOUtils
.readFully(cmd.getInputStream()).toString().trim());
} catch (IOException e) {
throw RaspiQueryException.createTransportFailure(hostname,
e);
}
} else {
throw new IllegalStateException("You must establish a connection first.");
}
} else {
throw new IllegalStateException("You must establish a connection first.");
}
}
示例11: 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);
}
}
示例12: 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);
}
}
示例13: 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);
}
}
示例14: 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);
}
}
示例15: 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);
}
}