Solved

running unix commands from java class

Posted on 2001-08-07
6
301 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Expert Comment

by:CleanupPing
Comment Utility
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
Comment Utility
IMHO, split btw  dnoelpp and me

CJ
0
 
LVL 20

Expert Comment

by:Venabili
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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 …
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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:

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now