Skip to main content



      Home
Home » Eclipse Projects » Target Management » Need example code snippet to open remote terminal and execute shell commands programmatically(Need an example code to open remote terminal and execute shell commands programmatically)
Need example code snippet to open remote terminal and execute shell commands programmatically [message #1739730] Thu, 04 August 2016 08:00 Go to next message
Eclipse UserFriend
I am using the below code to open a remote terminal and execute a shell command in it.
            TerminalsUI terminalsUI = TerminalsUI.getInstance();
            TerminalViewer viewer = terminalsUI.activateTerminalsView();
            CTabItem tab = viewer.getTabFolder().createTabItem(TerminalServiceHelper.getTerminalSubSystem(conn).getHost(), "");
            TerminalElement element = TerminalServiceHelper.createTerminalElement(tab,
                    TerminalServiceHelper.getTerminalSubSystem(conn));
            TerminalServiceHelper.getTerminalSubSystem(conn).addChild(element);
            ITerminalShell lol = element.getTerminalShell();
            TerminalShellService fdf = (TerminalShellService)RSEHelper.getConnectedRemoteShellService(conn, null);
            SshTerminalService shelll = (SshTerminalService)fdf.getAdapter(ITerminalService.class);
            element.setTerminalShell((ITerminalShell)shelll);
            shelll.launchTerminal(null, null, new String[] {}, "", "ls -l", null);


This is opening the remote terminal in "Terminals" view with the content:

Last login: Wed Aug 17 11:14:33 2016 from 10.X.X.X
root@moron:~#


Also command ls -l was not executed and shown in the newly opened terminal may be because of the exception:
java.lang.ClassCastException: org.eclipse.rse.internal.services.ssh.terminal.SshTerminalService cannot be cast to org.eclipse.rse.services.terminals.ITerminalShell

at the line: element.setTerminalShell((ITerminalShell)shelll);

Question:
The above code snippet gives warning as "Discouraged access" at many places.

Please can any one post the recommended code snippet that can be used to open a remote terminal programmatically and execute commands in it programmatically?

[Updated on: Thu, 04 August 2016 08:01] by Moderator

Re: Need example code snippet to open remote terminal and execute shell commands programmatically [message #1739736 is a reply to message #1739730] Thu, 04 August 2016 08:42 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

are you sure you want to do this inside the RSE Framework (RSE Terminals View) ?

I would personally recommend using the newer Terminal 4.x view instead, which has direct SSH access with a lot less bulk around it. A code snippet for programmatically open a tab in that view is referenced here:

https://marketplace.eclipse.org/comment/3982#comment-3982

and here:

https://dev.eclipse.org/mhonarc/lists/tm-dev/msg01128.html

and here:

https://dev.eclipse.org/mhonarc/lists/tm-dev/msg01044.html

Cheers,
Martin
Re: Need example code snippet to open remote terminal and execute shell commands programmatically [message #1739872 is a reply to message #1739736] Fri, 05 August 2016 04:22 Go to previous message
Eclipse UserFriend
Yes. Is it not possible to do it on older RSE/TM. I am using org.eclipse.rse.terminals.ui (1.3.0.201504210807).

I am able to execute commands in remote shell with the below code:

	IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
            IStructuredSelection sel = (IStructuredSelection)window.getSelectionService().getSelection();
            RemoteProcessImpl ele = (RemoteProcessImpl)sel.getFirstElement();
            IHost conn = ele.getSystemConnection();
            IRemoteCmdSubSystem cmdSS = RemoteCommandHelpers.getCmdSubSystem(conn);
            Object[] obs = null;
            RemoteOutput fe = null;
            try {
                cmdSS.checkIsConnected(null);
            } catch (SystemMessageException e2) {
                e2.printStackTrace();
            }
            try {
                cmdSS.initializeSubSystem(null);
            } catch (SystemMessageException e1) {
                e1.printStackTrace();
            }
            RemoteCommandShell shell = null;
            try {
                shell = (RemoteCommandShell)cmdSS.getDefaultShell();
            } catch (Exception e) {
                e.printStackTrace();
            }
            shell.removeOutput();
            cmdSS.sendCommandToShell("ls -l", shell, null);
            Thread.sleep(1000);
            obs = new Object[shell.getRoots().length];
            obs = shell.getRoots();
            for (int i = 0; i < obs.length; i++) {
                fe = (RemoteOutput)obs[i];
                String op1 = fe.getText();
                System.out.println(op1);
            }
            command.setLength(0);

I want to do the same thing in remote terminal.
Previous Topic:using Remote Terminal in my plugin
Next Topic:Add connections from code
Goto Forum:
  


Current Time: Tue May 06 23:34:39 EDT 2025

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

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

Back to the top