Need example code snippet to open remote terminal and execute shell commands programmatically [message #1739730] |
Thu, 04 August 2016 08:00  |
Eclipse User |
|
|
|
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 #1739872 is a reply to message #1739736] |
Fri, 05 August 2016 04:22  |
Eclipse User |
|
|
|
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.
|
|
|
Powered by
FUDForum. Page generated in 0.03035 seconds