[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1648
  • Last Modified:

How to execute multiple runtime commands

myCommands.sh contains

ls /home/username

The following command resulting correctly,
scenario1: java method callt: runtimeExec("ssh username@ipaddr sh myCommands.sh");

  x.txt
  y.txt
  z.txt

scenario2: But when the user <username> don't have the permission to read the files in the directory, he is able to do sudo and read them in the following manner.

>>ssh username@ipaddr
Password:
>>sudo ls /home/username
Password:
  x.txt
  y.txt
  z.txt              

How can I achieve the same result in the case of scenario2.

//method runtimeExec(command)
               Process proc = null;
            Runtime rt = null;
            StringBuffer result = new StringBuffer();      
            int ch;

            try
               {              
                  //For executing command
                  rt = Runtime.getRuntime();
                        proc = rt.exec(command);

                  InputStreamReader isr = new InputStreamReader(proc.getInputStream());

                  while ((ch = isr.read()) != -1) {
                                     result.append((char) ch);
                    }                  
                 
                  exitVal = proc.waitFor();      
                  
                  if(exitVal != 0)
                  {
                        error = true;
                        isr = new InputStreamReader(proc.getErrorStream());
                        while ((ch = isr.read()) != -1) {
                                      result.append((char) ch);
                          }
                  }
                                    
          }// end try

      catch (Exception e){
              error = true;
     } // end catch



Please help me.

Thanks,
Bhaskar
0
bhaskar20001
Asked:
bhaskar20001
  • 4
  • 2
1 Solution
 
NelliosCommented:
The exact way you open an input stream from the process you can open an output stream in order to type the password for the sudo command.
0
 
bhaskar20001Author Commented:
password will be automatically taken from some linux file for ssh. but for sudo it is going into password prompt is going into an error stream of process.
0
 
NelliosCommented:
No matter in which stream sudo is sending output to, the input is still one and you can controll it.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
bhaskar20001Author Commented:
I don't want to provide password for this user sudo access explicitely through the outputstream. It should auto authenticate by some way and execute the command. I want to know that some way.
0
 
bhaskar20001Author Commented:
I got the solution for this problem. I want to take back the points assigned to this problem.

Thanks,
Bhaskar
0
 
bhaskar20001Author Commented:
The solution to this issue is providing some way of NOPASSWD authentication that particular user group in the /etc/sudoers.

Thanks,
Bhaskar.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now