[JSch-users] ChannelExec unable to execute top command
Status: Alpha
Brought to you by:
ymnk
From: Amol K. <am...@gm...> - 2008-09-24 08:28:19
|
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 |