简体   繁体   中英

How to use a command-line *nix command that needs an interactive terminal over JSch SSH session in Java?

I'm trying to create code in java that sends SMS using GSM modem through minicom package that I tested successfully in Linux.

Right know I'm using JSch library to SSH and use minicom. I've successfully tested this code with normal Linux commands.

import java.io.InputStream;

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


public class SSHCommandExecutor {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String host="localhost";
        String user="root";
        String password="XXXXX";
        String command1="minicom";
        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");

            Channel channel=session.openChannel("exec");
            ((ChannelExec)channel).setCommand(command1);
            channel.setInputStream(null);
            ((ChannelExec)channel).setErrStream(System.err);

            InputStream in=channel.getInputStream();
            channel.connect();
            byte[] tmp=new byte[1024];
            while(true){
              while(in.available()>0){
                int i=in.read(tmp, 0, 1024);
                if(i<0)break;
                System.out.print(new String(tmp, 0, i));
              }
              if(channel.isClosed()){
                System.out.println("exit-status: "+channel.getExitStatus());
                break;
              }
              try{Thread.sleep(1000);}catch(Exception ee){}
            }
            channel.disconnect();
            session.disconnect();
            System.out.println("DONE");
        }catch(Exception e){
            e.printStackTrace();
        }

    }

}

But the problem is that I can't send commands to minicom it will output this:

No cursor motion capability (cm)

I believe it need terminal environment and I don't want to use terminal emulator in Java. I just want to send command to minicom through Java code.

Is there any way I could do this?

PS: I've tried other possibilities to solve this using smslib but it couldn't work properly in Linux cause of the rxtx and other solutions. Right now only minicom works properly.

If the problem is indeed that the command does not start on a non-interactive terminal, yet it does not really need any interactivity, you can simply make the "exec" channel "interactive" by using .setPty(true) call.

channel.setPty(true);
channel.connect();

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