Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Window extends JFrame {
- private JFrame frame;
- private static App sendPath;
- /**
- * Launch the application.
- */
- public static void main1(String[] args) {
- sendPath = new App();
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- Window window = new Window();
- window.frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- public Window() {
- inicialize();
- }
- // el chooser se selecciona desde el principio.
- public JFileChooser chooser = new JFileChooser();
- // message se inicializa desde el principio
- public JLabel message = new JLabel("");
- /**
- * Agrega el primer panel al JFrame
- */
- private void inicialize() {
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- JPanel user = new JPanel();
- add(user);
- placeComponents(user);
- }
- /**
- * Pone los elementos del primer panel y agrega el evento al botón
- */
- private void placeComponents(JPanel user) {
- user.setLayout(null);
- JLabel userLabel = new JLabel("Usuario");
- userLabel.setBounds(10, 10, 80, 25);
- user.add(userLabel);
- final JTextField userText = new JTextField(20);
- userText.setBounds(100, 10, 160, 25);
- user.add(userText);
- JButton loginButton = new JButton("ENVIAR");
- loginButton.setBounds(20, 85, 85, 30);
- user.add(loginButton);
- // esto agrega una acción al pulsar el botón
- loginButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent ae) {
- //SE DECLARA VARIABLE (NOMBRE USUARIO)
- String nombUser=userText.getText();
- System.out.println("Usuario es: "+nombUser);
- if (nombUser.equals(userText)) {
- JPanel user = new JPanel();
- user.setVisible(true);
- }
- // quita todo del panel principal
- Window.this.getContentPane().removeAll();
- // genera un segundo panel
- JPanel panelFileChooser = new JPanel();
- // al panel le agrega message
- panelFileChooser.add(message);
- // pone este segundo panel como panel principal
- Window.this.setContentPane(panelFileChooser);
- // refresca la pantalla
- Window.this.setVisible(true);
- // invoca la rutina para abrir el file chooser
- invocaChooser();
- }
- });
- }
- /** Abre el filechooser y con el directorio que se elige comienza a trabajar usando la clase App
- */
- public void invocaChooser() {
- chooser.setCurrentDirectory(new java.io.File("."));
- chooser.setDialogTitle("Elija su archivo");
- chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
- //
- // disable the "All files" option.
- //
- chooser.setAcceptAllFileFilterUsed(false);
- // si eligio un archivo se arranca la ejecución
- if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
- System.out.println("getCurrentDirectory(): "
- + chooser.getCurrentDirectory());
- System.out.println("getSelectedFile() : "
- + chooser.getSelectedFile());
- App sendPath = new App();
- //PARA WINDOWS PARA ENVIAR //sendPath.setPath(chooser.getSelectedFile().toString()+ "\" );
- sendPath.setPath(chooser.getSelectedFile().toString() + "/");
- try {
- // intenta conectar al servidor
- message.setText("Conecting with server...");
- Window.this.setVisible(true);
- invocaDemonio();
- } catch (Exception e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- } else {
- System.out.println("No Selection ");
- }
- }
- /** Invoca al demonio pero lo hace con un hilo.
- Esto permite refrescar la pantalla (la variable message) y
- y cuando termina invoca a keyGen.
- */
- public void invocaDemonio() {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- App.daemon();
- message.setText("Generating public key...");
- Window.this.setVisible(true);
- invocaKeyGen();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- public void invocaKeyGen() {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- App.sshKeyGen();
- message.setText("Sending public key to server...");
- Window.this.setVisible(true);
- invocaBinarios();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- public void invocaBinarios() {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- App.sshBinaries();
- message.setText("SSH conection ...");
- Window.this.setVisible(true);
- invocaSsh();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- public void invocaSsh() {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- App.ssh();
- message.setText("RSYNC conection ...");
- Window.this.setVisible(true);
- invocaSyncBinaries();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- public void invocaSyncBinaries() {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- App.sshBinaries();
- message.setText("Done");
- Window.this.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- public static void main(String[] args) {
- //sendWindow = new Window();
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- Window user = new Window();
- // esto hace que el sistema operativo ubique la ventana
- user.setLocationRelativeTo(null);
- // esto define el tamaño de la ventana
- user.setSize(450, 300);
- // se pinta la ventana
- user.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- }
- public class App extends Window{
- private static String path;
- public static void daemon() throws Exception{
- System.out.println("Conecting to server ....");
- RSync rsync = new RSync();
- //VARIABLE NOMBRE USUARIO RECUPERA EL NOMBRE DEL USUARIO INGRESADO
- String nombUser="";
- //SSH VIA RSYNC
- rsync.setOptions(new String[]{"-avz","-e","ssh",path,nombUser+"@192.168.2.219:rsync"});
- ConsoleOutputProcessOutput output = new ConsoleOutputProcessOutput();
- output.monitor(rsync.builder());
- }
- private static void nombUser() {
- // TODO Auto-generated method stub
- }
- //EJECUTA COMANDOS EN TERMINAL REMOTA
- public static void ssh() throws Exception {
- Ssh ssh = new Ssh()
- .outputCommandline(true)
- .verbose(1)
- .hostname("madai@192.168.2.219")
- .command("echo hola servidor");
- ConsoleOutputProcessOutput output = new ConsoleOutputProcessOutput();
- output.monitor(ssh.builder());
- }
- //GENERA LLAVES PUBLICAS Y PRIVADAS
- public static void sshKeyGen() throws Exception{
- System.out.println("Generating ssh_key for secure conection");
- SshKeyGen keygen = new SshKeyGen()
- .outputCommandline(true)
- .verbose(1)
- .keyType("rsa")
- .newPassPhrase("")
- .comment("testkey")
- .keyFile("~/.ssh/id_rsa");
- ConsoleOutputProcessOutput output = new ConsoleOutputProcessOutput();
- output.monitor(keygen.builder());
- }
- public static void sshBinaries() throws Exception {
- System.out.println("Sending pubic key to server...");
- RSync rsync = new RSync()
- .source(path)
- .destination("crdntls")
- .recursive(true)
- .rsh(Binaries.sshBinary() + " -i " + Binaries.convertPath("sshkey\testkey.pub"));
- ConsoleOutputProcessOutput output = new ConsoleOutputProcessOutput();
- output.monitor(rsync.builder());
- }
- public void setPath(String path) {
- App.path = path;
- System.out.println("Path recived: " + path);
- }
- public String getPath() {
- return path;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement