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?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Ryan T.
  • 197
  • 1
  • 15
  • 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? – Martin Prikryl Jun 06 '17 at 10:01
  • Do I understand right that with `((ChannelExec)channel).setPty(true);` you get the same behavior with `ifconfig` as without the `setPty`? – Martin Prikryl Jun 06 '17 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` – Ryan T. Jun 07 '17 at 01: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? – Martin Prikryl Jun 07 '17 at 06: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 – Ryan T. Jun 07 '17 at 06:58
  • I didn't want you to run them in JSch. - How do you run Git Bash? I do not get the problem with *"Pseudo-terminal will not be allocated because stdin is not a terminal."* when I run the commands from Git Bash window. - If you are on Windows, you can use Plink instead of `ssh` from `cmd.exe` console window. - How come you get "connection timeout"? Is the Linux host reachable from the site, where you run the command on? – Martin Prikryl Jun 07 '17 at 07:17
  • My Command Prompt does not recognise `Plink` or `plink`. In Git Bash, I just type in the commands above, with the relevant `host` and `user`. Yes, the Linux host is reachable from the site. – Ryan T. Jun 07 '17 at 07:24
  • https://the.earth.li/~sgtatham/putty/latest/w32/plink.exe – Martin Prikryl Jun 07 '17 at 07:25
  • I am unable to run the the file, the application closes almost instantly when I try to run it. – Ryan T. Jun 07 '17 at 07:32
  • It's a console application, you have to run it from a console window (cmd.exe). – Martin Prikryl Jun 07 '17 at 07:33
  • I get the error: `ash: ifconfig: not found` – Ryan T. Jun 07 '17 at 07:38
  • For all four commands? (replace `ssh` with `plink` in the commands). – Martin Prikryl Jun 07 '17 at 08:07
  • I'm not sure how to run the last 2 commands with `plink`, but the first 2 commands give me `ash: ifconfig: not found` – Ryan T. Jun 07 '17 at 08:22
  • For clarity, I entered `E:/plink -t root@192.168.1.41 ifconfig` and `E:/plink -t root@192.168.1.41 ifconfig` – Ryan T. Jun 07 '17 at 08:23
  • Exactly the same way as with `ssh`: `echo ifconfig | E:\plink -t username@host` – Martin Prikryl Jun 07 '17 at 08:23
  • The last 2 commands give me: `'E:' is not recognised as an internal or external command, operable program or batch file.` – Ryan T. Jun 07 '17 at 08:26
  • Show me a screenshot of a whole console window with the error message. – Martin Prikryl Jun 07 '17 at 08:31
  • It should look like: https://i.stack.imgur.com/l3Vxg.png – Martin Prikryl Jun 07 '17 at 08:42
  • The screenshot: http://imgur.com/a/I6h68 – Ryan T. Jun 07 '17 at 09:47
  • Download and run `PuTTY.exe`, in *Host Name* box, enter `username@host`, go to *Connection > SSH > Kex* and move *"Diffie-Hellman group 1"* above the *"-- warn below here --"* line. And save settings to save session. Try to connect. Confirm the host key. And then with plink do `echo ifconfig | E:\plink store_session_name -t` (and `-T`). – Martin Prikryl Jun 07 '17 at 11:49
  • I'm getting the error `-ash: plink: not found`. Am I supposed to download plink on to the device I am trying to connect to? If yes, is there a way I can transfer the app on my computer onto the device? The device cannot be connected to the internet. – Ryan T. Jun 08 '17 at 02:37
  • Show us a screenshot of TeraTerm or PuTTY showing how you execute the `ifconfig`. – Martin Prikryl Jun 08 '17 at 03:39
  • Screenshot: http://imgur.com/a/sy5Dq – Ryan T. Jun 08 '17 at 03:47
  • No, just simple `ifconfig` command please. – Martin Prikryl Jun 08 '17 at 03:48
  • [Screenshot](http://imgur.com/a/iuiKN) – Ryan T. Jun 08 '17 at 03:52
  • In PuTTY, if you type `which ifconfig`, what do you get? – Martin Prikryl Jun 08 '17 at 04:13
  • I get `/sbin/ifconfig` – Ryan T. Jun 08 '17 at 05:40
  • Try using `/sbin/ifconfig` in your `setCommand`. – Martin Prikryl Jun 08 '17 at 05:42
  • It works! Thank you! So the issue was that I was not accessing the directory the command was in, is that correct? – Ryan T. Jun 08 '17 at 05:59

1 Answers1

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.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992