Results 1 to 7 of 7
- 03-14-2011, 02:49 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
Converting string into inputstream and outputstream Jsch (ssh connection)
Hi,
I want to transform string commands into Inputstream, then send it to server using Jsch and see the result.
The Jsch/examples/shell allows us to load .profile that's a good thing(so don't try to offer exec which doesn't load shell commands in a file automatically! unless u can make that work...), but I want to create a program which interacts on button click not on endless entering of commands to javaSwingputty app... so i need to transform this(standart Jsch shell example) :
Into something where instead or reading commands from System.in(keyboard) to something like having a string String commands="ls;date;newgrp;lrp_list";Java Code:import com.jcraft.jsch.*; import java.awt.*; import javax.swing.*; public class Shell{ public static void main(String[] arg) throws JSchException{ try{ JSch jsch=new JSch(); String host=null; String user="user1"; host="server1.co.uk"; String pasww="12345"; Session session=jsch.getSession(user, host, 22); session.setPassword(pasww); session.setConfig("StrictHostKeyChecking", "no"); session.connect(); Channel channel=session.openChannel("shell"); channel.setInputStream(System.in);//read stuff from keyboard(newgrp,lrp_list...) channel.setOutputStream(System.out);//outprintln stuff channel.connect(); } catch(Exception e){ System.out.println(e); } }
and then send these commands to server and see the servers response just like in putty. This is what I have so far:
Java Code:import java.util.logging.Level; import java.util.logging.Logger; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import com.jcraft.jsch.JSch; import com.jcraft.jsch.JSchException; import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelShell; import com.jcraft.jsch.Session; import java.io.ByteArrayInputStream; public class ShellMultipleCommandsAfterLoadingDotprofile { public static void main(String[] arg) throws JSchException{ [COLOR="Green"] String commandToRun= "cat > furryanimal.txt;lrp_list;newgrp server1;date";//executing remote scripts InputStream in = null; OutputStream out =null; byte[] bytes = commandToRun.getBytes(); ByteArrayInputStream bais=new ByteArrayInputStream(bytes); [/COLOR] try {//connection JSch jsch = new JSch(); String user="user1"; host="server1.co.uk"; String pasww="12345"; Session session = null; session = jsch.getSession(user, host, 22); session.setPassword(pasww); session.setConfig("StrictHostKeyChecking", "no"); session.connect(); Channel channel = session.openChannel("shell"); //here the fun begins [COLOR="Green"] ((ChannelShell) channel).setInputStream(bais); //what does this thing do? in = channel.getInputStream(); channel.connect();//connected throug shell :) loaded .profile hopefully!? channel.setInputStream(in);// at this point I know that shell has loded .profile because I see the output, // but what about my commands(lrp_list;newgrp...)? were they excecuted? channel.setOutputStream(System.out);//here's the output of .profile! byte[] tmp = new byte[1024]; while (true) {// what does this do? excecute commands? while (in.available() > 0) { int i = in.read(tmp, 0, 1024); if (i < 0) { break; } } if (channel.isClosed()) { break; } try { Thread.sleep(1000);//some scripts might take up to 20 minutes to complete ??? } catch (Exception ee) { } // System.out.println("After reading smtng! inputstream> "+in); loop... //System.out.println(result after each command, to see if we have succeded 1)to load .profile //and see the list of its commands, yes we have //2) to see the result of each command ls;newgrp;date... I think no);[/COLOR] } } catch (IOException ex) { Logger.getLogger(ShellMultipleCommandsAfterLoadingDotprofile.class.getName()).log(Level.SEVERE, null, ex); } catch (JSchException ex) { Logger.getLogger(ShellMultipleCommandsAfterLoadingDotprofile.class.getName()).log(Level.SEVERE, null, ex); } finally { try { in.close(); } catch (IOException ex) { Logger.getLogger(ShellMultipleCommandsAfterLoadingDotprofile.class.getName()).log(Level.SEVERE, null, ex); } } }}Last edited by Greed; 03-14-2011 at 02:52 PM.
- 03-14-2011, 03:03 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,383
- Blog Entries
- 7
- Rep Power
- 17
Have a look at the ByteArrayInputStream and ByteArrayOutputStream classes. the String.getBytes() method delivers the byte array for your output stream and one of String's constructor can convert a byte array to a String again.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-15-2011, 01:15 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
The point is that this program doesn't excecute commands!!!
so my string command is ("furry.txt;lrp_list;newgrp server1;date");
now how transformation goes from string to InputStream;
Because I believe the command is being send to server using command:channel.setInputStream(in);// ...setInputStream(InputStream in); there is no documentation about this...
But for some divine reason no commands go to the server because file furry.txt is not created :/
System out println:
String command: cat > furry.txt;lrp_list;newgrp server1;date
byte[] bytes : cat > furry.txt;lrp_list;newgrp server1;date
ByteArrayInputStream bais: java.io.ByteArrayInputStream@a90653
Input stream in : com.jcraft.jsch.Channel$MyPipedInputStream@30c221
command:channel.setInputStream(in);//this loads .profile at least
if i try command:channel.setInputStream(System.in); //format :java.io.BufferedInputStream@42719c
but either way this thing doesn't excecute the commands, i believe something is wrong with giving String command or the way I send it to the server...
- 03-15-2011, 02:12 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,383
- Blog Entries
- 7
- Rep Power
- 17
- 03-16-2011, 07:40 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
Yes I did read your last post Joe :)(and documentaition of inputstreams), unfortunatelly this way doesn't work:
Jsch is a very interesting thing...Java Code:[COLOR="Green"] String commandToRun= "cat > furry.txt;lrp_list;newgrp server1;date;"+" exit\n";//executing remote scripts byte[] bytes = commandToRun.getBytes(); input= new ByteArrayInputStream(bytes);[/COLOR] ((ChannelShell) channel).setInputStream(input); //what does this thing do? in = channel.getInputStream();//"Go, I will. Good relations with the Wookiees, I have." Yoda channel.connect(); [COLOR="Green"] channel.setInputStream(input);[/COLOR]//nothing works channel.setOutputStream(System.out);//no output
- 03-16-2011, 07:56 AM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Why are you even using setInputStream? Simply write the string to the inputstream. It is as easy as that.
- 03-16-2011, 09:07 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Converting to String
By darek9576 in forum New To JavaReplies: 1Last Post: 03-13-2010, 10:07 PM -
Is it OK to do this... (converting int to string)
By Psyclone in forum New To JavaReplies: 1Last Post: 02-16-2010, 05:51 PM -
Can I connect InputStream to OutputStream
By Splat in forum New To JavaReplies: 2Last Post: 10-22-2009, 12:30 AM -
[SOLVED] simple server chat program, using InputStream and OutputStream. NullPointerE
By jim478 in forum New To JavaReplies: 2Last Post: 07-28-2008, 07:32 PM -
Converting InputStream to OutputStream
By Java Tip in forum Java TipReplies: 1Last Post: 01-11-2008, 10:13 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks