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: 35230
7
posted
0
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.
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...");
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
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
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?
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: 471
posted
0
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
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: 471
posted
0
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!!!
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
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.
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: 471
posted
0
Ya i ran the code. Its just like bringing the putty box into the java console!!!
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: 471
posted
0
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?
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.