Guest User

Untitled

a guest
Dec 9th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. package com.vidushi.takshila.Linux;
  2.  
  3. import java.io.InputStream;
  4.  
  5. import com.jcraft.jsch.Channel;
  6. import com.jcraft.jsch.ChannelExec;
  7. import com.jcraft.jsch.JSch;
  8. import com.jcraft.jsch.Session;
  9.  
  10. public class TaskExecuter implements IConstants{
  11.  
  12.  
  13. public static final String SERVER_HOST_1= "10.10.100.109";
  14.  
  15.  
  16. public static final String USER="root";
  17. public static final String PASSWORD ="root";
  18.  
  19. public static final String KEY = "StrictHostKeyChecking";
  20. public static final String YES = "yes";
  21. public static final String NO = "no";
  22. public static final String PREFERRED_AUTHENTICATION="PreferredAuthentications";
  23. public static final String PREFERRED_AUTHENTICATION_VALUE ="gssapi-with-mic,publickey,keyboard-interactive,password";
  24. public static final String EXEC ="exec";
  25.  
  26.  
  27. public static synchronized void execute(int count,String host,String user,String password){
  28.  
  29. // the task that we want to perform on linux machine
  30. String command = "PATH=/root/jdk1.7.0_71/bin:$PATH; export PATH;java -jar test1.jar \"shortstory.txt\" \"updatedshortstory.txt\"";
  31. log("command executed"+command );
  32.  
  33.  
  34. try{
  35.  
  36. java.util.Properties config = new java.util.Properties();
  37. config.put(KEY, NO);
  38. config.put( PREFERRED_AUTHENTICATION,PREFERRED_AUTHENTICATION_VALUE); //setting up authentication
  39. JSch jsch = new JSch();
  40. Session session=jsch.getSession(user, host, 22);
  41. session.setPassword(password);
  42. session.setConfig(config);
  43. session.connect();
  44. Channel channel=session.openChannel(EXEC);
  45. ((ChannelExec)channel).setCommand(command);// executing command
  46. channel.setInputStream(null);
  47. ((ChannelExec)channel).setErrStream(System.err);
  48. InputStream in=channel.getInputStream();
  49. channel.connect();
  50. byte[] tmp=new byte[1024];
  51. while(true){
  52. while(in.available()>0){
  53. int i=in.read(tmp, 0, 1024);
  54. if(i<0)break;
  55. log(new String(tmp, 0, i));
  56. }
  57.  
  58. if(channel.isClosed()){
  59. break;
  60. }
  61. try{Thread.sleep(1000);}catch(Exception ee){}
  62. }
  63. channel.disconnect();
  64. session.disconnect();
  65. }catch(Exception e){
  66. e.printStackTrace();
  67. }
  68. }
  69.  
  70.  
  71.  
  72. private static void log(String log){
  73. System.out.println(log);
  74. }
  75.  
  76. }
Add Comment
Please, Sign In to add comment