aspose file tools*
The moose likes Java in General and the fly likes exec() method is limited in function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "exec() method is limited in function" Watch "exec() method is limited in function" New topic
Author

exec() method is limited in function

RUI CHEN
Greenhorn

Joined: Nov 16, 2002
Posts: 12
Hi guys,
It is too bad to find that the exec() method in class Porcess can not do a lot of things.
For example, if the program is currently running in directory "A", and I want to enter directory "B" and list the files in "B". The "cd" command the program will executed first won't affect the later command "ls". The "ls" will still list the files in directory "A".
Is there any way to do what I want?
Thanks very much!
Yours
Rui


Life is like a .jar, so many many things contained.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi RUI,
What exactly are you trying to do? If you are creating a shell, then to change directory all you should have to do is open the process' output stream and write something like "cd /usr/bin" and the shell should respond. You should then be able to write "ls -l" to the process' stream and then read its input stream which should cough up a long listing of the files in /usr/bin.
Michael Morris


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
RUI CHEN
Greenhorn

Joined: Nov 16, 2002
Posts: 12
Hi Michael,
It is still the old question I posted a week ago. The Server receive request, and carry our in Unix, then foward the result to client.
And I want the user at the Client could operate on the remote Unix freely. Like, cd any directory and list files.
I don't exactly know what you mean by write something like "cd /usr/bin".
Anyway, is it possible that the program continues to run correctly after it changes directory many times?
Thanks very much!
Yours
Rui
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi RUI,

I don't exactly know what you mean by write something like "cd /usr/bin".

Doesn't that change directory to /usr/bin on all flavors of Unix?
I'm assuming that you are doing something like:

on your server. The thing you have to do is connect the client socket streams to the process' streams. Before you even attempt to write any client code, you should make sure that your server is working properly. You can do that with telnet. For example, if the server is running on 192.168.0.101 on port 2000, then you should have no problem connecting to it with:

You probably won't be able to see what you type on the telnet client until you hit a return. But if the streams are properly connected then the output should come back along with what you typed.
You should start a thread on the server for each of the three streams from the process: stdin, stdout and stderr.
Here is a class that I've been using to do the same thing, but for Windows 2000:

Now somewhere in your server, you'll need to start the three threads. Something like this:

If you want to do it like this, your server will have to implement Observer and the update method will look something like this:

Let me say that I still have a little problem with this in that stdin in never closing, because the client socket is not sending an EOF marker since the client socket is still open. It can probably be fixed by shutting down the socket on the server once stdout and stderr both close down.
Hope this helps,
Michael Morris
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
The Server receive request, and carry our in Unix, then foward the result to client.
And I want the user at the Client could operate on the remote Unix freely. Like, cd any directory and list files.

So cd + ls is just an example, and you really want to be able to run an arbitrary series of commands? Gee, I hope this system is only accessed by very trustworthy clients, 'cause this sounds like a major security risk. But if you really want to do this:
It sounds like the problem might be that you're running each command with a separate process. I.e. if you use exec() to run a "cd" command, and then another exec() to run an "ls", the second command runs in a separate process which knows nothing about the first command. I think that you want a single process which simply spawns a new shell (korn/bash/whatever you prefer), and then you feed that process new commands to interpret using the associated OutputStream. Unfortunately I don't presently have access to a UNIX box to test this on, and by googling I can't find any good articles to back me up on this. So maybe I'm just making it up. But I really thought I read somewhere that this was possible. It would look something like:

At least, I hope that's close. Has anyone seen something like this before? Or am I just hallucinating? Too lazy to continue researching it now - hopefully someone out there will have more input. Good luck...
[ February 25, 2003: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451

I think that you want a single process which simply spawns a new shell (korn/bash/whatever you prefer), and then you feed that process new commands to interpret using the associated OutputStream. Unfortunately I don't presently have access to a UNIX box to test this on, and by googling I can't find any good articles to back me up on this. So maybe I'm just making it up. But I really thought I was somewhere that this was possible. It would look something like:

I'm pretty sure, that if you just start a shell with Runtime.getRuntime().exec("/bin/sh") (or /bin/csh or whatever shell) and do the proper plumbing, that Unix will happily kick back a remote shell to the client. I know that NT/2000/XP will by Runtime.getRuntime().exec("cmd"). Unfortunately, I don't have access to a Unix box either or I would do some testing.

Gee, I hope this system is only accessed by very trustworthy clients, 'cause this sounds like a major security risk.

The Ultimate backdoor, especially if the server runs a set UID root.
Michael Morris
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: exec() method is limited in function