help required (trying to automate sending commands in SSH factory)
Hi,
I need help in automation of ssh. I am trying to send commands automatically so that it will fire commands and give me the output of it. As we all know that, ssh is for getting into the node and fire some commands and give output for that command.
I am using ssh factory jar file and i am trying to send commands automatically.
kindly check for the below code which i have tried:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import com.jscape.inet.ssh.Ssh;
import com.jscape.inet.ssh.SshAdapter;
import com.jscape.inet.ssh.SshConnectedEvent;
import com.jscape.inet.ssh.SshDataReceivedEvent;
import com.jscape.inet.ssh.SshDisconnectedEvent;
import com.jscape.inet.ssh.SshException;
import com.jscape.inet.ssh.SshScript;
import com.jscape.inet.ssh.SshTask;
import com.jscape.inet.ssh.SshTaskEndEvent;
import com.jscape.inet.ssh.SshTaskStartEvent;
import com.jscape.inet.ssh.SshTaskTimeoutException;
import com.jscape.inet.ssh.connection.channels.SessionClient;
import com.jscape.inet.ssh.util.SshParameters;
public class SshScriptTutorial extends SshAdapter {
public SshScriptTutorial() {}
public void executeSshScript(String hostname, String username, String password)
throws SshException, IOException, InterruptedException
{
// assumes that SSH shell prompt is "$" .. this MUST match exactly
String shellPrompt = ">";
// initialize and create new Ssh instance
SshParameters sshParams = new SshParameters(hostname,username,password);
Ssh ssh = new Ssh(sshParams);
// register this class to receive Ssh events
ssh.addSshListener(this);
// create new script object and bind to the given ssh object
SshScript script = new SshScript(ssh);
// add tasks to script object
script.addTask(new SshTask(shellPrompt, "show host", shellPrompt));
script.addTask(new SshTask(shellPrompt, "ssh ssgpun", shellPrompt));
// while sending password, it is not able to fire this.
script.addTask(new SshTask(shellPrompt, "password", ":")); // trying to send password to the server.
// connect to SSH server and execute script
ssh.connect();
// wait until last task is complete
while(!script.isComplete()) {
try {
Thread.sleep(500);
} catch(Exception e) {}
}
// disconnect from server
// ssh.disconnect();
}
public void connected(SshConnectedEvent event) {
System.out.println("Connected to host: " + event.getHost());
}
public void disconnected(SshDisconnectedEvent event) {
System.out.println("Disconnected from host: " + event.getHost());
}
public void dataReceived(SshDataReceivedEvent event) {
System.out.print(event.getData());
}
public static void main(String[] args) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
// System.out.print("Enter SSH hostname: ");
// String hostname = reader.readLine();
// System.out.print("Enter SSH username: ");
// String username = reader.readLine();
String hostname = "hostname"; // ip of server
String username = "username";
System.out.print("Enter SSH password: ");
String password = reader.readLine();
SshScriptTutorial tutorial = new SshScriptTutorial();
tutorial.executeSshScript(hostname, username, password);
// System.out.print("Hi");
String cus_pass = reader.readLine();
} catch(Exception e) {
e.printStackTrace();
}
}
}
Output appears as,
Enter SSH password: 4732885288
Connected to host: 150.236.14.11
egw-pnq > show host
Node IP number Reverse SSH tunnel
_______________________________________________________________________
ssgpun 192.168.181.2 yes
egw-pnq > ssh ssgpun
Connecting to ssgpun as user
Password:
Till this i am able to send commands automatically. when i am trying to send password, it is not able to fire it.
I feel that this might be due to setting shell prompt or delay problem. but i dont know how to solve it.
If any1 knows, kindly help me out in this step.