-1

Requirement is to run the following unix command sudo su - fwt****app in java.

Below is my code:

Channel channel=session.openChannel("exec");

String[] cmd = {"/bin/bash","-c","echo password| sudo -S -p su - fwt***app; ls"};

((ChannelExec)channel).setCommand(cmd.toString());

InputStream in=channel.getInputStream();

OutputStream out=channel.getOutputStream();

((ChannelExec)channel).setErrStream(System.err);

channel.connect();

out.write(("Password"+"\n").getBytes()); 

out.flush(); 

Have tried other method as well, which is ((ChannelExec)channel).setCommand(sudo -S -p su - fwt***app), it didn't worked.

I have tried switching to other user by using command su - fwtveguiapp Password: But got response as - su: Sorry It needs to run using both sudo and su commands. As both sudo and su commands are required I am getting a syntax error in sudo command. Other references helps only with sudo command hence they were not helpful in my case.

pmbc
  • 1
  • 2
  • I have checked the above solution it didn't worked for me as the above code requires both sudo and su command to be used together – pmbc Jun 13 '16 at 11:48
  • And how does that make it not work? – tripleee Jun 13 '16 at 13:01
  • I get the below error on executing the following command in java : sudo -S -p su - fwtveguiapp; Error : sudo: '-' requires an argument usage: sudo -K | -L | -V | -h | -k | -l | -v usage: sudo [-HPSb] [-p prompt] [-u username|#uid] [-g group|#gid] { -e file [...] | -i | -s | } – pmbc Jun 14 '16 at 05:38
  • You are specifying the option `-p su` to `sudo` where apparently your intent is something else. Check the manual page when you use options. – tripleee Jun 14 '16 at 05:44
  • the requirement is to execute sudo su - fwt****pp command, which requires password using java. Hence I have used sudo -S -p su - fwt**app command which will prompt for password but it turn outs to be a wrong syntax. Could there be some other way? – pmbc Jun 14 '16 at 05:49
  • Read this slowly. The `-p` option to `sudo` takes a parameter. You are not running `su`, you are specifying `su` as the string to use as the prompt for `sudo` and the stuff after it is also being interpreted as options to `sudo`, not to `su` – tripleee Jun 14 '16 at 05:52
  • Opening the channel as shell and using ps.println("sudo su - fwtveguiapp"); getting connected; thanx :-) – pmbc Jun 14 '16 at 10:32

1 Answers1

0

I think it is not possible to switch to root or other user.

But what you can do is running a process with sudo. I think you need to provide each argument as String in the cmd[]

String[] cmd = {"/bin/bash","-c","sudo", "fwt***app"};

And if you have multiple commands, then you need to execute each command separately

Lorenz Pfisterer
  • 802
  • 7
  • 17