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!!!

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Random Coder
  • 329
  • 2
  • 5
  • 11

1 Answers1

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:
http://www.jcraft.com/jsch/examples/Sudo.java.html

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).
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Show us your code + Show us a matching interactive SSH session transcript. – Martin Prikryl Jun 28 '17 at 06:57
  • The code on the java side is same as mentioned on the link that you gave : "http://www.jcraft.com/jsch/examples/Sudo.java.html" – Random Coder Jun 28 '17 at 10:10
  • You have to replace the `sudo` with `become` in the `setCommand` call! – Martin Prikryl Jun 28 '17 at 11:45
  • 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 Or you can do: become -list This will list accounts you have access to on this server exit-status: 1 – Random Coder Jun 30 '17 at 05:52
  • 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. – Martin Prikryl Jun 30 '17 at 08:37