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.