Search in sources :

Example 1 with CommandExecutor

use of com.orion.remote.channel.ssh.CommandExecutor in project orion-kit by lijiahangmax.

the class CommandExecutorTests method test1.

@Test
public void test1() throws IOException {
    SessionHolder.addIdentity("C:\\Users\\ljh15\\Desktop\\env", "admin123");
    SessionStore store = SessionHolder.getSession("192.168.146.230", "root").connect();
    CommandExecutor executor = store.getCommandExecutor("echo $JAVA_HOME");
    executor.inherit().connect();
    System.out.println(SessionStore.getCommandOutputResultString(executor));
    executor.close();
    store.close();
}
Also used : CommandExecutor(com.orion.remote.channel.ssh.CommandExecutor) Test(org.junit.Test)

Example 2 with CommandExecutor

use of com.orion.remote.channel.ssh.CommandExecutor in project orion-kit by lijiahangmax.

the class CommandExecutorTests method test3.

@Test
public void test3() throws IOException {
    SessionStore store = SessionHolder.getSession("192.168.146.230", "root").setPassword("admin123").connect();
    CommandExecutor executor = store.getCommandExecutor("ec111ho 1");
    executor.inherit().connect();
    System.out.println(SessionStore.getCommandOutputResultString(executor));
    executor.close();
    store.close();
}
Also used : CommandExecutor(com.orion.remote.channel.ssh.CommandExecutor) Test(org.junit.Test)

Example 3 with CommandExecutor

use of com.orion.remote.channel.ssh.CommandExecutor in project orion-kit by lijiahangmax.

the class CommandExecutorTests method test2.

@Test
public void test2() throws IOException {
    SessionStore store = SessionHolder.getSession("192.168.146.230", "root").setPassword("admin123").connect();
    CommandExecutor executor = store.getCommandExecutor("/bin/bash -c 'echo $JAVA_HOME'");
    executor.inherit().connect();
    System.out.println(SessionStore.getCommandOutputResultString(executor));
    executor.close();
    store.close();
}
Also used : CommandExecutor(com.orion.remote.channel.ssh.CommandExecutor) Test(org.junit.Test)

Example 4 with CommandExecutor

use of com.orion.remote.channel.ssh.CommandExecutor in project orion-ops by lijiahangmax.

the class MachineInfoServiceImpl method runRemoteCommand.

/**
 * 执行远程机器命令
 */
private String runRemoteCommand(Long id, String command) {
    SessionStore session = null;
    CommandExecutor executor = null;
    try {
        session = this.openSessionStore(id);
        executor = session.getCommandExecutor(Strings.replaceCRLF(command));
        executor.connect();
        String res = SessionStore.getCommandOutputResultString(executor);
        log.info("执行机器命令-成功 {} {} {}", id, command, res);
        return res;
    } catch (Exception e) {
        log.error("执行机器命令-失败 {} {} {}", id, command, e);
        if (e instanceof IOException) {
            throw Exceptions.ioRuntime(e);
        } else if (e instanceof ConnectionRuntimeException) {
            throw (ConnectionRuntimeException) e;
        } else if (e instanceof AuthenticationException) {
            throw (AuthenticationException) e;
        }
        return null;
    } finally {
        Streams.close(executor);
        Streams.close(session);
    }
}
Also used : SessionStore(com.orion.remote.channel.SessionStore) ConnectionRuntimeException(com.orion.exception.ConnectionRuntimeException) AuthenticationException(com.orion.exception.AuthenticationException) CommandExecutor(com.orion.remote.channel.ssh.CommandExecutor) IOException(java.io.IOException) AuthenticationException(com.orion.exception.AuthenticationException) ConnectionRuntimeException(com.orion.exception.ConnectionRuntimeException) IOException(java.io.IOException)

Example 5 with CommandExecutor

use of com.orion.remote.channel.ssh.CommandExecutor in project orion-kit by lijiahangmax.

the class CommandExecutorTests method ls.

private static void ls() {
    SessionHolder.setLogger(SessionLogger.ERROR);
    CommandExecutor e = SessionHolder.getSession("192.168.146.230", "root").setPassword("admin123").setTimeout(20000).connect(20000).getCommandExecutor("ls -la /a/b/c");
    e.callback(exe -> {
        System.out.println("end....");
        System.out.println(e.getExitCode());
        e.close();
    });
    e.streamHandler(ReaderLineConsumer.getDefaultPrint());
    e.errorStreamHandler(ReaderLineConsumer.getDefaultPrint());
    e.connect().exec();
}
Also used : CommandExecutor(com.orion.remote.channel.ssh.CommandExecutor)

Aggregations

CommandExecutor (com.orion.remote.channel.ssh.CommandExecutor)6 Test (org.junit.Test)3 AuthenticationException (com.orion.exception.AuthenticationException)1 ConnectionRuntimeException (com.orion.exception.ConnectionRuntimeException)1 SessionStore (com.orion.remote.channel.SessionStore)1 IOException (java.io.IOException)1