Advertisement
Guest User

Untitled

a guest
Jan 13th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. public void processLogs(List<String> logs) {
  2. try {
  3. Connection conn = DMConnector.getDMConnection();
  4. PreparedStatement statement = conn.prepareStatement("INSERT INTO Logs (to_address, status, smtp_transac, log_timestamp) VALUES (?,?,?,?)");
  5.  
  6. System.out.println(logs.size());
  7. String to_address;
  8. String status;
  9. String smtp_transac;
  10. String timestamp;
  11.  
  12. for (String log : logs) {
  13. to_address = log.split(" <")[1].split("> ")[0];
  14. status = log.split(" ")[2];
  15. smtp_transac = log.split(">:")[1];
  16. timestamp = log.split(" ")[0] + log.split(" ")[1];
  17.  
  18. statement.setString(1, to_address);
  19. statement.setString(2, status);
  20. statement.setString(3, smtp_transac);
  21. statement.setString(4, timestamp);
  22. statement.executeUpdate();
  23.  
  24. System.out.println("toAdd = " + to_address + " status= " + status + " smtp_transac = " + smtp_transac + "n");
  25. }
  26.  
  27. statement.close();
  28. conn.close();
  29. } catch (Exception ex) {
  30. System.out.println(ex.getMessage());
  31. }
  32. }
  33.  
  34. public List<String> getLogs() {
  35.  
  36.  
  37. String remoteHostUserName = "";
  38. String remoteHostPassword = "";
  39. String hostIP = "";
  40. List<String> results = new ArrayList<String>();
  41.  
  42. JSch jsch = new JSch();
  43. try {
  44.  
  45. Session session = jsch.getSession(remoteHostUserName, hostIP, 22);
  46.  
  47. Properties config = new Properties();
  48. config.put("StrictHostKeyChecking", "no");
  49. session.setConfig(config);
  50.  
  51. // Skip prompting for the password info and go direct...
  52. session.setPassword(remoteHostPassword);
  53. session.connect();
  54.  
  55. String command = "hvmail_report -q all --last '1 min' -a --show-address --show-mtaid --show-time | sort";
  56.  
  57. Channel channel = session.openChannel("exec");
  58. ((ChannelExec) channel).setCommand(command);
  59.  
  60. ((ChannelExec) channel).setErrStream(System.err);
  61.  
  62. InputStream in = channel.getInputStream();
  63.  
  64. System.out.println("Connect to session...");
  65. channel.connect();
  66.  
  67. byte[] tmp = new byte[1024];
  68. while (true) {
  69. while (in.available() > 0) {
  70. int i = in.read(tmp, 0, 1024);
  71. if (i < 0) {
  72. break;
  73. }
  74. //System.out.println(new String(tmp, 0, i));
  75.  
  76. Collections.addAll(results, new String(tmp, 0, i).split("rn|r|n"));
  77. }
  78. if (channel.isClosed()) {
  79. System.out.println("exit-status: " + channel.getExitStatus());
  80. break;
  81. }
  82. try {
  83. Thread.sleep(1000);
  84. } catch (Exception ee) {
  85. ee.printStackTrace();
  86. }
  87. }
  88. channel.disconnect();
  89. session.disconnect();
  90.  
  91. if (in.markSupported()){
  92. in.reset();
  93. }
  94.  
  95.  
  96. } catch (Exception e) {
  97. System.out.println(e);
  98. }
  99. return results;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement