Re: [JSch-users] pl. help - exec and threads.
Status: Alpha
Brought to you by:
ymnk
From: Satish A <sa...@my...> - 2005-01-24 23:13:38
|
Thanks Kevin. Your solution works well. I was wondering if there was any way to get the exit status of the remote command like openSSH does? Satish. On Sun, 2005-01-23 at 17:43 -0500, Kevin Kane wrote: > Where you have your slight delay, I'd suggest instead doing something like > > while (!channel.isEOF()) > { > // Wait for the remote process to complete. > Thread.sleep(500); > } > > Ideally, you should be able to register a listener with the Channel object which > would be notified of EOF events, but for right now you'll just have to poll. > > Hope this helps, > -Kevin > > ----- Original Message ----- > From: "Satish A" <sa...@my...> > To: <jsc...@li...> > Sent: Sunday, January 23, 2005 4:54 AM > Subject: [JSch-users] pl. help - exec and threads. > > > > Hello, > > > > I am new user to jsch. > > > > I am basically trying to do this: > > > > cat "hello world" | ssh -i <priv_key> user@remhost 'cat >> remfile' > > > > ie., append the output of a local program to a remote file over ssh. > > > > Basically, I am trying to do this in a method that starts a session, > > appends to remote file and ends the session. > > > > I figured that Exec.java example is the closest to this. With little > > mods, I have the code below. It works in that the remfile is appended > > to. But, the problem is that after channel.connect() > > is done executing, the program still runs because thread(s) are still > > alive. If I add session.disconnect() right after it, the file is not > > getting appended to. If I add a slight delay (in comments below), it > > works. > > > > So, I guess the actual write is happening in a different thread. How can > > I tell when the write is complete (ie., all the bytes from input > > stream are transferred to the remote file). > > > > Btw, I am using openssh-3.6.1p2-34 on the remote system. The local > > system is Fedora core 3. java 1.4.2-6. > > > > JSch jsch=new JSch(); > > jsch.addIdentity(privKeyFile.getAbsolutePath()); > > Session session=jsch.getSession(username, > > remHost.getHostAddress(), remPort); > > // establish ssh session > > UserInfo userInfo = new UserInfo() { > > > > public String getPassphrase() { > > return pass; > > } > > > > public String getPassword() { > > return pass; > > } > > > > public boolean promptPassword(String message) { > > System.out.println("passwd prompt: "+message); > > return false; > > } > > > > public boolean promptPassphrase(String message) { > > System.out.println("passphrase prompt: "+message); > > return false; > > } > > > > public boolean promptYesNo(String message) { > > return true; > > } > > > > public void showMessage(String message) { > > System.out.println(message); > > } > > }; > > > > session.setUserInfo(userInfo); > > session.connect(); > > > > // execute command on remote system > > String cmd = "cat >> "+remFile; > > Channel channel = session.openChannel("exec"); > > ((ChannelExec)channel).setCommand(cmd); > > InputStream bis = new ByteArrayInputStream(buf); > > channel.setInputStream(bis); > > channel.setOutputStream(System.out); > > ((ChannelExec)channel).setErrStream(System.err); > > channel.connect(); > > > > // try { > > // Thread.sleep(1000); > > // } catch (InterruptedException ex) { > > // ex.printStackTrace(); > > // } > > > > // disconnect session > > // session.disconnect(); > > > > Thanks, > > Satish. > > > > > > > > > > ------------------------------------------------------- > > This SF.Net email is sponsored by: IntelliVIEW -- Interactive Reporting > > Tool for open source databases. Create drag-&-drop reports. Save time > > by over 75%! Publish reports on the web. Export to DOC, XLS, RTF, etc. > > Download a FREE copy at http://www.intelliview.com/go/osdn_nl > > _______________________________________________ > > JSch-users mailing list > > JSc...@li... > > https://lists.sourceforge.net/lists/listinfo/jsch-users > > > > > > > > > > _________________________________________________ > Scanned on 23 Jan 2005 22:43:46 > -- Satish Annapureddy Myrio Corporation sa...@my... tel 425-368-4472 fax 425-368-4500 |