Guest User

Untitled

a guest
Jan 18th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. String unixCommand = "echo testing";
  2. runShellScript(unixCommand);
  3. JLabel labelel = new JLabel("");
  4. labelel.setText(runShellScript(unixCommand)); <----still error
  5.  
  6. import java.awt.BorderLayout;
  7. import java.awt.EventQueue;
  8. import java.io.BufferedReader;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.io.InputStreamReader;
  12.  
  13. import javax.swing.JFrame;
  14. import javax.swing.JPanel;
  15. import javax.swing.border.EmptyBorder;
  16.  
  17. import ch.ethz.ssh2.Connection;
  18. import ch.ethz.ssh2.Session;
  19. import ch.ethz.ssh2.StreamGobbler;
  20. import javax.swing.GroupLayout;
  21. import javax.swing.GroupLayout.Alignment;
  22. import javax.swing.JLabel;
  23.  
  24.  
  25. public class swcls extends JFrame {
  26.  
  27. private JPanel contentPane;
  28.  
  29. /**
  30. * Launch the application.
  31. */
  32. public static void runShellScript(String unixCommand) throws IOException, InterruptedException {
  33.  
  34. String hostname = "192.168.3.101";
  35. String username = "root";
  36. String password = "password";
  37.  
  38. boolean isAuthenticated = false;
  39.  
  40. try
  41. {
  42. Connection conn = new Connection(hostname);
  43.  
  44. conn.connect();
  45.  
  46. isAuthenticated=conn.authenticateWithPassword(username, password);
  47.  
  48. if (isAuthenticated == false)
  49. throw new IOException("Authentication failed.");
  50.  
  51. Session sess = conn.openSession();
  52.  
  53. //sess.execCommand("cd /;ls -l");
  54. sess.execCommand(unixCommand);
  55.  
  56. InputStream stdout = new StreamGobbler(sess.getStdout());
  57. InputStream stderr = new StreamGobbler(sess.getStderr());
  58.  
  59. InputStreamReader insrout=new InputStreamReader(stdout);
  60. InputStreamReader insrerr=new InputStreamReader(stderr);
  61.  
  62. BufferedReader stdoutReader = new BufferedReader(insrout);
  63.  
  64. BufferedReader stderrReader = new BufferedReader(insrerr);
  65.  
  66. while (true)
  67. {
  68. String line = stdoutReader.readLine();
  69. if (line == null)
  70. {
  71. break;
  72. }
  73. System.out.println(line);
  74. }
  75.  
  76. while (true)
  77. {
  78. String line = stderrReader.readLine();
  79. if (line == null)
  80. { break;}
  81. System.out.println(line);
  82. }
  83.  
  84. sess.close();
  85.  
  86. conn.close();
  87. }
  88. catch (IOException e)
  89. {
  90. e.printStackTrace(System.err);
  91. System.exit(2);
  92. }
  93.  
  94. }
  95.  
  96. public static void main(String[] args) {
  97. EventQueue.invokeLater(new Runnable() {
  98. public void run() {
  99. try {
  100. swcls frame = new swcls();
  101. frame.setVisible(true);
  102. } catch (Exception e) {
  103. e.printStackTrace();
  104. }
  105. }
  106. });
  107. }
  108.  
  109. /**
  110. * Create the frame.
  111. * @throws InterruptedException
  112. * @throws IOException
  113. */
  114. public swcls() throws IOException, InterruptedException {
  115. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  116. setBounds(100, 100, 450, 300);
  117. contentPane = new JPanel();
  118. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  119. contentPane.setLayout(new BorderLayout(0, 0));
  120. setContentPane(contentPane);
  121.  
  122. JPanel panel = new JPanel();
  123. contentPane.add(panel, BorderLayout.CENTER);
  124.  
  125. String unixCommand = "echo testing";
  126. runShellScript(unixCommand);
  127.  
  128. JLabel labelel = new JLabel("");
  129. labelel.setText(runShellScript(unixCommand));
  130.  
  131. GroupLayout gl_panel = new GroupLayout(panel);
  132. gl_panel.setHorizontalGroup(
  133. gl_panel.createParallelGroup(Alignment.LEADING)
  134. .addGroup(gl_panel.createSequentialGroup()
  135. .addGap(110)
  136. .addComponent(labelel)
  137. .addContainerGap(268, Short.MAX_VALUE))
  138. );
  139. gl_panel.setVerticalGroup(
  140. gl_panel.createParallelGroup(Alignment.LEADING)
  141. .addGroup(gl_panel.createSequentialGroup()
  142. .addGap(74)
  143. .addComponent(labelel)
  144. .addContainerGap(164, Short.MAX_VALUE))
  145. );
  146. panel.setLayout(gl_panel);
  147. }
  148.  
  149. }
Add Comment
Please, Sign In to add comment