Results 1 to 7 of 7
  1. #1
    Greed is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Smile 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) :


    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);
        }
      }
    Into something where instead or reading commands from System.in(keyboard) to something like having a string String commands="ls;date;newgrp;lrp_list";
    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 03:52 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,017
    Blog Entries
    7
    Rep Power
    23

    Default

    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,

    Jos
    I have the stamina of a seal; I lie on the beach instead of running on it.

  3. #3
    Greed is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    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...

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,017
    Blog Entries
    7
    Rep Power
    23

    Default

    Quote Originally Posted by Greed View Post
    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;
    You didn't read my previous reply did you? Convert your String to a byte array and use that byte array to create a ByteArrayInputStream; that's all there is to it.

    kind regards,

    Jos
    I have the stamina of a seal; I lie on the beach instead of running on it.

  5. #5
    Greed is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Yes I did read your last post Joe :)(and documentaition of inputstreams), unfortunatelly this way doesn't work:
    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
    Jsch is a very interesting thing...

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    10

    Default

    Why are you even using setInputStream? Simply write the string to the inputstream. It is as easy as that.

  7. #7
    Greed is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Well thx :) but i found this library sshtools and everything there seems way much easier (Jsch connection - 100 lines of code sshtools-20) + that works +it has documentation!!!! :)

Similar Threads

  1. Converting to String
    By darek9576 in forum New To Java
    Replies: 1
    Last Post: 03-13-2010, 11:07 PM
  2. Is it OK to do this... (converting int to string)
    By Psyclone in forum New To Java
    Replies: 1
    Last Post: 02-16-2010, 06:51 PM
  3. Can I connect InputStream to OutputStream
    By Splat in forum New To Java
    Replies: 2
    Last Post: 10-22-2009, 12:30 AM
  4. Replies: 2
    Last Post: 07-28-2008, 07:32 PM
  5. Converting InputStream to OutputStream
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 01-11-2008, 11:13 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •