0

Working with Unix server... My requirement is to read the name of the file that is there at /a/b/c/node01/d.ear location on a Unix server and I have do the same through a java program. The problem is that the directory a is a restricted directory and is accessible only to certain users. On the Unix side, I first issue a become command like become a, then supply the password and then using cd command, I reach the d.ear directory and then get to see the name of the file.

How do I do all of this via a Java program?

I don't mind if my Java program calls a shell script that accesses the restricted directory and then reach d.ear and fetch the name of the file and returns the same to the java program. Do we have a way of doing this? Maybe issuing the become command inside the script which is called from the Java program and the password which is asked after become command is supplied as a parameter while calling the script???

Is this approach doable? I am very new to Unix commands and JSch library. Kindly provide the code or any other alternate solutions...

Thanks!!!

1 Answer 1

0

As I have suggested you already, your become command seems to behave the same way (from an interface/API point of view) as common *nix su or sudo.

So, use the same solution as for those. There are many questions on Stack Overflow covering use of su/sudo with JSch.

There's even an official JSch example Sudo.java.

In short:

  • Execute become command
  • Feed a password to its input
  • Assuming the become starts a new shell (as su or sudo do), you feed the commands to be executed in the elevated environment to become input (the same was as the password).
Sign up to request clarification or add additional context in comments.

5 Comments

Show us your code + Show us a matching interactive SSH session transcript.
The code on the java side is same as mentioned on the link that you gave : "jcraft.com/jsch/examples/Sudo.java.html"
You have to replace the sudo with become in the setCommand call!
Replaced like : ((ChannelExec)channel).setCommand("become -S -p '' "+command); The input supplied on getting the prompt "Enter command, execed with sudo", "printenv SUDO_USER" is : become cussdv01 O/P is : Usage: become <account> Or you can do: become -list This will list accounts you have access to on this server exit-status: 1
Come on! Think a bit! This is not, what you are doing in shell! You are doing something like: (ChannelExec)channel).setCommand("become cussdv01"); I cannot be exact, as your comments are hardly readable. Particularly it's not clear what are individual lines.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.