Natasha Gfell wrote:
>
> Is there a way to run a Linux shell script from a java program if they
> are on different machines? I'm trying to write a Java program that will
> run on a Windows machine, connect to a Linux machine, and run a Linux
> shell script. Is that possible? Can anyone provide an example of how
> you would write such a program? I wrote a program that supposedly
> connects to the Linux machine from my Windows, but it does not run the
> shell script. Can anyone see what is wrong? I get all the messages
> indicating that it ran but the output from hello.sh is not being
> generated. Thanks in advance.
Comments interspersed:
>
> import java.io.*;
> import java.net.*;
>
> class Client
> {
> public static void main(String[] arg)
> {
> Socket s = null; // set up socket
> BufferedReader is = null; // input stream
> PrintWriter os=null; // output stream
>
> try
> {
> System.out.println( "Settting up Socket" );
> s = new Socket("10.1.1.200",23,true);
Okay, you're talking to the machine's telnet port. You need
to model your program's interactions from what you type
yourself when using the windows 'telnet' command. If you
don't know telnet, visit your linux box and type
'man telnet' and read it over.
>
> System.out.println( "Setting up Input Stream" );
> is = new BufferedReader(new InputStreamReader(
> s.getInputStream()));
>
> System.out.println( "Setting up Output Stream" );
> os = new PrintWriter(s.getOutputStream(), true);
> }
> catch(UnknownHostException uhe )
> {
> System.out.println( "I don't know the host" );
> }
> catch (Exception e)
> {
> System.out.println(e);
> }
The logic here could be better. If your socket open
fails, there's no point having the rest of the code
tossing commands into the dead socket, right?
>
> BufferedReader stdIn=new BufferedReader(new
> InputStreamReader(System.in));
> String userInput;
>
> if( s != null && is != null && os != null )
> {
> try
> {
> System.out.println( "Sending command" );
> String command = ("/home/hello.sh");
Are you sure this script is in /home and not in
/home/nmgfell?
I'd expect this to be something like /home/nmgfell/hello.sh.
> os.println(command);
> System.out.println( "Closing Output Stream" );
> os.close();
> System.out.println( "Closing Input Stream" );
> is.close();
Okay, you're sending the command. Where are you reading
the results? It looks like you're just closing the
pipes without reading anything back from hello.sh.
Of course, I've already mentioned that you need to make
this look like a telnet session. So you need to send
and read the prompts telnet sends you. Here's what
the input and output looks like when I telnet to my own
machine and launch a (non-existant) shell script.
[joi@joi joi]$ telnet localhost 23
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Red Hat Linux release 6.2 (Zoot)
Kernel 2.2.14-5.0 on an i686
login: joi
Password:
Last login: Mon Oct 9 11:02:08 from locahost
You have mail.
alias ls='ls --color=tty'
[joi@joi joi]$ /home/joi/home.sh
bash: /home/joi/home.sh: No such file or directory
[joi@joi joi]$ exit
logout
Connection closed by foreign host.
joi@joi joi]$ exit
--
Joi Ellis Software Engineer
Aravox Technologies [EMAIL PROTECTED], [EMAIL PROTECTED]
No matter what we think of Linux versus FreeBSD, etc., the one thing I
really like about Linux is that it has Microsoft worried. Anything
that kicks a monopoly in the pants has got to be good for something.
- Chris Johnson
----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]