[ale] Crazy Perl + Java question

Christopher Fowler cfowler at outpostsentinel.com
Sun May 23 15:45:10 EDT 2004


Here is java imp of that system

--- Cut Here ----
[tomcat at SAM-devel SAM]$ cat jcd.java 

public class jcd {
        public static void main(String args[]) {
                JCD j = new JCD("192.168.2.231", "ensign", "password");
                try {
                        System.out.print(j.get_buffer("Solaris"));
                } catch (Exception exp) {
                        System.err.println(exp.getMessage());
                }
        }
}
--- Cut Here ---
[tomcat at SAM-devel SAM]$ cat jcd.java 

public class jcd {
        public static void main(String args[]) {
                JCD j = new JCD("192.168.2.231", "ensign", "password");
                try {
                        System.out.print(j.get_buffer("Solaris"));
                } catch (Exception exp) {
                        System.err.println(exp.getMessage());
                }
        }
}
[tomcat at SAM-devel SAM]$ cat JCD.java 
import java.io.*;

public class JCD {
        private String _HOST;
        private String _USER;
        private String _PASS;
        private static final String _CMD = "/opt/SAM/jcd.pl";

        public JCD(String v1, String v2, String v3) {
                _HOST = v1;
                _USER = v2;
                _PASS = v3;
        }

        public String get_buffer(String console) 
                throws Exception {
                StringBuffer BUFFER = new StringBuffer("");
                try {

                        Process p = null;
                        String r[] = { 
                                _CMD, 
                                "-u" + _USER,
                                "-s" + _HOST,
                                "-p" + _PASS,
                                "-Se", 
                                "get_buffer(" + console + ")"
                        };

                        p  = Runtime.getRuntime().exec(r);


                        BufferedInputStream buffer =
                                new
BufferedInputStream(p.getInputStream());
                        BufferedReader br = new BufferedReader(new
InputStreamReader(buffer));
                        String s = null;
                        try {
                                while ((s = br.readLine()) != null) {
                                        BUFFER.append(s);
                                        BUFFER.append("\n");
                                }
                        br.close();
                        p.waitFor();

                        if(p.exitValue() != 0) {
                                /**
                                 * Is there anyway I can parse STDERR?
                                 */
                                throw new Exception("Error: execution
error when trying to get console data.");
                        }

                        } catch (Exception e) { /** Read errors are
normal */}
                } catch (Exception e) {
                        throw new Exception("ERROR!:"+ e.getMessage());
                }
                return BUFFER.toString();
        }

}

/** vi: set ts=2 sw=2: */
-- Cut Here ---

The speed is not bad either.

On Sun, 2004-05-23 at 15:27, Christopher Fowler wrote:
> Here is what I came up with.
> 
> I created an interface that used Getopt to allow the user to pass all
> arguments needed to create a JD object.  I then added a -e (exec)
> options that allowed the user to pass the method and arguments for that
> class that needed to be executed.
> 
> In this example I called up my remote JCD server and asked it for the
> console buffer contents of my Soalris 9 machine.
> 
> ./jcd.pl -u ensign -p password -s 192.168.2.231 -S -e
> "get_buffer('Solaris')"
> 
> What I got back was:
> 
> 
> sol9 console login: root
> Password: 
> May 21 14:28:38 sol9 login: ROOT LOGIN /dev/console
> Last login: Thu May 20 23:53:11 on console
> Sun Microsystems Inc.   SunOS 5.9       Generic May 2002
> # passwd
> passwd: Changing password for root
> New Password: 
> Re-enter new Password: 
> passwd(SYSTEM): They don't match.
> 
> Please try again
> New Password: 
> Re-enter new Password: 
> passwd: password successfully changed for root
> # ^D
> sol9 console login: 
> sol9 console login: 
> sol9 console login: 
> 
> 
> The beauty of this is that the '-S' switched used SSL as the connection
> and I did not have to waste my times with the problems of java not
> allowing a connection to a remote site that did not have a signed cert.
> 
> Unless there is a way to call perl code directly from Java I think this
> would be the best way.  I guess I could write a JNI interface that can
> return Java objects but that might be a royal PITA.
> 
> 
> 
> On Sun, 2004-05-23 at 15:00, Christopher Fowler wrote:
> > I was on my way to Home Depot to by some shelving for my ever growing
> > collection of technical manuals.  I purchased about 10 books in the last
> > 2 weeks.  I'm running out of space in my home office for all this
> > storage.  Back on subject.... I was thinking about some classes that I
> > need to write in Java to use in our J2EE app that I've written in Perl. 
> > I would like to know if there is any way to reuse that perl code so I do
> > not have to write more in Java?
> > 
> > All of the data that is returned are Strings.  I could create a simple
> > command line interface to the perl class and have Java execute it in a
> > process thread.  Is that a crazy idea?  Is there a better more efficient
> > way?
> > 
> > Chris
> > 
> > _______________________________________________
> > Ale mailing list
> > Ale at ale.org
> > http://www.ale.org/mailman/listinfo/ale
> 
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://www.ale.org/mailman/listinfo/ale



More information about the Ale mailing list