Hi,

I have a linux machine which I need to connect and send list of commands
using jsch java jar.

Following are the operations need to perform in linux server:
1. Connect linux server using ssh.
2. Execute login command and provide user name and password in linux shell
3. Show details of user.

Below are the commands which I need to automate using SSH connection.

[local_hots] mysh>login useradmin
Please enter user name: User1
Enter pin :
> ********                           ("123456" hint)

Authentication successful.

useradmin login successful.

Command Result : 0 (Success)
[local_host] mysh:> show user

I have connected linux server and send login command successfully through
java code but not able to enter user name and password.

Below is code which I am using

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Properties;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.ChannelShell;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;



public class JSchExampleSSHConnection {

    /**
     * JSch Example Tutorial
     * Java SSH Connection Program
     * @return
     */
    public static void main(String[] args) throws JSchException {
        String host="192.168.1.10";
        String user="admin";
        String password="linuxserver@123";



         JSch shell = new JSch();
        // get a new session

        Session session = null;

        String privateKey = "C:\\Users\\rahul\\.ssh\\id_rsa";

        try {
            shell.addIdentity(privateKey);
        } catch (JSchException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        try {
            session = shell.getSession(user, host, 22);

            System.out.println(session);
        //Session session = shell.getSession(userName, hostName, port);
        session.setPassword(password);
        Properties config = new Properties();
          config.put("StrictHostKeyChecking", ""
                  + "");
//          java.util.Properties config=new java.util.Properties();

          session.setConfig(config);

            session.connect();

            Channel channel = session.openChannel("shell");

              OutputStream ops = channel.getOutputStream();
              PrintStream ps = new PrintStream(ops, true);

              channel.connect();
              //give commands to be executed inside println.and can have
any no of commands sent.



              ps.println("login useradmin"");
              ps.println("User1");
              ps.println("password");


              ps.close();




              InputStream in=channel.getInputStream();



              byte[] bt=new byte[1024];


            while(true)
              {
                   while(in.available()>0)
                   {
                       int i=in.read(bt, 0, 1024);
                       if(i<0)
                           break;
                       strCmdOutput = new String(bt, 0, i);
                       System.out.println(strCmdOutput);

                   }
                   if(channel.isClosed())
                   {
                       break;
                   }

                   Thread.sleep(10000);
                   channel.disconnect();

                   session.disconnect();



        }


        }catch (JSchException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

 catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

}


Thanks & Regards
Rahul Katoch
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most 
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
JSch-users mailing list
JSch-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jsch-users

Reply via email to