java - SSH Output always empty -
i've been trying figure out problem hours , cant seem figure out. i'm trying utilize jsch ssh linux computer android phone. commands work fine output of channel empty. displays output of time doesn't. here's code found online.
string username = "user"; string password = "test123"; string connectionip = "192.168.1.13"; jsch jsch = new jsch(); session session; session = jsch.getsession(username, connectionip, 22); session.setpassword(password); // avoid asking key confirmation 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); // execute command channelssh.setcommand("ls"); channelssh.connect(); channelssh.disconnect(); result = baos.tostring();
result empty. if alter command mkdir or of nature files show on linux computer leads me believe command part working correctly. problem seems lie within bytearrayoutputstream. i've tested connectionip, username , password on different computer through terminal know credentials correct. i've googled problem death, input help me out significantly!
found reply reading wrong stream. heres proper code others problem.
inputstream inputstream = channelssh.getinputstream(); bufferedreader bufferedreader = new bufferedreader(new inputstreamreader(inputstream)); stringbuilder stringbuilder = new stringbuilder(); string line; while ((line = bufferedreader.readline()) != null) { stringbuilder.append(line); stringbuilder.append('\n'); } homecoming stringbuilder.tostring();
java android ssh outputstream jsch
No comments:
Post a Comment