I'd think that this is expected behavior.

"top" needs a Terminal emulation in order to move the
cursor around etc -- but ChannelExec just provides a
raw input and output stream without terminal emulation.

You'll need ChannelShell if you want to run top. And,
a Terminal emulation connected to your Streams such 
as PuTTY or the Terminal component from our Eclipse 
Open Source project: http://www.eclipse.org/dsdp/tm/

Cheers,
--
Martin Oberhuber, Senior Member of Technical Staff, Wind River
Target Management Project Lead, DSDP PMC Member
http://www.eclipse.org/dsdp/tm
 
 

> -----Original Message-----
> From: Amol Kulkarni [mailto:[EMAIL PROTECTED] 
> Sent: Wednesday, September 24, 2008 10:28 AM
> To: [email protected]
> Subject: [JSch-users] ChannelExec unable to execute top command
> 
> Hi,
> 
> I want to execute system command using ChannelExec on remote Linux
> machine. I am not able to execute "top" command. I am able to execute
> "top -b" (batch mode) command properly. I want to use ChannelExec to
> avoid getting garbage characters that I receive when I use
> ChannelShell.
> On the error stream, it writes the following:
> 
> TERM environment variable not set.
> 
> Here is the sample application:
> 
> package com.appperfect.common.main;
> 
> import java.io.BufferedReader;
> import java.io.InputStreamReader;
> import java.io.OutputStream;
> 
> import com.jcraft.jsch.Channel;
> import com.jcraft.jsch.ChannelExec;
> import com.jcraft.jsch.ChannelShell;
> import com.jcraft.jsch.JSch;
> import com.jcraft.jsch.Logger;
> import com.jcraft.jsch.Session;
> import com.jcraft.jsch.UserInfo;
> 
> public class SSHSample
> {
>       private static class MyUserInfo implements UserInfo
>       {
>               private String passwd;
> 
>               MyUserInfo(String passwd)
>               {
>                       this.passwd = passwd;
>               }
> 
>               public String getPassword()
>               {
>                       return passwd;
>               }
> 
>               public boolean promptYesNo(String str)
>               {
>                       return true;
>               }
> 
>               public String getPassphrase()
>               {
>                       return null;
>               }
> 
>               public boolean promptPassphrase(String message)
>               {
>                       return true;
>               }
> 
>               public boolean promptPassword(String message)
>               {
>                       return true;
>               }
> 
>               public void showMessage(String message)
>               {
>               }
>       }
>       
>       
>       
>       public static void main(String[] args)
>       {
>               if(args.length != 4)
>               {
>                       System.out.println("Usage : SSHSample 
> hostName UserName Password Type");
>                       System.exit(0);
>               }
>               
>               final String hostName = args[0];
>               final String userName = args[1];
>               final String password = args[2];
>               final String channelTye = args[3];
>               
>               JSch.setLogger(new Logger()
>               {
> 
>                       public boolean isEnabled(int level) 
> {return true;}
> 
>                       public void log(int level, String message) {
>                               System.out.println(message);
>                       }
>               });
>               
>               final String command = "top";
>               final JSch jsch = new JSch();
>               Session session = null;
>               Channel channel = null;
>               boolean exec = channelTye.equals("exec");
>               try
>               {
>                       session = jsch.getSession(userName, 
> hostName, 22);
>                       session.setUserInfo(new MyUserInfo(password));
>                       session.setDaemonThread(false);
>                       session.connect();
>                       channel = session.openChannel(channelTye);
>                       OutputStream os = channel.getOutputStream();
>                       BufferedReader lineReader = new 
> BufferedReader(new
> InputStreamReader(channel.getInputStream()));
>                       if (exec)
>                       {
>                               
> ((ChannelExec)channel).setErrStream(System.err);
>                               
> ((ChannelExec)channel).setCommand(command);
>                       }
>                       channel.connect();
>                       if (!exec)
>                       {
>                               
> os.write("PS1=\"MY_PROMPT>\"".getBytes());
>                               os.write("\n".getBytes());
>                               os.write("TERM=ansi".getBytes());
>                               os.write("\n".getBytes());
>                               os.write(command.getBytes());
>                               os.write("\n".getBytes());
>                               os.flush();
>                       }
>                       Thread.sleep(2000);
>                       while (lineReader.ready())
>                       {
>                               
> System.out.println(lineReader.readLine());
>                               if (!lineReader.ready())
>                               {
>                                       Thread.sleep(2000);
>                               }
>                       }
>               if(channel.isClosed())
>               {
>                   System.out.println("exit-status: " + 
> channel.getExitStatus());
>               }
>                       channel.disconnect();
>               }
>               catch (Exception e)
>               {
>                       e.printStackTrace(System.out);
>               }
>               finally
>               {
>                       if (session != null)
>                       {
>                               session.disconnect();
>                       }
>               }
>               
>               
>       }
> }
> 
> 
> Note, when we run just a top command it is not started in "batch"
> mode. Any idea how handle this case. If ChannelShell is the only way,
> then how to get rid of garbage (formatting) characters.
> 
> 
> Thanks & Regards,
> Amol
> 
> --------------------------------------------------------------
> -----------
> This SF.Net email is sponsored by the Moblin Your Move 
> Developer's challenge
> Build the coolest Linux based applications with Moblin SDK & 
> win great prizes
> Grand prize is a trip for two to an Open Source event 
> anywhere in the world
> http://moblin-contest.org/redirect.php?banner_id=100&url=/
> _______________________________________________
> JSch-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/jsch-users
> 

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
JSch-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jsch-users

Reply via email to