Guest User

Untitled

a guest
Sep 9th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.22 KB | None | 0 0
  1. Java Socket read from socket in real time
  2. ssh_input = new javax.swing.JPanel();
  3. connect = new javax.swing.JButton();
  4. jLabel1 = new javax.swing.JLabel();
  5. jLabel2 = new javax.swing.JLabel();
  6. jLabel3 = new javax.swing.JLabel();
  7. hostname = new javax.swing.JTextField();
  8. username = new javax.swing.JTextField();
  9. password = new javax.swing.JPasswordField();
  10. ssh_command = new javax.swing.JTextField();
  11. ssh_output_pannel = new javax.swing.JPanel();
  12. jScrollPane1 = new javax.swing.JScrollPane();
  13. ssh_output = new javax.swing.JTextArea();
  14. clear_text = new javax.swing.JButton();
  15.  
  16. connect.setText("Connect");
  17. connect.addActionListener(new java.awt.event.ActionListener() {
  18. public void actionPerformed(java.awt.event.ActionEvent evt) {
  19. connectActionPerformed(evt);
  20. }
  21. });
  22.  
  23. jLabel1.setText("Host Name");
  24.  
  25. jLabel2.setText("User Name");
  26.  
  27. jLabel3.setText("Password");
  28.  
  29. hostname.setText("10.4.0.135");
  30.  
  31. username.setText("xzhang");
  32.  
  33. password.setText("Samboapple0827!");
  34.  
  35. ssh_command.addActionListener(new java.awt.event.ActionListener() {
  36. public void actionPerformed(java.awt.event.ActionEvent evt) {
  37. ssh_commandActionPerformed(evt);
  38. }
  39. });
  40.  
  41. javax.swing.GroupLayout ssh_inputLayout = new javax.swing.GroupLayout(ssh_input);
  42. ssh_input.setLayout(ssh_inputLayout);
  43. ssh_inputLayout.setHorizontalGroup(
  44. ssh_inputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  45. .addGroup(ssh_inputLayout.createSequentialGroup()
  46. .addContainerGap()
  47. .addGroup(ssh_inputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  48. .addComponent(jLabel1)
  49. .addGroup(ssh_inputLayout.createSequentialGroup()
  50. .addGroup(ssh_inputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  51. .addComponent(jLabel2)
  52. .addComponent(jLabel3))
  53. .addGap(18, 18, 18)
  54. .addGroup(ssh_inputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  55. .addComponent(username, javax.swing.GroupLayout.Alignment.TRAILING)
  56. .addComponent(password)
  57. .addComponent(hostname, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE))))
  58. .addGroup(ssh_inputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  59. .addGroup(ssh_inputLayout.createSequentialGroup()
  60. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  61. .addComponent(connect))
  62. .addGroup(ssh_inputLayout.createSequentialGroup()
  63. .addGap(49, 49, 49)
  64. .addComponent(ssh_command, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)))
  65. .addContainerGap(60, Short.MAX_VALUE))
  66. );
  67. ssh_inputLayout.setVerticalGroup(
  68. ssh_inputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  69. .addGroup(ssh_inputLayout.createSequentialGroup()
  70. .addGap(14, 14, 14)
  71. .addGroup(ssh_inputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  72. .addComponent(jLabel1)
  73. .addComponent(hostname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  74. .addComponent(connect))
  75. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  76. .addGroup(ssh_inputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  77. .addComponent(jLabel2)
  78. .addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  79. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  80. .addGroup(ssh_inputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  81. .addComponent(jLabel3)
  82. .addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  83. .addComponent(ssh_command, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  84. .addContainerGap(18, Short.MAX_VALUE))
  85. );
  86.  
  87. ssh_output.setColumns(20);
  88. ssh_output.setEditable(false);
  89. ssh_output.setLineWrap(true);
  90. ssh_output.setRows(5);
  91. jScrollPane1.setViewportView(ssh_output);
  92.  
  93. clear_text.setText("Clear");
  94. clear_text.addActionListener(new java.awt.event.ActionListener() {
  95. public void actionPerformed(java.awt.event.ActionEvent evt) {
  96. clear_textActionPerformed(evt);
  97. }
  98. });
  99.  
  100. javax.swing.GroupLayout ssh_output_pannelLayout = new javax.swing.GroupLayout(ssh_output_pannel);
  101. ssh_output_pannel.setLayout(ssh_output_pannelLayout);
  102. ssh_output_pannelLayout.setHorizontalGroup(
  103. ssh_output_pannelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  104. .addGroup(ssh_output_pannelLayout.createSequentialGroup()
  105. .addContainerGap()
  106. .addGroup(ssh_output_pannelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  107. .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 591, Short.MAX_VALUE)
  108. .addComponent(clear_text, javax.swing.GroupLayout.Alignment.TRAILING))
  109. .addContainerGap())
  110. );
  111. ssh_output_pannelLayout.setVerticalGroup(
  112. ssh_output_pannelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  113. .addGroup(ssh_output_pannelLayout.createSequentialGroup()
  114. .addContainerGap()
  115. .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 371, Short.MAX_VALUE)
  116. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  117. .addComponent(clear_text)
  118. .addContainerGap())
  119. );
  120.  
  121. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  122. getContentPane().setLayout(layout);
  123. layout.setHorizontalGroup(
  124. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  125. .addComponent(ssh_input, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  126. .addComponent(ssh_output_pannel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  127. );
  128. layout.setVerticalGroup(
  129. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  130. .addGroup(layout.createSequentialGroup()
  131. .addComponent(ssh_input, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  132. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  133. .addComponent(ssh_output_pannel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  134. .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  135. );
  136. }// </editor-fold>
  137.  
  138. private void connectActionPerformed(java.awt.event.ActionEvent evt) {
  139. // TODO add your handling code here:
  140. /*get host_name*/
  141. host_name=hostname.getText();
  142. user_name=username.getText();
  143. pass_word=password.getText();
  144. /* Create a connection instance */
  145. app_conn=new Connection(host_name);
  146. try
  147. {
  148. app_conn.connect();
  149. /* Authenticate.
  150. * If you get an IOException saying something like
  151. * "Authentication method password not supported by the server at this stage."
  152. * then please check the FAQ.
  153. */
  154. boolean isAuthenticated = app_conn.authenticateWithPassword(user_name, pass_word);
  155.  
  156. if (isAuthenticated == false)
  157. throw new IOException("Authentication failed.");
  158.  
  159. JOptionPane.showMessageDialog(null, "You are connected to host "+host_name, "Connection",
  160. JOptionPane.INFORMATION_MESSAGE);
  161. /* Create a session */
  162. }
  163. catch(IOException e)
  164. {}
  165.  
  166.  
  167. }
  168.  
  169. private void ssh_commandActionPerformed(java.awt.event.ActionEvent evt) {
  170. // TODO add your handling code here:
  171. String command=ssh_command.getText();
  172. try
  173. {
  174.  
  175. app_session=app_conn.openSession();
  176. app_session.execCommand( command );
  177. InputStream stdout = new StreamGobbler( app_session.getStdout() );
  178. BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
  179. String line ;
  180. while( (line= br.readLine()) != null )
  181. {
  182. ssh_output.append( line + "n" );
  183. //line = br.readLine();
  184. }
  185. // DEBUG: dump the exit code
  186. System.out.println( "ExitCode: " + app_session.getExitStatus() );
  187.  
  188. // Close the session
  189. app_session.close();
  190. }
  191. catch(Exception e)
  192. {}
  193. }
  194.  
  195. private void clear_textActionPerformed(java.awt.event.ActionEvent evt) {
  196. // TODO add your handling code here:
  197. ssh_output.setText("");
  198. }
  199.  
  200.  
  201. // Variables declaration - do not modify
  202. private javax.swing.JButton clear_text;
  203. private javax.swing.JButton connect;
  204. private javax.swing.JTextField hostname;
  205. private javax.swing.JLabel jLabel1;
  206. private javax.swing.JLabel jLabel2;
  207. private javax.swing.JLabel jLabel3;
  208. private javax.swing.JScrollPane jScrollPane1;
  209. private javax.swing.JPasswordField password;
  210. private javax.swing.JTextField ssh_command;
  211. private javax.swing.JPanel ssh_input;
  212. private javax.swing.JTextArea ssh_output;
  213. private javax.swing.JPanel ssh_output_pannel;
  214. private javax.swing.JTextField username;
  215. // End of variables declaration
Add Comment
Please, Sign In to add comment