public static void SSH(String Host,String username,String passwor,String cmd,String cmd1) {
byte[] tmp = new byte[1024];
String host =Host;
String user =username;
String password =passwor;
String command = cmd;
String ou=null;
try {
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
// Create a JSch session to connect to the server
Session session = jsch.getSession(user, host, 22);
session.setPassword(password);
session.setConfig(config);
// Establish the connection
session.connect();
System.out.println("Connected...");
ChannelExec channel = (ChannelExec) session.openChannel("exec");
channel.setCommand(command);
channel.setErrStream(System.err);
channel.connect();
channel.setCommand(cmd1);
channel.setErrStream(System.err);
channel.connect();
InputStream in = channel.getInputStream();
channel.connect();
while (true) {
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0) {
break;
}
System.out.print(new String(tmp, 0, i));
ou=new String(tmp);
}
if (channel.isClosed()) {
System.out.println("Exit Status: "
+ channel.getExitStatus());
break;
}
Thread.sleep(1000);
}
channel.disconnect();
session.disconnect();
System.out.println("DONE!!!");
} catch (Exception e) {
e.printStackTrace();
}
}
Comments
Post a Comment