The following Code is not returning any command output. If anyone can help me here I'd be really happy.
public String cmd(
String command) throws Exception {
try{
String username = MainActivity._user;
String password = MainActivity._pass;;
String hostname = MainActivity._host;
String port = MainActivity._port;
JSch jsch = new JSch();
Session session = jsch.getSession(username, hostname, Integer.parseInt(port));
session.setPassword(password);
// Avoid asking for key confirmation
Properties prop = new Properties();
prop.put("StrictHostKeyChecking", "no");
session.setConfig(prop);
session.connect();
// SSH Channel
ChannelExec channelssh = (ChannelExec) session.openChannel("exec");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
channelssh.setOutputStream(baos);
channelssh.setCommand(command);
channelssh.connect();
while(true){
if(channelssh.isClosed()){
break;
}
}
channelssh.disconnect();
return baos.toString();
} catch (Exception e){
Log.e("ControlActivity-Login:", e.getMessage());
return "ERROR";
}
}
Although the same function in MainActivity.java does.
public static String executeRemoteCommand(
String username,
String password,
String hostname,
int port,
String command) throws Exception {
try{
hostname = hostname.replace(" ", "");
JSch jsch = new JSch();
Session session = jsch.getSession(username, hostname, 22);
session.setPassword(password);
// Avoid asking for key confirmation
Properties prop = new Properties();
prop.put("StrictHostKeyChecking", "no");
session.setConfig(prop);
session.connect();
// SSH Channel
ChannelExec channelssh = (ChannelExec) session.openChannel("exec");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
channelssh.setOutputStream(baos);
channelssh.setCommand(command);
channelssh.connect();
while(true){
if(channelssh.isClosed()){
break;
}
}
channelssh.disconnect();
return baos.toString();
} catch (Exception e){
Log.e("MainActivity-Login:", e.getMessage());
return "ERROR";
}
}
The following Code is not returning any command output. If anyone can help me here I'd be really happy.
Although the same function in MainActivity.java does.