?
Solved

running unix commands from java class

Posted on 2001-08-07
6
Medium Priority
?
309 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 200 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 200 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month15 days, 3 hours left to enroll

770 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