Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[dsdp-tm-dev] simple use case

Here's a simple case. I want to create an ssh connection to a host, run a command on the host, read the stdout/stderr from the command, then close the connection. The code below kind of works, but I'm not seeing any output from the command (I see the shell prompt though). Am I on the right track here?

Also, how do I know when the command finishes? What if the command is long running (maybe requiring user input before exiting)? Is there any way to force the command to terminate without closing the connection? Is there any notification of exit status?

Thanks,

Greg


ISystemRegistry sysReg = RSECorePlugin.getDefault().getSystemRegistry ();
	if (sysReg != null) {
IRSECoreRegistry coreReg = RSECorePlugin.getDefault ().getCoreRegistry();
		if (coreReg != null) {
IRSESystemType sshType = coreReg.getSystemTypeById ("org.eclipse.rse.systemtype.ssh");
			if (sshType != null) {
				IHost host = sysReg.createHost(sshType, "test", theHost, "my host");
				if (host != null) {
					ISubSystem[] subSystems = host.getSubSystems();
					ISubSystem subSystem = null;
					for(ISubSystem sub : subSystems) {
						if(sub instanceof IShellServiceSubSystem) {
							subSystem = sub;
							break;
						}
					}
		
					if (subSystem != null) {
						final ISubSystem sshSubSys = subSystem;
						IConnectorService conn = subSystem.getConnectorService();
						conn.setUserId(theUser);
						conn.setPassword(theUser, thePassword, false, false);
						conn.connect(new NullProgressMonitor());
								
						if (subSystem.isConnected()) {
IShellService shell = ((IShellServiceSubSystem) sshSubSys).getShellService();
							String env[] = new String[0];
HostShell hostShell = shell.runCommand("", "ls", env, new NullProgressMonitor()); hostShell.getStandardOutputReader().addOutputListener(new IHostShellOutputListener() {
								public void shellOutputChanged(IHostShellChangeEvent event) {
									IHostOutput[] input = event.getLines();
									try {
										for(int i = 0; i < input.length; i++) {
											System.out.write(input[i].getString().getBytes());
											System.out..write('\n');
											System.out..flush();
										}
									} catch(IOException e) {
										// Ignore
									}
								}
							});
hostShell.getStandardErrorReader().addOutputListener(new IHostShellOutputListener() {
								public void shellOutputChanged(IHostShellChangeEvent event) {
									IHostOutput[] input = event.getLines();
									try {
										for(int i = 0; i < input.length; i++) {
											System.err.write(input[i].getString().getBytes());
											System.err.write('\n');
											System.err.flush();
										}
									} catch(IOException e) {
										// Ignore
									}
								}
							});
							
							hostShell.exit();
						
							sshSubSys.disconnect();
						}
					}
				}
			}
		}
}

Back to the top