即使在启用 setPty 的情况下,使用 JSch 通过 Java 执行时,某些 Unix 命令失败并显示“...未找到”

Certain Unix commands fail with "... not found", when executed through Java using JSch even with setPty enabled

我正在创建一个 android 应用程序,它向设备上的 linux 终端发送命令。使用 "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 应用在其 activity 中收到一个空字符串

iwconfig

没有((ChannelExec)channel).setPty(true);。有了它,我的输出是:

ash: iwconfig: not found
exit-status: 127

我得到与

相似的结果

ifconfig

我使用 ((ChannelExec)channel).setPty(true); 的 "ls" 命令的输出:

[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

我的 "ls" 命令的输出没有 ((ChannelExec)channel).setPty(true);:

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/ifconfigls 命令的正确输出?

您的 server/shell 配置有误。当 shell 会话未启动时,它不会正确设置 PATH。这就是为什么找不到 ifconfig/iwconfig 二进制文件的原因。

要么修复您的启动脚本以针对所有情况正确设置 PATH。或者使用 ifconfig/iwconfig.

的完整路径

要查找完整路径,请使用 SSH 客户端打开常规 shell 会话并键入:

which ifconfig

对于类似的问题,请参阅