1

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.

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

}

My android app receives an empty String in its activity when I enter

iwconfig

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

ash: iwconfig: not found
exit-status: 127

I get a similar result with

ifconfig

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

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

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?

29
  • Use command-line SSH client (like ssh on *nix or plink on Windows) to run these commands: ssh -T username@host ifconfig, ssh -t username@host ifconfig, echo ifconfig | ssh -t username@host and echo ifconfig | ssh -T username@host. Which of these work and which not? Commented Jun 6, 2017 at 10:01
  • Do I understand right that with ((ChannelExec)channel).setPty(true); you get the same behavior with ifconfig as without the setPty? Commented Jun 6, 2017 at 10:03
  • Sorry I don't understand the first comment, I should run ssh -T username@host ifconfig and the other commands on an SSH is that it? Will VM linux or Git Bash work? username would be my computer's username and host would be the IP addr I am trying to connect to, is that right? As for ifconfig it gives me the same output ash: ifconfig: not found when I run the code individually in Eclipse. In the android app, without ((ChannelExec)channel).setPty(true); the output is an empty String "" whereas with ((ChannelExec)channel).setPty(true);, the output is ash: ifconfig: not found Commented Jun 7, 2017 at 1:40
  • Yes, VM linux or Git bash would do. But you can actually use the commands in SSH terminal of the server itself (connecting back to the host). username and host are the same username and host that you have in your Java code. - "As for ifconfig it gives me the same output ash: ifconfig: not found when I run the code individually in Eclipse" - Not sure what you refer to. Did you actually try the commands I've suggested in Eclipse? And all of them give you that error message? Commented Jun 7, 2017 at 6:04
  • I edited my question with the results of the commands you suggested above. I'm not too sure what those commands are doing though Commented Jun 7, 2017 at 6:58

1 Answer 1

3

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.

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

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

which ifconfig

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.