aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Unix With Java.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Unix With Java.... " Watch "Unix With Java.... " New topic
Author

Unix With Java....

Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 477
Hi Everyone,

I have a shell script on remote Unix server. I want to use JAVA to access the Unix server and run the script and copy the file onto my windows hard disk!!! Is it possible??? I need some help on this urgently!!! Thanks!!!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42940
    
  68
What kind of access do you have to the Unix machine? The Apache Commons Net library includes a Telnet client; you should be able to use to automate logging into the machine and running the script.

You don't say what "the file" is, but if the server is accessible via FTP, then the Commons Net library also includes an FTP client.
Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 477
I am trying to use SSH. I downloaded a jar file from jscape!!! I am able to connect to the machine.... I have written this code for the connection please check...

Ssh ssh = null;
Process process = null;
String command = "ksh abc.sh";

ssh = new Ssh(Login_Credentials.iP, Login_Credentials.userName, Login_Credentials.passWord);
System.out.println("Connecting Please Wait...");

ssh.connect();
System.out.println("Connected!!!");

process = Runtime.getRuntime().exec(command);
System.out.println("Done!!!");


When I run this code... I get the error message.....

java.io.IOException: Cannot run program "ksh": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at com.b2b.Heal_Check.HealtCheckAutomation.main(HealtCheckAutomation.java:40)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 5 more


Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8997
    
    9

Somnath Mallick wrote:
java.io.IOException: Cannot run program "ksh": CreateProcess error=2, The system cannot find the file specified


Sounds like you don't have "ksh" in your PATH environment variable. Try specifying the complete path.


[How To Ask Questions On JavaRanch]
Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 477
I tried ./ instead of ksh. But still I got the same error...

java.io.IOException: Cannot run program "./": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at com.b2b.Heal_Check.HealtCheckAutomation.main(HealtCheckAutomation.java:40)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 5 more


Could you please tell how to set the varibale....
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8997
    
    9

"./" is not the name of a program. It cannot be executed. I said to try the full path to ksh, something like "/usr/bin/ksh".
Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 477
Modified my code to this.... But to no avail...

Ssh ssh = null;
Process process = null;
String command = "/usr/bin/ksh abc.sh";

ssh = new Ssh(Login_Credentials.iP, Login_Credentials.userName, Login_Credentials.passWord);
System.out.println("Connecting Please Wait...");

ssh.connect();
System.out.println("Connected!!!");

process = Runtime.getRuntime().exec(command);
System.out.println("Done!!!");

Still getting the same error!!!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42940
    
  68
Does an executable named "/usr/bin/ksh" exist on that machine, and does the account under which this code runs have permission to run it?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

1) ssh to the machine using an ssh client like PuTTY.
2) At the command prompt, type 'which ksh'. It should tell you where ksh is.
3) If it doesn't, ask a system administrator for the machine where ksh is.
4) Now that you know the exact path to ksh, use that instead of /usr/bin/ksh.

If ksh is not installed (always a possibility) and the script needs ksh to run, then maybe the script won't run on that machine. Have you confirned that the script actually works?

[Jess in Action][AskingGoodQuestions]
Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 477
I did that and I got the output as /usr/bin/ksh. Which is the same one i used!!!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

You tell us you got the same error, but of course that's impossible; each of your first two attempts gave different error messages. What does it actually say, exactly, when you use /usr/bin/ksh?

You haven't told us where the script is; we've been tacitly assuming it's in your home directory on the UNIX machine. If it's not, then you need to include the full path to the script, as well.
Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 477
This time I got....

java.io.IOException: Cannot run program "usr/bin/ksh": CreateProcess error=2, The system cannot find the file specified
Error!!!
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at com.b2b.Heal_Check.HealthCheckAutomation.main(HealthCheckAutomation.java:40)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 5 more

Which kinda looks as the same as above!!!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Nope -- this time it points out a typo you've made. Looks like you're running

usr/bin/ksh

instead of

/usr/bin/ksh

THat slash at the beginning makes all the difference!
Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 477
Before I run the code... Is it necessary to give the path of the file? Like my file is in the path /export/home/vesta.

What should I use... "/usr/bin/ksh abc.sh" or "/usr/bin/ksh /export/home/vesta/abc.sh"???
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

If you're running a script by passing it as an argument to a shell like this, then the argument has to be a relative or absolute path to the script; so yes, it should be that second one!
Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 477
Didn't work!!!

Connecting Please Wait...
Connected!!!
java.io.IOException: Cannot run program "/usr/bin/ksh": CreateProcess error=3, The system cannot find the path specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at com.b2b.Heal_Check.HealthCheckAutomation.main(HealthCheckAutomation.java:40)
Caused by: java.io.IOException: CreateProcess error=3, The system cannot find the path specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 5 more
Error!!!
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8997
    
    9

I think I see the problem. If this is your code:


The ksh process you are attempting to create is on your local system. That's why it can't find ksh.
If you look at the javadoc for com.jscape.inet.ssh.Ssh, there's an example which shows opening an OutputStream, writing commands to the remote system and using an InputStream to read the remote output.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Aaaak. OK, now I feel dumb -- I never really looked at your code. You're making a connection to the remote machine with your ssh library (I guess -- I'm not familiar with the library.) But then you're using Runtime.exec() to try to run the script ON THE LOCAL WINDOWS MACHINE! Of course /usr/bin/ksh isn't being found -- there's no such file on your Windows computer. You need to push the command line through the ssh connection to the remote machine; exactly how to do that is probably documented somewhere as part of the library you're using. I'm sorry I didn't see this sooner.

In my defense, both Joe and Ulf made the same mistake.

[ Edit: I see Joe noticed our mistake as well! ]
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8997
    
    9

Ernest Friedman-Hill wrote:
In my defense, both Joe and Ulf made the same mistake.


Woah! I said ksh wasn't on the path, which it isn't on Windows computers.
Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 477
I read the code... But didn't get much of it.

Could you guys please modify the code accordingly?? It would be of great help!!!
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8997
    
    9

JavaRanch is Not A Code Mill
Give it a try. It appears to work just like java.lang.Process as far as sending commands and receiving output, so if you're familiar with that, it shouldn't be hard. We'll be here if you have trouble.
Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 477
Ya i ran the code. Its just like bringing the putty box into the java console!!!
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8997
    
    9

So it should be a trivial task to send your script command rather than sending data from standard input.
Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 477
I just want to automate the process... The user should do nothing. That is why... I want to know that is there any way to send the Enter key and y to the OutputStream automatically?
 
Consider Paul's rocket mass heater.
 
subject: Unix With Java....