[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

running unix commands from java class

Posted on 2001-08-07
6
Medium Priority
?
312 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

649 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