简体   繁体   中英

Automation of ssh session using jsch library in java

I am automating the ssh session using java, using this code i am able to login and i get the output on screen, but after this, after we enter the application to run in the ssh session, it doesn't take username and password as it requires keyboard interactions like TAB and ENTER and function keys etc, so is there a way to send keyboard events in place of commands so that automation is successful?

public class jschputty {

public static void main(String args[])
{   
 String host="x.x.x.x";
 String user="username";
 String password="password";
 String command1="ls -ltr\t";

 try{

    java.util.Properties config = new java.util.Properties(); 
    config.put("StrictHostKeyChecking", "no");
    JSch jsch = new JSch();
    Session session=jsch.getSession(user, host, 22);
    session.setPassword(password);
    session.setConfig(config);
    session.connect();
    System.out.println("Connected");

    ChannelExec channel=(ChannelExec) session.openChannel("exec");
    BufferedReader in=new BufferedReader(new InputStreamReader(channel.getInputStream()));

  channel.setCommand(command);
    channel.connect();
           String msg=null;
           while((msg=in.readLine())!=null){
            System.out.println(msg);
           }                  
                channel.disconnect();
                session.disconnect();
                System.out.println("DONE");
            }catch(Exception e){
                e.printStackTrace();
            }

        }

}

如果要将输出发送到远程会话,我建议您写入channel.getOutputStream()返回的OutputStream。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM