I want to:
- Log in to putty using Hostname, username, password and port number. This I have achieved.
- Once I logged in, I want to connect to server1. Usually in putty we connect using ssh command (ssh user@server1).
- Once I connected to that server.I need to run multiple commands like:
df -kh ps -ef|grep www
- And after executing above commands, I need to log out from server1 and need to log in to server2.
How can I do it in JSCH?
JSch jsch=new JSch();
Session session=jsch.getSession(remoteHostUserName, RemoteHostName, remoteHostPortNo);
session.setPassword(remoteHostpassword);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
System.out.println("Please wait...");
session.connect();
System.out.println("Connected "+remoteHostUserName+"@"+RemoteHostName);
ChannelExec channel=(ChannelExec) session.openChannel("shell");
BufferedReader in=new BufferedReader(new InputStreamReader(channel.getInputStream()));
channel.setCommand("df -kh");
channel.setCommand("pwd");
channel.connect();