Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: session.Channel(exec) command - PLease help

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    7
    My Mood
    Cool
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default session.Channel(exec) command - PLease help

    Hello folks,

    I am very new to JAVA and trying to learn the same.


    I need to write a scenario where I need to pass the command to remote server and execute it there and fetch the result. I searched the google and got multiple post on same and I tried one of the code which worked for me fine. Please find below the same code:
    package eclipsepackage;
     
    import java.io.IOException;
    import java.io.InputStream;
     
    import com.jcraft.jsch.ChannelExec;
    import com.jcraft.jsch.JSch;
    import com.jcraft.jsch.JSchException;
    import com.jcraft.jsch.Session;
     
    public class VE_GetDetails_1 
    {
     
    	public static void main(String[] args) throws JSchException, IOException{
    		// TODO Auto-generated method stub
      System.out.println("Hello");
      String sUsername = "<uname>";
      String sPassword = "<pwd>";
      String sSMS1 = "<IP address>";
      String sCMD = "<cmd>";
     
      JSch jsch = new JSch();
      Session session = null;
     
      session = jsch.getSession(sUsername, sSMS1, 22);
      session.setConfig("StrictHostKeyChecking", "no"); //authenticity no 
      session.setPassword(sPassword);
      session.connect();
     
            ChannelExec channel1 = (ChannelExec) session.openChannel("exec");
            channel1.setCommand(sCMD);
            channel1.setInputStream(null);
            ((ChannelExec) channel1).setErrStream(System.err);
            InputStream in = channel1.getInputStream(); // gets data from InputStream created !!!
            channel1.connect();
            // System.out.println("SMS system connected...");
            System.out.println("Console Output for command " + sCMD);
     
            PrintConsoleOutput(in,channel1); 
            channel1.disconnect();
     
    	}
    	public static void PrintConsoleOutput(InputStream in, ChannelExec channel) throws IOException
        {         
           System.out.println("-----------------------------------------------------");
        byte[] tmp = new byte[1024];
        while (true)
        {
            while (in.available() > 0) 
            {
                int i = in.read(tmp, 0, 1024);
                /*
                  Number of bytes actually read is returned as an integer.
     
                 */
                if (i < 0) 
                {
                    break;
                }
                String line = new String(tmp, 0, i);
                /*
                 String(char chars[ ], int startIndex, int numChars)
                 Here, startIndex specifies the index at which the subrange begins, and numChars specifies the number of characters to use. Here is an example:
                 char chars[] = { 'a', 'b', 'c', 'd', 'e', 'f' }; 
                 String s = new String(chars, 2, 3);
                 This initializes s with the characters cde.
                 */
                System.out.println(line);            
            }
            if (channel.isClosed())
            {
                break;
            }
            try 
            {
                Thread.sleep(1000);
            } 
            catch (Exception ee)
            {
                //ignore
            }
        }
     
        }
    }

    I tried to understand the code, but didnt get which command really triggering/executing the required linux command on remote host.
    getinputstream will fetch the byte data i.e. the result, but which command really executing on remote host?

    PLease let me know !!! and if possible, please help me in understanding the channel flow !!

    Thanks,
    Asif Masood
    Last edited by Asif Masood; April 12th, 2014 at 01:21 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: session.Channel(exec) command - PLease help

    That is not a familiar package. Read the API doc for the classes and methods to see how to use them.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    7
    My Mood
    Cool
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: session.Channel(exec) command - PLease help

    Hi Norm,

    eclipsepackage is the default eclipse package which is mandate to add.
    I am stuck in below part of code where command is passed through channel, How can we pass multiple commands too be executed in channel.?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: session.Channel(exec) command - PLease help

    The package I am talking about: com.jcraft.jsch
    Is there a web site for that package where you can get help about using it?
    That package is not part of the Java SE classes.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. how to send out multiple commands using channel(exec)
    By Asif Masood in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: April 12th, 2014, 01:34 PM
  2. Reading ant output when exec the command
    By cipm66 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: October 23rd, 2010, 01:48 PM
  3. Runtime.getRuntime().exec(command) - I get nothing
    By andersbk in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 7th, 2010, 08:47 PM
  4. opening Telnet Command Session
    By voyager in forum Java Networking
    Replies: 3
    Last Post: June 23rd, 2009, 10:34 AM