The following header lines retained to effect attribution:
|Date: Fri, 04 May 2001 11:25:48 +1200
|From: Hamish Kibblewhite <[EMAIL PROTECTED]>
|Subject: scp or sftp - information on how to wrap calls to them with Java
|To: [EMAIL PROTECTED]

|Hi,
|       this may not be the right list for this question and if so please
|redirect me...

|I have an idea of a little java project to wrapper scp and or sftp.  What I
|would like to know is where information on how to call scp +/or sftp from
|java might be.  Can anyone point me in the right direction regarding
|documenation?

|regards,
|Hamish Kibblewhite

Wrong news group.

You need to post in a Java language group.

Here is a sample using /bin/mail.  You may have troubles with ssh et al.
because of their use of /dev/tty.  I leave that for you to handle.

package Service;

import java.io.*;

public class Email {

  private String email = null;
  private Process process = null;
  private InputStream stdout = null;
  private InputStream stderr = null;
  private PrintWriter stdin = null;
  public String getEmailAddress() { return email; }
  public PrintWriter getEmailOut() { return stdin; }

  Email() {
    this.email = null;
    this.process = null;
    this.stdout = null;
    this.stderr = null;
    this.stdin = null;
  }

  Email(String email) {
    this.email = email; // do any desired address validation here.
    try { setup(); } catch (IOException ex) { teardown(); }
  }

  public void
  setup() throws IOException {
    process = null;
    stdin = null;
    stdout = null;
    stderr = null;
    if(email != null && email.length() != 0) {
      process = Runtime.getRuntime().exec("/bin/mail "+email);
      stdin = new PrintWriter(process.getOutputStream());
      stdout = process.getInputStream();
      stderr = process.getErrorStream();
      if(process == null ||
         stdin == null || stdout == null || stderr == null) {
        teardown();
      }
    }
  }

  public void
  teardown() {
    try {
      if(stdin != null) try { stdin.close(); } catch (Exception ex) { };
      if(stdout != null) try { stdout.close(); } catch (Exception ex) { };
      if(stderr != null) try { stderr.close(); } catch (Exception ex) { };
      if(process != null) {
        try { int status = process.waitFor(); } catch (Exception ex) { };
      }
    } finally {
      stdin = null;
      stdout = null;
      stderr = null;
      process = null;
    }
  }

  public void
  put(String field) { if(stdin != null) stdin.print(field); }

  public void
  putline(String text) {
    if(stdin != null) stdin.println(text);
  }

  public void
  putline() { if(stdin != null) stdin.println(); }
}

Randolph J. Herber, [EMAIL PROTECTED], +1 630 840 2966, CD/CDFTF PK-149F,
Mail Stop 318, Fermilab, Kirk & Pine Rds., PO Box 500, Batavia, IL 60510-0500,
USA.  (Speaking for myself and not for US, US DOE, FNAL nor URA.)  (Product,
trade, or service marks herein belong to their respective owners.)

Reply via email to