i trying execute multiple commands through ssh protocol using jsch library. seem have stuck , cannot find solution. setcommand()
method can execute single commands per session. want execute commands sequentially connectbot app on android platform. far code :
package com.example.ssh; import java.io.bytearrayinputstream; import java.io.bytearrayoutputstream; import java.io.ioexception; import java.util.properties; import android.app.activity; import android.os.bundle; import android.view.view; import android.widget.edittext; import android.widget.textview; import android.widget.toast; import com.jcraft.jsch.channel; import com.jcraft.jsch.jsch; import com.jcraft.jsch.jschexception; import com.jcraft.jsch.session; public class examplessh extends activity { /** called when activity first created. */ edittext command; textview result; session session; bytearrayoutputstream baos; bytearrayinputstream bais; channel channel; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); bais = new bytearrayinputstream(new byte[1000]); command = (edittext) findviewbyid(r.id.edittext1); result = (textview) findviewbyid(r.id.terminal); } public void onssh(view v){ string username = "xxxyyyzzz"; string password = "aaabbbccc"; string host = "192.168.1.1"; // sample ip address if(command.gettext().tostring() != ""){ jsch jsch = new jsch(); try { session = jsch.getsession(username, host, 22); session.setpassword(password); properties properties = new properties(); properties.put("stricthostkeychecking", "no"); session.setconfig(properties); session.connect(30000); channel = session.openchannel("shell"); channel.setinputstream(bais); channel.setoutputstream(baos); channel.connect(); } catch (jschexception e) { // todo auto-generated catch block toast.maketext(this, e.getmessage(), toast.length_long).show(); } } else{ toast.maketext(this, "command cannot empty !", toast.length_long).show(); } } public void oncommand(view v){ try { bais.read(command.gettext().tostring().getbytes()); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } baos = new bytearrayoutputstream(); channel.setoutputstream(baos); result.settext(baos.tostring()); } }
the code seems connected server think there problem input , output array buffers because there no output @ all. can please guide me how handle input , output , server desired output?
if not have distinguish inputs or outputs of individual commands, answer aaron (giving commands in row, separated \n
or ;
) fine.
if have handle them separately, or don't know later commands before earlier ones finished: can open multiple exec
-channels on same session (i.e. connection), 1 after other (i.e. after 1 before closed). each 1 has own command. (but don't share environment, cd
command in first 1 has no effect on later ones.)
you have take care have session object around, , not create new 1 each command.
another option shell channel, , passing individual commands remote shell input (i.e. via stream). have take care not mix the input 1 command next command (i.e. works if know commands doing, or if have interactive user can supply both input command , next command, , knows 1 used when.)
Comments
Post a Comment