我正在编写一个简单的Android程序,这样我就可以通过手机在我的Ubuntu设备上运行某些程序了。我合法只是想要我的程序,发送命令,然后告诉我输出。寻找其他答案真的让我深入了解如何运行sudo,如果我只有1个命令运行。知道为什么我也没有输出?它确实连接,因为使用false凭据会给我一个错误。任何帮助,将不胜感激。我正在运行的命令是:sudo transmission-gtk。命令" ls"作品。所以我假设它是我做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();
}
}
什么有效(但给了我一个不同的错误是代码的粗体部分)。不管那意味着什么,我显然缺乏pty。目前正在试图弄清楚为什么我不能通过ssh运行图形程序。