Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.18 KB | None | 0 0
  1. public class Window extends JFrame {
  2.  
  3. private JFrame frame;
  4. private static App sendPath;
  5.  
  6. /**
  7. * Launch the application.
  8. */
  9. public static void main1(String[] args) {
  10. sendPath = new App();
  11. EventQueue.invokeLater(new Runnable() {
  12. public void run() {
  13. try {
  14. Window window = new Window();
  15. window.frame.setVisible(true);
  16. } catch (Exception e) {
  17. e.printStackTrace();
  18. }
  19. }
  20. });
  21. }
  22.  
  23. public Window() {
  24. inicialize();
  25. }
  26.  
  27. // el chooser se selecciona desde el principio.
  28. public JFileChooser chooser = new JFileChooser();
  29. // message se inicializa desde el principio
  30. public JLabel message = new JLabel("");
  31.  
  32.  
  33. /**
  34. * Agrega el primer panel al JFrame
  35. */
  36. private void inicialize() {
  37.  
  38. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  39.  
  40. JPanel user = new JPanel();
  41. add(user);
  42. placeComponents(user);
  43.  
  44. }
  45.  
  46. /**
  47. * Pone los elementos del primer panel y agrega el evento al botón
  48. */
  49. private void placeComponents(JPanel user) {
  50.  
  51. user.setLayout(null);
  52.  
  53. JLabel userLabel = new JLabel("Usuario");
  54. userLabel.setBounds(10, 10, 80, 25);
  55. user.add(userLabel);
  56.  
  57. final JTextField userText = new JTextField(20);
  58. userText.setBounds(100, 10, 160, 25);
  59. user.add(userText);
  60.  
  61.  
  62. JButton loginButton = new JButton("ENVIAR");
  63. loginButton.setBounds(20, 85, 85, 30);
  64. user.add(loginButton);
  65.  
  66.  
  67. // esto agrega una acción al pulsar el botón
  68. loginButton.addActionListener(new ActionListener() {
  69.  
  70. public void actionPerformed(ActionEvent ae) {
  71.  
  72. //SE DECLARA VARIABLE (NOMBRE USUARIO)
  73. String nombUser=userText.getText();
  74. System.out.println("Usuario es: "+nombUser);
  75.  
  76. if (nombUser.equals(userText)) {
  77. JPanel user = new JPanel();
  78. user.setVisible(true);
  79.  
  80. }
  81. // quita todo del panel principal
  82. Window.this.getContentPane().removeAll();
  83. // genera un segundo panel
  84. JPanel panelFileChooser = new JPanel();
  85. // al panel le agrega message
  86. panelFileChooser.add(message);
  87. // pone este segundo panel como panel principal
  88. Window.this.setContentPane(panelFileChooser);
  89. // refresca la pantalla
  90. Window.this.setVisible(true);
  91. // invoca la rutina para abrir el file chooser
  92. invocaChooser();
  93. }
  94. });
  95.  
  96. }
  97.  
  98. /** Abre el filechooser y con el directorio que se elige comienza a trabajar usando la clase App
  99. */
  100. public void invocaChooser() {
  101.  
  102. chooser.setCurrentDirectory(new java.io.File("."));
  103. chooser.setDialogTitle("Elija su archivo");
  104. chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  105. //
  106. // disable the "All files" option.
  107. //
  108. chooser.setAcceptAllFileFilterUsed(false);
  109. // si eligio un archivo se arranca la ejecución
  110. if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
  111.  
  112. System.out.println("getCurrentDirectory(): "
  113. + chooser.getCurrentDirectory());
  114.  
  115. System.out.println("getSelectedFile() : "
  116. + chooser.getSelectedFile());
  117. App sendPath = new App();
  118.  
  119. //PARA WINDOWS PARA ENVIAR //sendPath.setPath(chooser.getSelectedFile().toString()+ "\" );
  120. sendPath.setPath(chooser.getSelectedFile().toString() + "/");
  121. try {
  122. // intenta conectar al servidor
  123. message.setText("Conecting with server...");
  124. Window.this.setVisible(true);
  125. invocaDemonio();
  126. } catch (Exception e1) {
  127. // TODO Auto-generated catch block
  128. e1.printStackTrace();
  129. }
  130.  
  131. } else {
  132. System.out.println("No Selection ");
  133. }
  134.  
  135. }
  136. /** Invoca al demonio pero lo hace con un hilo.
  137. Esto permite refrescar la pantalla (la variable message) y
  138. y cuando termina invoca a keyGen.
  139. */
  140. public void invocaDemonio() {
  141. EventQueue.invokeLater(new Runnable() {
  142. public void run() {
  143. try {
  144. App.daemon();
  145. message.setText("Generating public key...");
  146. Window.this.setVisible(true);
  147. invocaKeyGen();
  148. } catch (Exception e) {
  149. e.printStackTrace();
  150. }
  151. }
  152. });
  153. }
  154.  
  155. public void invocaKeyGen() {
  156. EventQueue.invokeLater(new Runnable() {
  157. public void run() {
  158. try {
  159. App.sshKeyGen();
  160. message.setText("Sending public key to server...");
  161. Window.this.setVisible(true);
  162. invocaBinarios();
  163. } catch (Exception e) {
  164. e.printStackTrace();
  165. }
  166. }
  167. });
  168. }
  169.  
  170. public void invocaBinarios() {
  171. EventQueue.invokeLater(new Runnable() {
  172. public void run() {
  173. try {
  174. App.sshBinaries();
  175. message.setText("SSH conection ...");
  176. Window.this.setVisible(true);
  177. invocaSsh();
  178. } catch (Exception e) {
  179. e.printStackTrace();
  180. }
  181. }
  182. });
  183. }
  184.  
  185. public void invocaSsh() {
  186. EventQueue.invokeLater(new Runnable() {
  187. public void run() {
  188. try {
  189. App.ssh();
  190. message.setText("RSYNC conection ...");
  191. Window.this.setVisible(true);
  192. invocaSyncBinaries();
  193. } catch (Exception e) {
  194. e.printStackTrace();
  195. }
  196. }
  197. });
  198.  
  199. }
  200.  
  201. public void invocaSyncBinaries() {
  202. EventQueue.invokeLater(new Runnable() {
  203. public void run() {
  204. try {
  205. App.sshBinaries();
  206. message.setText("Done");
  207. Window.this.setVisible(true);
  208.  
  209. } catch (Exception e) {
  210. e.printStackTrace();
  211. }
  212. }
  213. });
  214. }
  215.  
  216. public static void main(String[] args) {
  217. //sendWindow = new Window();
  218. EventQueue.invokeLater(new Runnable() {
  219. public void run() {
  220. try {
  221. Window user = new Window();
  222. // esto hace que el sistema operativo ubique la ventana
  223. user.setLocationRelativeTo(null);
  224. // esto define el tamaño de la ventana
  225. user.setSize(450, 300);
  226. // se pinta la ventana
  227. user.setVisible(true);
  228. } catch (Exception e) {
  229. e.printStackTrace();
  230. }
  231. }
  232. });
  233. }
  234. }
  235.  
  236. public class App extends Window{
  237. private static String path;
  238. public static void daemon() throws Exception{
  239. System.out.println("Conecting to server ....");
  240. RSync rsync = new RSync();
  241. //VARIABLE NOMBRE USUARIO RECUPERA EL NOMBRE DEL USUARIO INGRESADO
  242. String nombUser="";
  243. //SSH VIA RSYNC
  244. rsync.setOptions(new String[]{"-avz","-e","ssh",path,nombUser+"@192.168.2.219:rsync"});
  245. ConsoleOutputProcessOutput output = new ConsoleOutputProcessOutput();
  246. output.monitor(rsync.builder());
  247. }
  248.  
  249. private static void nombUser() {
  250. // TODO Auto-generated method stub
  251.  
  252. }
  253.  
  254. //EJECUTA COMANDOS EN TERMINAL REMOTA
  255. public static void ssh() throws Exception {
  256. Ssh ssh = new Ssh()
  257. .outputCommandline(true)
  258. .verbose(1)
  259. .hostname("madai@192.168.2.219")
  260. .command("echo hola servidor");
  261. ConsoleOutputProcessOutput output = new ConsoleOutputProcessOutput();
  262. output.monitor(ssh.builder());
  263. }
  264.  
  265. //GENERA LLAVES PUBLICAS Y PRIVADAS
  266. public static void sshKeyGen() throws Exception{
  267. System.out.println("Generating ssh_key for secure conection");
  268. SshKeyGen keygen = new SshKeyGen()
  269. .outputCommandline(true)
  270. .verbose(1)
  271. .keyType("rsa")
  272. .newPassPhrase("")
  273. .comment("testkey")
  274. .keyFile("~/.ssh/id_rsa");
  275. ConsoleOutputProcessOutput output = new ConsoleOutputProcessOutput();
  276. output.monitor(keygen.builder());
  277. }
  278. public static void sshBinaries() throws Exception {
  279. System.out.println("Sending pubic key to server...");
  280. RSync rsync = new RSync()
  281. .source(path)
  282. .destination("crdntls")
  283. .recursive(true)
  284. .rsh(Binaries.sshBinary() + " -i " + Binaries.convertPath("sshkey\testkey.pub"));
  285. ConsoleOutputProcessOutput output = new ConsoleOutputProcessOutput();
  286. output.monitor(rsync.builder());
  287. }
  288.  
  289. public void setPath(String path) {
  290. App.path = path;
  291. System.out.println("Path recived: " + path);
  292.  
  293. }
  294. public String getPath() {
  295. return path;
  296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement