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
LVL 4
bhaskar20001Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.