Solved

Strange behaviour of scp

Posted on 2004-08-02
15
537 Views
Last Modified: 2012-05-05
Hi!

I have some strange behaviour of the scp.exe (Cygwin on Win2k)
When I start the Programm manually on the Commandline it gives me some fine output (percentage, eta, speed ...)
But when I try to start the same thing in a java-process (via Runtime.exec())
neither the output nor the error stream give me any data.

scp itself works fine, meaning the file is successfully transfered, and the streams are closed afterwards.
But where is my output?

Anyone a clue?

Bye,
marko
0
Comment
Question by:karlika
  • 6
  • 5
  • 2
  • +1
15 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 125 total points
ID: 11693471
You probably are not handling stderr and stdin asynchronously. Try this:

SNIP========================================================================


public class CommandRunner {

      private int returnValue = Integer.MIN_VALUE;



      public int getReturnValue() {
            return returnValue;
      }



      public static void main(String[] args) {
            new CommandRunner().run(args);
      }






      public void run(String[] args) {

            try {

                  if (args.length < 1) {
                        System.out.println("Usage: java CommandRunner <command string>");
                        System.exit(-1);
                  }
                  Process pro = null;
                  if (args.length > 1) {
                        pro = Runtime.getRuntime().exec(args);
                  }
                  else {
                        pro = Runtime.getRuntime().exec(args[0]);
                  }
                  InputStream error = pro.getErrorStream();
                  InputStream output = pro.getInputStream();
                  Thread err = new Thread(new OutErrReader(error));
                  Thread out = new Thread(new OutErrReader(output));
                  out.start();
                  err.start();
                  returnValue = pro.waitFor();
            }
            catch (java.io.IOException e) {
                  e.printStackTrace();
            }
            catch (java.lang.InterruptedException e) {
                  e.printStackTrace();
            }

      }


      class OutErrReader implements Runnable {
            InputStream is;


            public OutErrReader(InputStream is) {
                  this.is = is;
            }


            public void run() {
                  try {
                        BufferedReader in = new BufferedReader(new InputStreamReader(is));
                        String temp = null;
                        while ((temp = in.readLine()) != null) {
                              System.out.println(temp);
                        }
                        is.close();
                  }
                  catch (Exception e) {
                        e.printStackTrace();
                  }
            }
      }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11693493
Use as follows:

CommandRunner.main(new String[] { "scp.exe", "arg1", "arg2" });
0
 
LVL 2

Author Comment

by:karlika
ID: 11693541
Well, Im allready doing this.
It worked fine until I switched to a newer Version of cygwin.
The only thing I get now from output.readLine() and error.readLine() is null.

Regards,
Marko
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11693573
I'm not familiar with scp.exe. You don't have to run an additional shell interpreter do you? i.e. something like

scp.exe sh <some shell script>

?
0
 
LVL 2

Author Comment

by:karlika
ID: 11693604
Oke, it does not seem to be a Java Problem, since the command:
scp [some options] > testout 2>&1

Which should print every output into the testout file delivers an empty file.
But, how does the output come to the screen if not over the standard output and error -streams?

And how can I access this output?

Regards,
Marko
0
 
LVL 2

Author Comment

by:karlika
ID: 11693649
No, you just call the programm directly.
No additional interpreters should be involved...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11693834
Does the simpler

scp [some options] > testout

work?
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 35

Accepted Solution

by:
TimYates earned 125 total points
ID: 11693868
Yeah, I don't believe you can redirect the output scp creates (as it is an interactive progress meter)...

What you can do, is use the exit code to work out if the operation completed successfully :-)

It returns 0 if it was successful, or something else if it failed...

http://sunportal.sunmanagers.org/pipermail/summaries/2002-May/001710.html

Tim
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11693892
So basically, check that

    returnValue = pro.waitFor();

(or whatever you have) returns 0
0
 
LVL 2

Author Comment

by:karlika
ID: 11695610
BAAAD scp.
Im using the output to draw a download progress bar, so the exit code does not really help me.

Anyone know another windows scp implementation which works with SSH 2?
I used pscp before but it doesnt support ssh2 identity files, which I am bound to use.

Regards,
marko
0
 
LVL 92

Expert Comment

by:objects
ID: 11699977
> But, how does the output come to the screen if not over the standard output and error -streams?

Its probably using a different stream than stdout or stderr.
0
 
LVL 2

Author Comment

by:karlika
ID: 11702467
Im just checking the filesize periodically to power my progress bar...

Thanks for your comments
0
 
LVL 92

Expert Comment

by:objects
ID: 11702502
heres another example of redirecting process output. It sends it to a text area but modifying to update a progress bar would be a simple change.

http://www.objects.com.au/java/examples/util/SwingExec.do

You just need to work out where scp is sending its output.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11702576
>> But, how does the output come to the screen if not over the standard output and error -streams?

It will use stderr/stdout. You will probably have to ditch the stream redirection in the command itself,handling it instead with Java
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11702583
:-)
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Java 1603 Error 2 42
sumDigits  challenge 7 61
wordlen challenge 3 47
Requested array size exceeds VM limit 3 50
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now