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?
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.
来源:https://stackoverflow.com/questions/44386496/certain-unix-commands-fail-with-not-found-when-executed-through-java-usin