package de.jgsoftwares.sshclient;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.concurrent.TimeUnit;
import org.apache.sshd.client.ClientFactoryManager;
import org.apache.sshd.client.SshClient;
import org.apache.sshd.client.channel.ClientChannel;
import org.apache.sshd.client.future.ConnectFuture;
import org.apache.sshd.client.session.ClientSession;
import org.apache.sshd.common.PropertyResolverUtils;
import org.apache.sshd.common.cipher.BuiltinCiphers;
import org.apache.sshd.common.future.CancelOption;
import org.apache.sshd.common.util.io.input.NoCloseInputStream;
import org.apache.sshd.common.util.io.output.NoCloseOutputStream;
import org.apache.sshd.common.FactoryManager;
import org.apache.sshd.common.channel.Channel;
/**
 *
 * @author hoscho
 */
public class NewJPanel extends javax.swing.JPanel {

    
    
    
    ClientChannel channel;
    String stuser = null;
    String sthost = null;
    String stpassword = null;
    /**
     * Creates new form NewJPanel
     */
    public NewJPanel() {
        initComponents();
        jTextField1.setText("");
        jPasswordField1.setText("");
        jTextField2.setText("");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jPasswordField1 = new javax.swing.JPasswordField();
        jLabel1 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        jLabel2.setText("user");

        jTextField1.setText("jTextField1");

        jPasswordField1.setText("jPasswordField1");

        jLabel1.setText("Host");

        jTextField2.setText("jTextField2");

        jButton1.setText("connect");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jTextField1)
                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addGap(0, 0, Short.MAX_VALUE))
                        .addComponent(jTextField2)
                        .addComponent(jPasswordField1, javax.swing.GroupLayout.DEFAULT_SIZE, 261, Short.MAX_VALUE))
                    .addComponent(jLabel2))
                .addContainerGap(127, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(44, 44, 44)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap(96, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try {
            // TODO add your handling code here:

            stuser = jTextField1.getText();
            stpassword = jPasswordField1.getText();
            sthost = jTextField2.getText();

            sshconnect(stuser, stpassword, sthost);
        } catch (Exception ex) {
            //Logger.getLogger(SSHConnect.class.getName()).log(Level.SEVERE, null, ex);
            System.out.print("Error " + ex);
            
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    public void sshconnect(String stuser, String stpassword, String sthost) throws IOException
    {
           org.apache.sshd.client.SshClient sshclient = new org.apache.sshd.client.SshClient();
           sshclient.setCipherFactories(Arrays.asList(BuiltinCiphers.cc20p1305_openssh, BuiltinCiphers.aes256ctr, BuiltinCiphers.aes192ctr));
           SshClient client = SshClient.setUpDefaultClient();
           
           
           // PropertyResolverUtils.updateProperty(client, ClientFactoryManager.SEND_IMMEDIATE_IDENTIFICATION, false);
           PropertyResolverUtils.updateProperty(client, ClientFactoryManager.DEFAULT_VERSION, false);
         
          
           client.start();
           ConnectFuture connectFuture = client.connect(stuser, new InetSocketAddress(sthost, 22));
           
           ClientSession session = connectFuture.verify(1000, TimeUnit.SECONDS, CancelOption.CANCEL_ON_TIMEOUT).getSession();
          
           session.addPasswordIdentity(stpassword);
           //channel = session.createShellChannel();
           
          
              //ClientChannel channel = session.createChannel(ClientChannel.CHANNEL_SHELL);
              //ClientChannel channel = session.createShellChannel();
              
              ClientChannel channel;
              channel = session.createChannel(Channel.CHANNEL_SUBSYSTEM, stpassword); 
              channel.setOut(new NoCloseOutputStream(System.out)); 
              channel.setErr(new NoCloseOutputStream(System.err)); 
              channel.open().await(); 
              //channel.waitFor(EnumSet.of(ClientChannel.ClientChannelEvent.CLOSED), 0);
             
              channel.close(); 
              session.close(false); 
              client.stop();
              
              //channel.setIn(new NoCloseInputStream(System.in));
              //channel.setOut(new NoCloseOutputStream(System.out));
              //channel.setErr(new NoCloseOutputStream(System.err));
              //channel.open();
              //channel.waitFor(ClientChannel.CLOSED, 0);
          
           
           
           //session.auth().verify(1000, TimeUnit.SECONDS, CancelOption.CANCEL_ON_TIMEOUT);
           session.auth();
           
           //ClientChannel channel =session.createShellChannel();
           //channel.setOut(new NoCloseOutputStream(System.out));
           //channel.setRedirectErrorStream(true);
           //channel.setIn(new NoCloseInputStream(System.in));
           //channel.open().verify(1000, TimeUnit.SECONDS, 
           //CancelOption.CANCEL_ON_TIMEOUT);
   
           //channel.waitFor(Collections.singleton(ClientChannelEvent.CLOSED), 0);
           //channel.close();
           //session.close();
           //client.stop();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables
}
