Ismail Sensei
Ismail Sensei

Reputation: 207

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.

Upvotes: 1

Views: 329

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202494

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();

Upvotes: 1

Related Questions