my dog learned polymorphism*
The moose likes Linux / UNIX and the fly likes issue in passiing variable to shell script Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "issue in passiing variable to shell script" Watch "issue in passiing variable to shell script" New topic
Author

issue in passiing variable to shell script

Murthy Manchala
Greenhorn

Joined: Aug 20, 2008
Posts: 21
I am trying to send password to a shell script to run batch jobs.

I am not able to send password to shell script via java standalone application. the java main method cannot return value as its return type is "VOID". please advice me how i can get password in shell script though java standalone.

Due to security i can't use system out or i can't write to a file.

I saw below code in google but its not working for me
Test.java:


test.sh

Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11279
    
  59

Murthy Manchala wrote:I saw below code in google but its not working for me

We really need you to tell the details. Just stating that something does not work for you does not help us in any way. The problem could be anywhere.

Based on the code you posted, I noticed two problems with the shell script:

On line 1 you have a space immediately following the = sign. With that space there you should have received an error message along the lines of "murthy not found" or something like that. Take the space out.

On line 2 you do not have a space between the echo keyword and the $out variable. Without that space Unix will concatenate the two words together, and try to execute echomurthy, which is not a command.


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Beside Andrews hints, I have the impression that you try to solve the problem on the basis of bad information.

Murthy Manchala wrote:I am trying to send password to a shell script to run batch jobs.


That's in general not the way to do it. You use the sudoers file instead, which has a nopasswd-option, if possible, alias for linux tasks. Are we talking about linux, by the way?

Due to security i can't use system out or i can't write to a file.
I saw below code in google but its not working for me
Test.java:


In my impression this is just System.out, which you want to avoid.

But how else would you like transfer to transfer the password? It's not just, that java returns void (you may use System.exit, to pass an int), but the caller is only able to receive an int.

Passing the output is a differnt mechanism, but not suitable for password-transfer.

As you mentioned yourself, it can be observed, and it can even be observed in the class-file without executing it (man strings). You may encrypt it, but then you need another password, so you're catched in a devils circle.


http://home.arcor.de/hirnstrom/bewerbung
Murthy Manchala
Greenhorn

Joined: Aug 20, 2008
Posts: 21


I tried below code


Its printing nothing. If i put system.out it is printing the murthy. Is there any way i can get the ret_value in script.

Thanks


Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

You can use System.out, which is not secure, or more complicated ways, which aren't more secure - just more complicated.

For which programs do you need the password? Are we talking about linux?

Murthy Manchala
Greenhorn

Joined: Aug 20, 2008
Posts: 21
I need passord in shell script. In Unix.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

here http://www.gratisoft.us/sudo/man/sudoers.html you find a description of the sudoers file.

The user murthy can run cmdToRun as any user without giving a password.

It may be wise to restrict the command to the bare minimum, i.e. maybe not the script itself, but critical commands inside the script.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11279
    
  59

Murthy Manchala wrote:Its printing nothing. If i put system.out it is printing the murthy.

What "it" are you talking about here?

Is "it" the Java program? In which case you are correct - without you telling it out send something to System.out or System.err you will not see any output from the Java program. And neither will the Shell script.

Is "it" the shell script? In which case the shell script you are referring to shows that you are getting the output from the Java application into the Shell script. In line 18 of your listing above, you run the Java application, and store it's output in the "out" variable. In line 19 you display the output on screen. So if you do see "murthy" on screen when running the script, then you already have the ret_value in your script.

Please, please, please pay attention to Stefan's posts. Creating an application in this manner that outputs the password is such an incredibly bad idea that you could end up getting in big trouble at your work when someone discovers what you have done (especially if they find out because their systems have been hacked, and especially if they also discover that you have been told many times that this is a bad idea).
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: issue in passiing variable to shell script
 
Similar Threads
problem in standalone java returning value to shell script
to send sms.
how to get the return value from java method by shell script
How to return a value from java to script
Attempted a bean operation on a null object