Executing a script
Top  Previous  Next

A SshScript will begin execution as soon as a connection is established with the SSH server. Therefore it is recommended that your SshScript be created and all tasks added before the Ssh#connect method is invoked.

As an example the code example below will demonstrate how to automatically login to a SSH server, execute a command and logout. Some assumptions have been made about your shell prompt. Your shell prompt may differ although the fundamental process would be the same.

Example


// create ssh connection
SshParameters params = new SshParameters("10.0.0.2"
,"jsmith","secret");
Ssh ssh = new Ssh(params);
ssh.addSshListener(this);

// create script and add task

SshScript script = new SshScript(ssh);
SshTask task = new SshTask("$"
,"ls -al","$");
script.addTask(task);

// connect and execute script

ssh.connect();

// wait until script is complete

while(!script.isComplete()) {
  Thread.sleep(1000
);
}

// disconnect

ssh.disconnect();