0

I'm writing a simple Android program, that would just let me run certain programs on my Ubuntu device from my phone. I legit just want my program, to send a command, then tell me the output. Looking for the other answers really gave me an insight on how to run with sudo, if I only get 1 command to run. Any idea why I get no output either? It does connect, since using false credentials gives me an error. Any help would be appreciated. The command I'm running is: sudo transmission-gtk. The command "ls" works. So I'm assuming it's the way I do sudo?

package david.sshapplication;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;

import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

import java.util.Properties;

public class LoginActivity extends AppCompatActivity {
 private EditText pw;
private EditText uname;
private EditText hostname;
private EditText port;
private EditText command;
private EditText response;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    pw = (EditText)findViewById(R.id.pw);
    uname = (EditText)findViewById(R.id.uname);
    hostname = (EditText)findViewById(R.id.ip);
    port = (EditText)findViewById(R.id.port);
    command = (EditText)findViewById(R.id.command);
    response = (EditText)findViewById(R.id.response);

}
public void send(View v){
    final String password = pw.getText().toString();
    final String username = uname.getText().toString();
    final String hostname1 = hostname.getText().toString();
    final int port1 = Integer.valueOf(port.getText().toString());
    final String command1 = command.getText().toString();
    final String[] response1 = {null};

    new AsyncTask<Integer, Void, Void>(){
        @Override
        protected Void doInBackground(Integer... params) {
            try {
                String s = executeRemoteCommand(username,password,port1,hostname1,command1);
                response1[0] = s;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }.execute(1);
    System.out.println("XDDDDDDDDDDDDD");
    response.setText(response1[0]);
}
public static String executeRemoteCommand(String username, String password, int port, String hostname ,String command) throws Exception{
    JSch jSch = new JSch();
    Session session = jSch.getSession(username,hostname,port);
    session.setPassword(password);
    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);
    InputStream in= null;
    try {
        in = channelssh.getInputStream();
    } catch (IOException e) {
        e.printStackTrace();
    }

    // Execute command
    **channelssh.setPty(true);
    channelssh.setPtyType("VT100");**
           channelssh.setCommand("echo" + "SUDOPASS|sudo -S " + command);

    channelssh.connect();
 byte[] tmp=new byte[1024];
    while(true){
        try {
            while(in.available()>0){
                int i=in.read(tmp, 0, 1024);
                if(i<0)break;
                System.out.print(new String(tmp, 0, i));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(channelssh.isClosed()){
            try {
                if(in.available()>0) continue;
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("exit-status: "+channelssh.getExitStatus());
            break;
        }
        try{Thread.sleep(1000);}catch(Exception ee){}
    }
    System.out.println("DISCONNECTED");
    channelssh.disconnect();

    return baos.toString();

}
}

What worked(but gave me a different error is the part of the code in bold). I was apparently lacking pty, whatever that means. Currently trying to figure out why I cant run graphical programs through ssh.

Nephilim
  • 494
  • 5
  • 25
  • You have to wait for the command to finish + Why do you ask about "password", if you know that you do not have a problem with password? – Martin Prikryl Sep 13 '17 at 20:44
  • To get the output? – Nephilim Sep 13 '17 at 20:44
  • I do not understand what you react to. – Martin Prikryl Sep 13 '17 at 20:45
  • I responded before you edited. The command finishes when I call .disconnect() right? I print it out after that. When I type sudo transmission-gtk into my laptop, it asks for a password, the sudo password. And since the command wont run(doesnt open up the Transmission program on my laptop) I just assumed I wasnt inputting sudo password correctly. – Nephilim Sep 13 '17 at 20:52
  • No it does not wait for the command to finish. – Martin Prikryl Sep 13 '17 at 20:59
  • So when does the command finish? How can I even tell – Nephilim Sep 13 '17 at 21:01
  • Possible duplicate of [Running command using "exec" channel with JSch does not return any output](https://stackoverflow.com/questions/36380274/running-command-using-exec-channel-with-jsch-does-not-return-any-output) – Martin Prikryl Sep 13 '17 at 21:01
  • Adding the thing that's supposed to keep it waiting until command finishes didnt work. I'll edit my code in a second – Nephilim Sep 13 '17 at 21:06
  • Sorry, but you have to tell us more. *"Didn't work"* is a bad problem description. – Martin Prikryl Sep 14 '17 at 05:03
  • I edited the code with what I tried. Adding PTY in combination with making my code wait until it's done executing now makes me able to execute commands, however nothing happens on my Laptop when I try to open it remotely. I've tried executing a script that opens up the program but same thing. Same problem when using the program JuiceSSH but not Home Remote Control. – Nephilim Sep 14 '17 at 10:50
  • Sorry, I have no idea what talk about. Makes no sense to. – Martin Prikryl Sep 14 '17 at 11:32

0 Answers0