[英]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. 我正在尝试在Java中创建代码,该代码通过在Linux中成功测试的minicom包使用GSM调制解调器发送短信。
Right know I'm using JSch library to SSH and use minicom. 知道我正在使用JSch库进行SSH并使用minicom。 I've successfully tested this code with normal Linux commands.
我已经使用普通的Linux命令成功测试了此代码。
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: 但是问题是我无法向minicom发送命令,它将输出以下内容:
No cursor motion capability (cm)
无光标移动能力(厘米)
I believe it need terminal environment and I don't want to use terminal emulator in Java. 我相信它需要终端环境,并且我不想在Java中使用终端仿真器。 I just want to send command to minicom through Java code.
我只想通过Java代码向minicom发送命令。
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. PS:我尝试了使用smslib解决此问题的其他方法,但由于rxtx和其他解决方案,它在Linux中无法正常工作。 Right now only minicom works properly.
目前,只有minicom可以正常工作。
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. 如果问题确实是该命令不是在非交互终端上启动,但实际上并不需要任何交互,则可以使用
.setPty(true)
调用使“ exec”通道“交互”。
channel.setPty(true);
channel.connect();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.