android - How do I add a sudo password to JSch? -
i'm writing simple android program, let me run programs on ubuntu device phone. legit want program, send command, tell me output. looking other answers gave me insight on how run sudo, if 1 command run. idea why no output either? connect, since using false credentials gives me error. appreciated. command i'm running is: sudo transmission-gtk. command "ls" works. i'm assuming it's way 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 different error part of code in bold). apparently lacking pty, whatever means. trying figure out why cant run graphical programs through ssh.
Comments
Post a Comment