Solved

running unix commands from java class

Posted on 2001-08-07
6
303 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
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 82
topping2 challenge 13 90
VB Script to add site to Java Exception List 4 76
java stored proc example 9 29
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

776 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