Solved

running unix commands from java class

Posted on 2001-08-07
6
305 Views
Last Modified: 2012-05-05
Hi all,
i need to write a java class which connect a unix server from WIN2000 and to run some unix commands and scripts on that server.
what is the best way to do it?
note : i'm working with J++.
source code example will help.

niv
0
Comment
Question by:d_niv
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 19

Accepted Solution

by:
cheekycj earned 50 total points
ID: 6360391
you could connect to the Unix Server (socket programming) and then send commands for the Unix Server to execute.

The Unix server should have a server java app listening on a certain port and the client app should connect and communicate to that port.

The Java code that is running on the Unix server will need to execute the incomming commands using:
Runtime.getRuntime().exec("commandsent");
0
 
LVL 3

Assisted Solution

by:dnoelpp
dnoelpp earned 50 total points
ID: 6363544
Open a telnet or rsh connection and put the commands to the server.

Warning: This opens up a security hole to your Unix server. Imagine someone got hold of your program and used it to hack into your Unix server? So be very careful. Never use hardcoded user names and passwords.

Now to the details:

1. Create a socket with port 23 (the default port for telnet)
2. The first things you need to send to the socket are: username CR password CR
3. Now submit Unix commands and read the answer back.

Code:

I didn't test this code. This is supplied only to get you started and as a basis for you to improve it till it is what you wanted. Cheers!

// 1.
Socket socket = new Socket("unixserver.com", 23);

// 2.
PrintWriter out;
try {
    out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
} catch (IOException e) {
    System.out.println("opening output failed: " + e);
    System.exit(1);
}
out.println("youruser");
out.println("yourpasswd");

// read login message from system
BufferedReader in;
try {
    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e) {
    System.out.println("opening input failed: " + e);
    System.exit(1);
}

String line;
while (in.ready()) {
    line = in.readLine();
    if (line == null) {
        System.out.println("unexpected EOF");
        System.exit(1);
    }
    System.out.println("SERVER: " + line);
}

// 3.

out.println("ls");
while (in.ready()) {
    line = in.readLine();
    System.out.println("Output of ls command: " + line);
}
0
 
LVL 3

Expert Comment

by:dnoelpp
ID: 6363559
Additional comments:

1. It is insecure that the telnet password is sended in clear text through the net. Anybody can sniff out the password and misuse it.

2. I am not really sure whether ready() is used correctly here. The problem is: The data back from the server is a stream and is closed (EOF) when you log out. But sometimes we need to find out whether the server stopped "talking", for example, after a ls command the server sends the directory listing and the prompt and then waits for additional commands. Your program needs to detect when the server waits for additional commands. ready() could help here because it could tell whether the next readLine() blocks or not.

Be careful here because if the server stopped talking and you still are waiting for more data, then you have a deadlock, because the server doesn't get more commands from you, because you are waiting for more data from server, and so on.

Just try it out and if you have a deadlock, investigate thoroughly where.

Good luck!
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Expert Comment

by:CleanupPing
ID: 9061911
d_niv:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 9063160
IMHO, split btw  dnoelpp and me

CJ
0
 
LVL 20

Expert Comment

by:Venabili
ID: 9329738
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Split points between cheekycj and dnoelpp

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Venabili
EE Cleanup Volunteer
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question