我正在创建一个将命令发送到设备上linux终端的android应用.使用“ ls”命令时,我可以获得正确的输出,但是当我使用“ ifconfig”或“ iwconfig”时,设备没有任何输出.使用Tera Term,我已经验证了这些命令是否有效.我试过包括((ChannelExec)channel).setPty(true);为了解决此问题,终端仍然无法识别该命令.添加该行代码还将“ ls”命令的输出更改为我无法识别的输出.
这是我的JSch代码:
package com.example.riot94.whizpacecontroller;
import android.os.AsyncTask;
import android.util.Log;
import java.io.IOException;
import java.io.InputStream;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
/**
* Created by riot94 on 1/6/2017.
*/
public class JSchConnectionProtocol extends AsyncTask{
private String host;
private String user;
private String password;
public JSchConnectionProtocol(String h, String u, String p){
host = h;
user = u;
password = p;
}
@Override
protected String doInBackground(String... command) {
String output = "";
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.setTimeout(10000);
Log.d("CONNECTION", "Attempting to connect to " + host + " as user: " + user);
session.connect();
Log.d("CONNECTION", "Connected to " + host + " as user: " + user);
Channel channel=session.openChannel("exec");
((ChannelExec)channel).setPty(true);
((ChannelExec)channel).setCommand(command[0]);
channel.setInputStream(null);
((ChannelExec)channel).setErrStream(System.err);
output = printOutputAfterXMilliSeconds(channel,1000);
channel.disconnect();
session.disconnect();
Log.d("DONE","DONE");
}catch(Exception e){
e.printStackTrace();
}
return output;
}
private String printOutputAfterXMilliSeconds(Channel channel, int ms) throws IOException, JSchException {
InputStream in=channel.getInputStream();
channel.connect();
String totalOutput = "";
byte[] tmp=new byte[1024];
while(true){
while(in.available()>0){
int i=in.read(tmp, 0, 1024);
if(i<0)break;
String output = new String(tmp, 0, i);
totalOutput += output;
Log.d("OUTPUT", output);
}
if(channel.isClosed()){
Log.d("EXIT_STAT","exit-status: "+channel.getExitStatus());
break;
}
try{
Log.d("PRE-SLEEP","About to sleep");
Thread.sleep(ms);
//channel.sendSignal("2");
Log.d("POST-SLEEP","Slept and woken");
}catch(Exception ee){
ee.printStackTrace();
channel.disconnect();
}
}
return totalOutput;
}
}
当我输入时,我的Android应用程序的活动中收到一个空字符串
iwconfig
没有((ChannelExec)channel).setPty(true);.有了它,我的输出是:
ash: iwconfig: not found
exit-status: 127
我得到类似的结果
ifconfig
我的(ls)命令的输出带有((ChannelExec)channel).setPty(true);:
[1;34mGUI[0m [1;32mmeter.sh[0m
[1;32mReadme4Gui[0m [0;0mmeter_iplist.txt[0m
[1;32mami_concentrator_ETH_20120413[0m [0;0mmeter_list.txt[0m
[1;32mami_demo_qingjun[0m [0;0mroute.sh[0m
[1;32mami_festtech[0m [1;32mscript.sh[0m
[1;32mami_mac[0m [1;32msetGateway.sh[0m
[1;32mami_qingjun[0m [1;32mspectrmgmt[0m
[1;32mbootup.sh[0m [1;32msystem.sh[0m
[1;32mconcentrator.sh[0m [1;32mtemp1.sh[0m
[1;32mdisFreq.sh[0m [1;32mtest.sh[0m
[1;32mdisLinkQuality.sh[0m [1;32mtest1.sh[0m
我没有((ChannelExec)channel).setPty(true);的“ ls”命令的输出:
GUI
Readme4Gui
ami_concentrator_ETH_20120413
ami_demo_qingjun
ami_festtech
ami_mac
ami_qingjun
bootup.sh
concentrator.sh
disFreq.sh
disLinkQuality.sh
meter.sh
meter_iplist.txt
meter_list.txt
route.sh
script.sh
setGateway.sh
spectrmgmt
system.sh
temp1.sh
test.sh
test1.sh
我不确定自己在做什么错,如何解决此问题,以便同时获得iwconfig / ifconfig和ls命令的正确输出?