Class Node
This is a pure POJO class that provides SSH-based command execution capabilities. It has NO dependency on ActorSystem or workflow components.
Three Levels of Usage
Level 1: Pure POJO (Synchronous)
Node node = new Node("192.168.1.1", "admin");
CommandResult result = node.executeCommand("show version");
System.out.println(result.getStdout());
Level 2: Actor-based (Asynchronous, Parallel)
ActorSystem system = new ActorSystem("iac", 4);
ActorRef<Node> nodeActor = system.actorOf("node1", node);
CompletableFuture<CommandResult> future = nodeActor.ask(n -> n.executeCommand("show version"));
Level 3: Workflow-based (YAML/JSON/XML)
// Use NodeInterpreter instead for workflow capabilities
NodeInterpreter nodeInterpreter = new NodeInterpreter(node, system);
IIActorRef<NodeInterpreter> nodeActor = new IIActorRef<>("node1", nodeInterpreter, system);
Uses ssh-agent for SSH key authentication. Make sure ssh-agent is running and your SSH key is added before using this class.
- Author:
- devteam@scivics-lab.com
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classRepresents the result of a command execution. -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a Node with default port 22 (POJO constructor).Constructs a Node with the specified connection parameters (POJO constructor).Constructs a Node with the specified connection parameters and local mode.Constructs a Node with all connection parameters including password. -
Method Summary
Modifier and TypeMethodDescriptionvoidcleanup()Cleans up resources used by this Node.private com.jcraft.jsch.SessionCreates a JSch SSH session with configured credentials.private com.jcraft.jsch.SessioncreateSessionViaProxyJump(com.jcraft.jsch.JSch jsch, String proxyJump, String targetUser, String targetHost, int targetPort) Creates a session through a jump host using ProxyJump.executeCommand(String command) Executes a command on the node.private Node.CommandResultexecuteLocalCommand(String command) Executes a command locally using ProcessBuilder with real-time streaming.private Node.CommandResultexecuteRemoteCommand(String command) Executes a command on the remote node via SSH using JSch with real-time streaming.executeSudoCommand(String command) Executes a command with sudo privileges on the remote node.Gets the hostname of this node.intgetPort()Gets the SSH port for this node.getUser()Gets the SSH username for this node.booleanChecks if this node is in local execution mode.private voidsetupAuthentication(com.jcraft.jsch.JSch jsch, String identityFileFromConfig) Sets up authentication for JSch.toString()
-
Field Details
-
hostname
-
user
-
port
-
localMode
-
password
-
jumpHostSession
-
SUDO_PASSWORD_ENV
- See Also:
-
-
Constructor Details
-
Node
-
Node
Constructs a Node with the specified connection parameters and local mode.- Parameters:
hostname- the hostname or IP address of the nodeuser- the SSH usernameport- the SSH port (typically 22)localMode- if true, execute commands locally instead of via SSH
-
Node
Constructs a Node with all connection parameters including password.- Parameters:
hostname- the hostname or IP address of the nodeuser- the SSH usernameport- the SSH port (typically 22)localMode- if true, execute commands locally instead of via SSHpassword- the SSH password (null to use ssh-agent key authentication)
-
Node
-
-
Method Details
-
isLocalMode
Checks if this node is in local execution mode.- Returns:
- true if commands are executed locally, false for SSH
-
executeCommand
Executes a command on the node.If localMode is true, executes the command locally using ProcessBuilder. Otherwise, executes via SSH using JSch.
- Parameters:
command- the command to execute- Returns:
- the execution result containing stdout, stderr, and exit code
- Throws:
IOException- if command execution fails
-
executeLocalCommand
Executes a command locally using ProcessBuilder with real-time streaming.Output is streamed to System.out/System.err in real-time as it becomes available, while also being captured for the CommandResult.
- Parameters:
command- the command to execute- Returns:
- the execution result
- Throws:
IOException- if command execution fails
-
executeRemoteCommand
Executes a command on the remote node via SSH using JSch with real-time streaming.Output is streamed to System.out/System.err in real-time as it becomes available, while also being captured for the CommandResult.
- Parameters:
command- the command to execute- Returns:
- the execution result containing stdout, stderr, and exit code
- Throws:
IOException- if SSH connection or command execution fails
-
executeSudoCommand
Executes a command with sudo privileges on the remote node.Reads the sudo password from the SUDO_PASSWORD environment variable. If the environment variable is not set, throws an IOException.
Multi-line scripts are properly handled by wrapping them in bash -c.
- Parameters:
command- the command to execute with sudo- Returns:
- the execution result containing stdout, stderr, and exit code
- Throws:
IOException- if SSH connection fails or SUDO_PASSWORD is not set
-
createSession
Creates a JSch SSH session with configured credentials. Supports ProxyJump for connections through a jump host.- Returns:
- configured but not yet connected Session
- Throws:
com.jcraft.jsch.JSchException- if session creation failsIOException- if SSH key file operations fail
-
setupAuthentication
private void setupAuthentication(com.jcraft.jsch.JSch jsch, String identityFileFromConfig) throws IOException, com.jcraft.jsch.JSchException Sets up authentication for JSch.- Throws:
IOExceptioncom.jcraft.jsch.JSchException
-
createSessionViaProxyJump
private com.jcraft.jsch.Session createSessionViaProxyJump(com.jcraft.jsch.JSch jsch, String proxyJump, String targetUser, String targetHost, int targetPort) throws com.jcraft.jsch.JSchException, IOException Creates a session through a jump host using ProxyJump. Format: user@host or user@host:port- Throws:
com.jcraft.jsch.JSchExceptionIOException
-
cleanup
Cleans up resources used by this Node. Closes any open jump host sessions. -
getHostname
-
getUser
-
getPort
-
toString
-