运行 使用带有 JSch 的 "exec" 通道的命令不会 return 任何输出
Running command using "exec" channel with JSch does not return any output
我正在尝试使用 JSch 从 Android 使用 SSH 在 Linux 服务器上执行命令。
据我所知,我正在连接到服务器,但是当我尝试检索命令的结果时,我什么也没得到。
连接到服务器:
public class SSHCommand {
public static String executeRemoteCommand(
String username,
String password,
String hostname,
int port) throws Exception {
JSch jsch = new JSch();
Session session = jsch.getSession(username, hostname, port);
session.setPassword(password);
// Avoid asking for key confirmation
Properties prop = new Properties();
prop.put("StrictHostKeyChecking", "no");
session.setConfig(prop);
session.connect();
// SSH Channel
ChannelExec channelssh = (ChannelExec)
session.openChannel("exec");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
channelssh.setOutputStream(baos);
// Execute command
channelssh.setCommand("ls");
channelssh.connect();
channelssh.disconnect();
return baos.toString();
}
}
检索数据:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String TAG = "TESTING";
new AsyncTask<Integer, Void, Void>(){
@Override
protected Void doInBackground(Integer... params) {
try {
Log.d(TAG, SSHCommand.executeRemoteCommand("username", "password", "192.168.0.1", 22));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}.execute(1);
}
}
我在这里错过了什么?
您在启动命令后立即断开连接,然后返回任何输出。
您必须等待“exec”通道关闭(它会在命令完成后关闭)。
参见official JSch example for the "exec" channel。
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()){
if(in.available()>0) continue;
System.out.println("exit-status: "+channel.getExitStatus());
break;
}
try{Thread.sleep(1000);}catch(Exception ee){}
}
尽管要让命令可靠地完成并收集包括错误在内的所有输出,请参阅 How to read JSch command output?
在我将精心制作的代码放在 channelssh.connect();
之后,它对我有用
while(true){
if(channelssh.isClosed()){
break;
}
}
所以我完成的函数是:
public static String executeRemoteCommand(
String username,
String password,
String hostname,
int port) throws Exception {
try{
JSch jsch = new JSch();
Session session = jsch.getSession(username, hostname, 22);
session.setPassword(password);
// Avoid asking for key confirmation
Properties prop = new Properties();
prop.put("StrictHostKeyChecking", "no");
session.setConfig(prop);
session.connect();
// SSH Channel
ChannelExec channelssh = (ChannelExec) session.openChannel("exec");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
channelssh.setOutputStream(baos);
// Execute command
channelssh.setCommand("ls -al");
channelssh.connect();
while(true){
if(channelssh.isClosed()){
break;
}
}
channelssh.disconnect();
return baos.toString();
} catch (Exception e){
Log.e("Some Tag", e.getMessage());
return "ERROR";
}
}
我正在尝试使用 JSch 从 Android 使用 SSH 在 Linux 服务器上执行命令。
据我所知,我正在连接到服务器,但是当我尝试检索命令的结果时,我什么也没得到。
连接到服务器:
public class SSHCommand {
public static String executeRemoteCommand(
String username,
String password,
String hostname,
int port) throws Exception {
JSch jsch = new JSch();
Session session = jsch.getSession(username, hostname, port);
session.setPassword(password);
// Avoid asking for key confirmation
Properties prop = new Properties();
prop.put("StrictHostKeyChecking", "no");
session.setConfig(prop);
session.connect();
// SSH Channel
ChannelExec channelssh = (ChannelExec)
session.openChannel("exec");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
channelssh.setOutputStream(baos);
// Execute command
channelssh.setCommand("ls");
channelssh.connect();
channelssh.disconnect();
return baos.toString();
}
}
检索数据:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String TAG = "TESTING";
new AsyncTask<Integer, Void, Void>(){
@Override
protected Void doInBackground(Integer... params) {
try {
Log.d(TAG, SSHCommand.executeRemoteCommand("username", "password", "192.168.0.1", 22));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}.execute(1);
}
}
我在这里错过了什么?
您在启动命令后立即断开连接,然后返回任何输出。
您必须等待“exec”通道关闭(它会在命令完成后关闭)。
参见official JSch example for the "exec" channel。
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()){
if(in.available()>0) continue;
System.out.println("exit-status: "+channel.getExitStatus());
break;
}
try{Thread.sleep(1000);}catch(Exception ee){}
}
尽管要让命令可靠地完成并收集包括错误在内的所有输出,请参阅 How to read JSch command output?
在我将精心制作的代码放在 channelssh.connect();
while(true){
if(channelssh.isClosed()){
break;
}
}
所以我完成的函数是:
public static String executeRemoteCommand(
String username,
String password,
String hostname,
int port) throws Exception {
try{
JSch jsch = new JSch();
Session session = jsch.getSession(username, hostname, 22);
session.setPassword(password);
// Avoid asking for key confirmation
Properties prop = new Properties();
prop.put("StrictHostKeyChecking", "no");
session.setConfig(prop);
session.connect();
// SSH Channel
ChannelExec channelssh = (ChannelExec) session.openChannel("exec");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
channelssh.setOutputStream(baos);
// Execute command
channelssh.setCommand("ls -al");
channelssh.connect();
while(true){
if(channelssh.isClosed()){
break;
}
}
channelssh.disconnect();
return baos.toString();
} catch (Exception e){
Log.e("Some Tag", e.getMessage());
return "ERROR";
}
}