Welcome to my site... Have a nice day...

Friday, 6 September 2013

SSH Client With Java



Hi my friend i write a ssh client for my universty project..

i am using Jsch library http://www.jcraft.com/jsch/ you can find it here.!


here my program:Mo_Ssh


This is my main code.. Sorry their description turkish :S when i have a little time  i will translate to english.

package IsletimSistemleriProje;//this is project package

//adding JSCH library.
import com.jcraft.jsch.*;
import java.awt.*;
import javax.swing.*;

public class IsletimSistemleriProje {

     public static void main(String[] arg){
   
    try{
        //Creating new object from JSCH library
      JSch jsch=new JSch();

      //host creating
      String host=null;
      //checking argumans if come from out
      if(arg.length>0){
          //adding coming arguman to host string
        host=arg[0];
      }
      else{
          //if no coming any argumans from out, we asking host name and username
        host=JOptionPane.showInputDialog("Enter username@hostname",
                                         "root"+"@83.136.252.56");
      }
      //we are parsing user name and host name
      String user=host.substring(0, host.indexOf('@'));
      host=host.substring(host.indexOf('@')+1);
     
      //we are creating new session on :22 port
      Session session=jsch.getSession(user, host, 22);
     
      //asking password
      String passwd = JOptionPane.showInputDialog("Enter password");
     
      //giving password to session
      session.setPassword(passwd);

      UserInfo ui = new MyUserInfo(){
          //if server have messaj we show them
        public void showMessage(String message){
          JOptionPane.showMessageDialog(null, message);
        }
        public boolean promptYesNo(String message){
            //Key accept message
          Object[] options={ "yes", "no" };
          int foo=JOptionPane.showOptionDialog(null,
                                               message,
                                               "Warning",
                                               JOptionPane.DEFAULT_OPTION,
                                               JOptionPane.WARNING_MESSAGE,
                                               null, options, options[0]);
          return foo==0;
        }

      };
      //adding session a user.
      session.setUserInfo(ui);
     
      //we are setting Connection time out.
      session.connect(30000); 

      //creting and starting shell
      Channel channel=session.openChannel("shell");

      //we set input values getting "System.in"
      channel.setInputStream(System.in);

      //we set output values show in "System.out"
      channel.setOutputStream(System.out);
      channel.connect(3*1000);
    }
    //Error handle
    catch(Exception e){
      System.out.println(e);
    }
  }

    
  public static abstract class MyUserInfo
                          implements UserInfo, UIKeyboardInteractive{
    public String getPassword(){ return null; }
    public boolean promptYesNo(String str){ return false; }
    public String getPassphrase(){ return null; }
    public boolean promptPassphrase(String message){ return false; }
    public boolean promptPassword(String message){ return false; }
    public void showMessage(String message){ }
    public String[] promptKeyboardInteractive(String destination,
                                              String name,
                                              String instruction,
                                              String[] prompt,
                                              boolean[] echo){
      return null;
    }
  }
}

0 comments: