当使用启用了setPty的JSch通过Java执行某些Unix命令时,它失败并显示"... not found". [英] Certain Unix commands fail with "... not found", when executed through Java using JSch even with setPty enabled

查看:120
本文介绍了当使用启用了setPty的JSch通过Java执行某些Unix命令时,它失败并显示"... not found".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个Android应用,该应用将命令发送到设备上的linux终端.使用"ls"命令时,我可以获得正确的输出,但是当我使用"ifconfig"或"iwconfig"时,设备没有任何输出.使用Tera Term,我已经验证了这些命令可以正常工作.我试图包括((ChannelExec)channel).setPty(true);来解决此问题,但是终端似乎仍然无法识别该命令.添加该行代码还将"ls"命令的输出更改为我无法识别的内容.

I'm creating an android app that sends commands to a linux terminal on a device. I am able to get the proper output when using the "ls" command, however the device does not give me any output when I use "ifconfig" or "iwconfig". Using Tera Term, I have verified these commands do work. I have tried including ((ChannelExec)channel).setPty(true); in order to fix this, however the terminal still appears to not recognise the command. Adding that line of code also changes my output for the "ls" command to something I do not recognise.

这是我的JSch代码:

This is my JSch code:

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;
    }

}

输入

iwconfig

iwconfig

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

without ((ChannelExec)channel).setPty(true);. With it, my output is:

ash:iwconfig:未找到
退出状态:127

ash: iwconfig: not found
exit-status: 127

我在

ifconfig

ifconfig

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

My output of the "ls" command with ((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"命令的输出:

My output of the "ls" command without ((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命令的正确输出?

I am not sure what I am doing wrong, how I can fix this so that I can get the correct output for both the iwconfig/ifconfig and ls commands?

推荐答案

您的服务器/外壳配置不正确.当没有启动Shell会话时,它不能正确设置PATH.这就是为什么找不到ifconfig/iwconfig二进制文件的原因.

Your server/shell is misconfigured somehow. It does not set the PATH correctly, when a shell session is not started. That's, why the ifconfig/iwconfig binaries cannot be found.

请修复您的启动脚本以在所有情况下正确设置PATH.或使用ifconfig/iwconfig的完整路径.

Either fix your startup scripts to set the PATH correctly for all situations. Or use a full path to the ifconfig/iwconfig.

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

To find the full path, open a regular shell session using your SSH client and type:

which ifconfig


对于类似的问题,请参见在使用JSch通过Java执行某些Unix命令时,失败并显示"... not found". /a>.


For a similar issue, see Certain Unix commands fail with "... not found", when executed through Java using JSch.

这篇关于当使用启用了setPty的JSch通过Java执行某些Unix命令时,它失败并显示"... not found".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆