如何在 Java 中通过 JSch SSH 会话使用需要交互式终端的命令行 *nix 命令?
How to use a command-line *nix command that needs an interactive terminal over JSch SSH session in Java?
我正在尝试在 java 中创建代码,通过我在 Linux.
中测试成功的 minicom 包使用 GSM 调制解调器发送 SMS
我知道我正在使用 JSch 库来 SSH 并使用 minicom。我已经使用正常的 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();
}
}
}
但问题是我无法向 minicom 发送命令,它会输出:
No cursor motion capability (cm)
我认为它需要终端环境,我不想在 Java 中使用终端模拟器。我只想通过 Java 代码向 minicom 发送命令。
有什么办法可以做到这一点吗?
PS:我已经尝试过使用 smslib 解决此问题的其他可能性,但由于 rxtx 和其他解决方案,它无法在 Linux 中正常工作。现在只有 minicom 可以正常工作。
如果问题确实是命令没有在非交互式终端上启动,但它确实不需要任何交互,您可以简单地使用 "exec" 频道 "interactive" .setPty(true)
呼唤。
channel.setPty(true);
channel.connect();
我正在尝试在 java 中创建代码,通过我在 Linux.
中测试成功的 minicom 包使用 GSM 调制解调器发送 SMS我知道我正在使用 JSch 库来 SSH 并使用 minicom。我已经使用正常的 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();
}
}
}
但问题是我无法向 minicom 发送命令,它会输出:
No cursor motion capability (cm)
我认为它需要终端环境,我不想在 Java 中使用终端模拟器。我只想通过 Java 代码向 minicom 发送命令。
有什么办法可以做到这一点吗?
PS:我已经尝试过使用 smslib 解决此问题的其他可能性,但由于 rxtx 和其他解决方案,它无法在 Linux 中正常工作。现在只有 minicom 可以正常工作。
如果问题确实是命令没有在非交互式终端上启动,但它确实不需要任何交互,您可以简单地使用 "exec" 频道 "interactive" .setPty(true)
呼唤。
channel.setPty(true);
channel.connect();