即使启用了setPty,使用JSch通过Java执行某些Unix命令,也会失败并显示“…not found”.
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了即使启用了setPty,使用JSch通过Java执行某些Unix命令,也会失败并显示“…not found”.,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4209字,纯文字阅读大概需要7分钟。
内容图文
我正在创建一个将命令发送到设备上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<String, Void, String>{
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命令的正确输出?
解决方法:
您的服务器/外壳配置不正确.当没有启动Shell会话时,它不能正确设置PATH.这就是为什么找不到ifconfig / iwconfig二进制文件的原因.
修复您的启动脚本以针对所有情况正确设置PATH.或使用ifconfig / iwconfig的完整路径.
要查找完整路径,请使用SSH客户端打开常规的shell会话,然后键入:
which ifconfig
对于类似的问题,请参阅Certain Unix commands fail with “… not found”, when executed through Java using JSch.
内容总结
以上是互联网集市为您收集整理的即使启用了setPty,使用JSch通过Java执行某些Unix命令,也会失败并显示“…not found”.全部内容,希望文章能够帮你解决即使启用了setPty,使用JSch通过Java执行某些Unix命令,也会失败并显示“…not found”.所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。