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 5 of 5

Thread: how to send out multiple commands using channel(exec)

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

    Default how to send out multiple commands using channel(exec)

    Hi Folks,

    I am trying to send out multiple commands using the same channel and grab the out output stating whether the execution was succssful or not?
    But when I am trying to send out 2 commands in row, its not working.

    PLease find code below:
    package eclipsepackage;
     
    import java.io.IOException;
    import java.io.InputStream;
     
    import com.jcraft.jsch.ChannelExec;
    import com.jcraft.jsch.JSchException;
     
    //import IOExceptions;
     
    public class GetLastCommandStatus extends VerifyHostReachable
    {
    	 public int rVal = 0;
    	 public String cmd = "ls";
    	 public String cmd1 = "echo $?";
     
     
     
    	 public void cHKval() throws JSchException, IOException
    	 {
    	   ChannelExec channel3 = (ChannelExec) session.openChannel("exec");	
           channel3.setCommand(cmd ; cmd1); //cmd1 will only be run if the first command is successful
           channel3.setInputStream(null);
    	   InputStream in = channel3.getInputStream();
    	   channel3.connect();
           gLastCmdStatus(in,channel3); 
           channel3.disconnect();
           //channel3.setInputStream();
     
         }
     
     
    	private void gLastCmdStatus(InputStream in, ChannelExec channel3) throws IOException 
    	{
    		//byte[] b = new byte[10];
    		while (true)
    	    {
    	        while (in.available() > 0) 
    	        {
    	            System.out.println("Execution of last command was not successful;\n");
    	            byte[] tmp = new byte[1024];
    	            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);
    	            System.out.println(line); 
     
    	        }
    	        if (channel3.isClosed())
    	        {
    	            break;
    	        }
    	        try 
    	        {
    	            Thread.sleep(1000);
    	        } 
    	        catch (Exception ee)
    	        {
    	            //ignore
    	        }
     
     
    	    }
        }
    }

    Its giving error when I am passing 2 commands in row...
    cmd1 will let me know the result of first cmd executed.

    Please let me know. Thanks in advance !!!



    Thanks,
    Asif Masood


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

    Default How to pass multiple command using channel(exec) : Please solve my problem

    Hi Folks,

    I am trying to send out multiple commands using the same channel and grab the out output stating whether the execution was succssful or not?
    But when I am trying to send out 2 commands in row, its not working.

    PLease find code below:
    package eclipsepackage;
     
    import java.io.IOException;
    import java.io.InputStream;
     
    import com.jcraft.jsch.ChannelExec;
    import com.jcraft.jsch.JSchException;
     
    //import IOExceptions;
     
    public class GetLastCommandStatus extends VerifyHostReachable
    {
    	 public int rVal = 0;
    	 public String cmd = "ls";
    	 public String cmd1 = "echo $?";
     
     
     
    	 public void cHKval() throws JSchException, IOException
    	 {
    	   ChannelExec channel3 = (ChannelExec) session.openChannel("exec");	
           channel3.setCommand(cmd ; cmd1); //cmd1 will only be run if the first command is successful
           channel3.setInputStream(null);
    	   InputStream in = channel3.getInputStream();
    	   channel3.connect();
           gLastCmdStatus(in,channel3); 
           channel3.disconnect();
           //channel3.setInputStream();
     
         }
     
     
    	private void gLastCmdStatus(InputStream in, ChannelExec channel3) throws IOException 
    	{
    		//byte[] b = new byte[10];
    		while (true)
    	    {
    	        while (in.available() > 0) 
    	        {
    	            System.out.println("Execution of last command was not successful;\n");
    	            byte[] tmp = new byte[1024];
    	            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);
    	            System.out.println(line); 
     
    	        }
    	        if (channel3.isClosed())
    	        {
    	            break;
    	        }
    	        try 
    	        {
    	            Thread.sleep(1000);
    	        } 
    	        catch (Exception ee)
    	        {
    	            //ignore
    	        }
     
     
    	    }
        }
    }

    Its giving error when I am passing 2 commands in row...
    cmd1 will let me know the result of first cmd executed.

    Please let me know. Thanks in advance !!!



    Thanks,
    Asif Masood

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: how to send out multiple commands using channel(exec)

    Duplicate threads merged.

    Have you read the API doc for the classes you are using? They are not part of the Java SE classes and there might not be anyone familiar with them.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: how to send out multiple commands using channel(exec)

    hello Norm,

    I didn't have any clue/idea under whose section I should put this thread, hence i put this in two different sections which I thought is nearer to thread.
    I don't have API doc for the class. How to find out what you are mentioning about !!


    Thanks in advance !!!

    Thanks,
    Asif

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: how to send out multiple commands using channel(exec)

    How to find out
    Use an internet search engine.
    Ask the people where you got the code that uses those classes.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. executing-multiple-commands-using-j2ssh-sometimes-return-null
    By Priya Mohol in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 22nd, 2013, 02:04 PM
  2. [SOLVED] Problem Multiple Commands and filepath or classpath.
    By Andrew R in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 11th, 2013, 06:55 AM
  3. Multiple clients and one server. How to send data from one client to the others
    By u-will-neva-no in forum Java Theory & Questions
    Replies: 1
    Last Post: March 21st, 2012, 07:29 PM
  4. how to create a Broadcast Channel?
    By sinni in forum Java Networking
    Replies: 0
    Last Post: March 17th, 2011, 04:36 AM