Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » DSDP - Target Management » How can I get only the output of remote commands ?
How can I get only the output of remote commands ? [message #16570] Thu, 06 December 2007 03:01 Go to next message
supertat is currently offline supertatFriend
Messages: 3
Registered: July 2009
Junior Member
Hello.
I would like to use RSE API to execute remote commands and get their
result strings.
However, as far as I tried, I cannot find any way to get only the result
of the commands, that is, not including remote host's login greeting
message nor shell prompt.
I appreciate if you could tell me how to do that.

---
Here is what I tried:

(for given IHost)
IRemoteCmdSubSystem cmdSS = RemoteCommandHelpers.getCmdSubSystem(host);
String cmdStr = "ls -ltr";

(option 1) using IShellService#launchShell(), IShellService#runCommand()
------------------------------------------------------------ ---------------IShellService
shellService = ((IShellServiceSubSystem) cmdSS).getShellService();
String[] environment = new String[1];
environment[0] = "AAA=BBB";
String workDir = ".";
IHostShell hostShell = shellService.runCommand(initialWorkingDirectory,
cmdStr, environment, new NullProgressMonitor());
hostShell.addOutputListener(new StdOutOutputListener());
IHostShellOutputReader reader = hostShell.getStandardOutputReader();
IHostOutput hostOutput = reader.readLine();
while( hostOutput !=null) {
System.out.println(hostOutput.getString());
hostOutput = reader.readLine();
}

public class StdOutOutputListener implements IHostShellOutputListener {
public void shellOutputChanged(IHostShellChangeEvent event) {
}
}
------------------------------------------------------------ ---------------

(option 2) using IRemoteCmdSubSystem#runCommand()
------------------------------------------------------------ ---------------Object[]
result = cmdss.runCommand(cmdStr, null, false, new NullProgressMonitor());
if (result.length > 0 && result[0] instanceof IRemoteCommandShell) {
IRemoteCommandShell cs = (IRemoteCommandShell) result[0];
while (cs.isActive()) {
Thread.sleep(1000);
}

Object[] output = cs.listOutput();

for (int i = 0; i < output.length; i++) {
if (output[i] instanceof IRemoteOutput) {
System.out.println(((IRemoteOutput) output[i]).getText());
}
}
cmdSS.removeShell(cs);
}
------------------------------------------------------------ ---------------

(option 3) using SimpleCommandOperation#runCommand()
------------------------------------------------------------ ---------------SimpleCommandOperation
operation = new SimpleCommandOperation(cmdSS, workDir, true);
operation.runCommand(cmdStr, true);
String line = operation.readLine(true);
while (line != null) {
System.out.println(line);
line = operation.readLine(true);
}
------------------------------------------------------------ ---------------


For all the options above, the output strings are like this
------------------------------------------------------------ ---------------Last
login: Thu Dec 6 12:19:17 2007 from XXX.XX.XX.XXX
ls -ltr

echo $PWD'>'

[aaa@ms ~]$ ls -ltr
total 92
drwxr-xr-x 12 onishi sim 4096 Apr 20 2007 aa.txt
drwxr-xr-x 2 onishi sim 4096 Jun 12 16:14 bb.txt
[aaa@ms ~]$
[aaa@ms ~]$ echo $PWD'>'
/home/aaa>
------------------------------------------------------------ ---------------

What I would like to get is only this part of the above.
------------------------------------------------------------ ---------------total
92
drwxr-xr-x 12 onishi sim 4096 Apr 20 2007 aa.txt
drwxr-xr-x 2 onishi sim 4096 Jun 12 16:14 bb.txt
------------------------------------------------------------ ---------------

Thanks in advance.
Re: How can I get only the output of remote commands ? [message #16743 is a reply to message #16570] Tue, 11 December 2007 19:44 Go to previous message
David McKnight is currently offline David McKnightFriend
Messages: 244
Registered: July 2009
Senior Member
Hi,

Here's something that you may want to try:

Extend RemoteCommandShellOperation with something like this:

public class TestRemoteCommandShellOperation extends
RemoteCommandShellOperation {

private boolean _commandOutputStarted = false;

public TestRemoteCommandShellOperation(Shell shell, IRemoteCmdSubSystem
cmdSubSystem, IRemoteFile pwd)
{
super(shell, cmdSubSystem, pwd);
}

public void handleOutputChanged(String command, Object output) {

if (output instanceof RemoteOutput)
{
String text = ((RemoteOutput)output).getText();

if (!_commandOutputStarted)
{
_commandOutputStarted = text.indexOf("BEGIN-END-TAG:") > 0;
}
else
{
processOutput(text);
}
}
}

private void processOutput(String text)
{
System.out.println("line = " + text);
}


public void handleCommandFinished(String cmd) {
}
}

The check for BEGIN-END-TAG can be used to determine when a command begins
and ends.

You can then use this class like this:

TestRemoteCommandShellOperation op = new
TestRemoteCommandShellOperation(getShell(), getRemoteCmdSubSystem(),
selectedFile);
op.run(); // starts the shell
op.sendCommand("ls -l"); // runs the command





"supertat" <hoimi0122@yahoo.co.jp> wrote in message
news:eb56541a54d69d92d62fcba33a1269fb$1@www.eclipse.org...
> Hello.
> I would like to use RSE API to execute remote commands and get their
> result strings.
> However, as far as I tried, I cannot find any way to get only the result
> of the commands, that is, not including remote host's login greeting
> message nor shell prompt.
> I appreciate if you could tell me how to do that.
>
> ---
> Here is what I tried:
>
> (for given IHost)
> IRemoteCmdSubSystem cmdSS = RemoteCommandHelpers.getCmdSubSystem(host);
> String cmdStr = "ls -ltr";
>
> (option 1) using IShellService#launchShell(), IShellService#runCommand()
> ------------------------------------------------------------ ---------------IShellService
> shellService = ((IShellServiceSubSystem) cmdSS).getShellService();
> String[] environment = new String[1];
> environment[0] = "AAA=BBB";
> String workDir = ".";
> IHostShell hostShell = shellService.runCommand(initialWorkingDirectory,
> cmdStr, environment, new NullProgressMonitor());
> hostShell.addOutputListener(new StdOutOutputListener());
> IHostShellOutputReader reader = hostShell.getStandardOutputReader();
> IHostOutput hostOutput = reader.readLine();
> while( hostOutput !=null) {
> System.out.println(hostOutput.getString());
> hostOutput = reader.readLine();
> }
>
> public class StdOutOutputListener implements IHostShellOutputListener {
> public void shellOutputChanged(IHostShellChangeEvent event) {
> }
> }
> ------------------------------------------------------------ ---------------
>
> (option 2) using IRemoteCmdSubSystem#runCommand()
> ------------------------------------------------------------ ---------------Object[]
> result = cmdss.runCommand(cmdStr, null, false, new NullProgressMonitor());
> if (result.length > 0 && result[0] instanceof IRemoteCommandShell) {
> IRemoteCommandShell cs = (IRemoteCommandShell) result[0];
> while (cs.isActive()) {
> Thread.sleep(1000);
> }
> Object[] output = cs.listOutput(); for (int i = 0; i
> < output.length; i++) {
> if (output[i] instanceof IRemoteOutput) {
> System.out.println(((IRemoteOutput) output[i]).getText());
> }
> }
> cmdSS.removeShell(cs);
> }
> ------------------------------------------------------------ ---------------
>
> (option 3) using SimpleCommandOperation#runCommand()
> ------------------------------------------------------------ ---------------SimpleCommandOperation
> operation = new SimpleCommandOperation(cmdSS, workDir, true);
> operation.runCommand(cmdStr, true);
> String line = operation.readLine(true);
> while (line != null) {
> System.out.println(line);
> line = operation.readLine(true);
> }
> ------------------------------------------------------------ ---------------
>
>
> For all the options above, the output strings are like this
> ------------------------------------------------------------ ---------------Last
> login: Thu Dec 6 12:19:17 2007 from XXX.XX.XX.XXX
> ls -ltr
>
> echo $PWD'>'
>
> [aaa@ms ~]$ ls -ltr
> total 92
> drwxr-xr-x 12 onishi sim 4096 Apr 20 2007 aa.txt
> drwxr-xr-x 2 onishi sim 4096 Jun 12 16:14 bb.txt
> [aaa@ms ~]$ [aaa@ms ~]$ echo $PWD'>'
> /home/aaa>
> ------------------------------------------------------------ ---------------
>
> What I would like to get is only this part of the above.
> ------------------------------------------------------------ ---------------total
> 92
> drwxr-xr-x 12 onishi sim 4096 Apr 20 2007 aa.txt
> drwxr-xr-x 2 onishi sim 4096 Jun 12 16:14 bb.txt
> ------------------------------------------------------------ ---------------
>
> Thanks in advance.
>
Re: How can I get only the output of remote commands ? [message #571338 is a reply to message #16570] Tue, 11 December 2007 19:44 Go to previous message
David McKnight is currently offline David McKnightFriend
Messages: 244
Registered: July 2009
Senior Member
Hi,

Here's something that you may want to try:

Extend RemoteCommandShellOperation with something like this:

public class TestRemoteCommandShellOperation extends
RemoteCommandShellOperation {

private boolean _commandOutputStarted = false;

public TestRemoteCommandShellOperation(Shell shell, IRemoteCmdSubSystem
cmdSubSystem, IRemoteFile pwd)
{
super(shell, cmdSubSystem, pwd);
}

public void handleOutputChanged(String command, Object output) {

if (output instanceof RemoteOutput)
{
String text = ((RemoteOutput)output).getText();

if (!_commandOutputStarted)
{
_commandOutputStarted = text.indexOf("BEGIN-END-TAG:") > 0;
}
else
{
processOutput(text);
}
}
}

private void processOutput(String text)
{
System.out.println("line = " + text);
}


public void handleCommandFinished(String cmd) {
}
}

The check for BEGIN-END-TAG can be used to determine when a command begins
and ends.

You can then use this class like this:

TestRemoteCommandShellOperation op = new
TestRemoteCommandShellOperation(getShell(), getRemoteCmdSubSystem(),
selectedFile);
op.run(); // starts the shell
op.sendCommand("ls -l"); // runs the command





"supertat" <hoimi0122@yahoo.co.jp> wrote in message
news:eb56541a54d69d92d62fcba33a1269fb$1@www.eclipse.org...
> Hello.
> I would like to use RSE API to execute remote commands and get their
> result strings.
> However, as far as I tried, I cannot find any way to get only the result
> of the commands, that is, not including remote host's login greeting
> message nor shell prompt.
> I appreciate if you could tell me how to do that.
>
> ---
> Here is what I tried:
>
> (for given IHost)
> IRemoteCmdSubSystem cmdSS = RemoteCommandHelpers.getCmdSubSystem(host);
> String cmdStr = "ls -ltr";
>
> (option 1) using IShellService#launchShell(), IShellService#runCommand()
> ------------------------------------------------------------ ---------------IShellService
> shellService = ((IShellServiceSubSystem) cmdSS).getShellService();
> String[] environment = new String[1];
> environment[0] = "AAA=BBB";
> String workDir = ".";
> IHostShell hostShell = shellService.runCommand(initialWorkingDirectory,
> cmdStr, environment, new NullProgressMonitor());
> hostShell.addOutputListener(new StdOutOutputListener());
> IHostShellOutputReader reader = hostShell.getStandardOutputReader();
> IHostOutput hostOutput = reader.readLine();
> while( hostOutput !=null) {
> System.out.println(hostOutput.getString());
> hostOutput = reader.readLine();
> }
>
> public class StdOutOutputListener implements IHostShellOutputListener {
> public void shellOutputChanged(IHostShellChangeEvent event) {
> }
> }
> ------------------------------------------------------------ ---------------
>
> (option 2) using IRemoteCmdSubSystem#runCommand()
> ------------------------------------------------------------ ---------------Object[]
> result = cmdss.runCommand(cmdStr, null, false, new NullProgressMonitor());
> if (result.length > 0 && result[0] instanceof IRemoteCommandShell) {
> IRemoteCommandShell cs = (IRemoteCommandShell) result[0];
> while (cs.isActive()) {
> Thread.sleep(1000);
> }
> Object[] output = cs.listOutput(); for (int i = 0; i
> < output.length; i++) {
> if (output[i] instanceof IRemoteOutput) {
> System.out.println(((IRemoteOutput) output[i]).getText());
> }
> }
> cmdSS.removeShell(cs);
> }
> ------------------------------------------------------------ ---------------
>
> (option 3) using SimpleCommandOperation#runCommand()
> ------------------------------------------------------------ ---------------SimpleCommandOperation
> operation = new SimpleCommandOperation(cmdSS, workDir, true);
> operation.runCommand(cmdStr, true);
> String line = operation.readLine(true);
> while (line != null) {
> System.out.println(line);
> line = operation.readLine(true);
> }
> ------------------------------------------------------------ ---------------
>
>
> For all the options above, the output strings are like this
> ------------------------------------------------------------ ---------------Last
> login: Thu Dec 6 12:19:17 2007 from XXX.XX.XX.XXX
> ls -ltr
>
> echo $PWD'>'
>
> [aaa@ms ~]$ ls -ltr
> total 92
> drwxr-xr-x 12 onishi sim 4096 Apr 20 2007 aa.txt
> drwxr-xr-x 2 onishi sim 4096 Jun 12 16:14 bb.txt
> [aaa@ms ~]$ [aaa@ms ~]$ echo $PWD'>'
> /home/aaa>
> ------------------------------------------------------------ ---------------
>
> What I would like to get is only this part of the above.
> ------------------------------------------------------------ ---------------total
> 92
> drwxr-xr-x 12 onishi sim 4096 Apr 20 2007 aa.txt
> drwxr-xr-x 2 onishi sim 4096 Jun 12 16:14 bb.txt
> ------------------------------------------------------------ ---------------
>
> Thanks in advance.
>
Previous Topic:[RSE] suggestions : bookmark or ReConnect
Next Topic:Easy Install Question
Goto Forum:
  


Current Time: Thu Dec 18 09:41:22 GMT 2014

Powered by FUDForum. Page generated in 0.03994 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software