Konsole output
Hy,I am trying to get the output from the console after executing an operation
String output =new String();
String line;
Runtime runtime = Runtime.getRuntime();
try{
Process process = runtime.exec("konsole -e snort -v");
InputStream is = process.getInputStream();
InputStreamReader isr =new InputStreamReader(is);
BufferedReader br =new BufferedReader(isr);
while((line = br.readLine())!=null){
output += line+"\n";
}
}
catch(Exception e){
System.out.println(e);
}
System.out.println(output);
the problem is that no data is being displayed and no errors
What is wrong or do you have an example that does the same thing ?
Thanks
[1190 byte] By [
Vladislava] at [2007-11-27 5:42:25]

Not sure exactly what the problem is. You should however change your code so it looks more like this:
String output = new String();
String line;
StringBuffer sb;
Runtime runtime = Runtime.getRuntime();
try{
sb = new StringBuffer();
Process process = runtime.exec("konsole -e snort -v");
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
while((line = br.readLine())!=null){
sb.append(line+'\n'); // Much better performance than your existing approach
}
Joining Strings the way you were is bad for performance. Have you tried putting in a System.out in your while loop to see if it is entered?
BTW snort? Breaking into a few networks are we :)
EDIT: Thats airsnort I am thinking of...
Message was edited by:
_helloWorld_
and don't I have to include also System.out.println(sb) or how to I print what is in sb?snort-is an intrusion detection system
Sorry, I left some code off. Try this.
public static void main(String[] args) {
String output = new String();
String line;
StringBuffer sb = null;
Runtime runtime = Runtime.getRuntime();
try{
sb = new StringBuffer();
Process process = runtime.exec("konsole -e snort -v");
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
while((line = br.readLine())!=null){
System.out.println("Inside Loop");
sb.append(line+'\n'); // Much better performance than your existing approach
}
}
catch(Exception e){
System.out.println(e);
}
System.out.println(sb.toString());
}
thanks for the reply , but still no error and most important No output displayed.Not even "Inside loop "what else should I try or I do ?
Uhm, Can you try calling something like "ls -l" or another shell command rather than the snort command and see if that produces any output?
I tested the code on windows with a DOS prompt and it works fine. It must be something to do with Konsole or Snort. I don't have any experience with Java on Linux, maybe someone else can help.
with the "ls -l " the output is displayed ; it must be Linux or Snort problemI'll try to fix ; if you have any other suggestions please displayed it
why only a few lines are being displayed and not all the output?
I don't get it: if I execute the same command in the konsole the output exists and is there .Am I not doing the right thing?
Hey,
Try replacing this:
Process process = runtime.exec("konsole -e snort -v");
//With this
String[] cmd = new String[]{"konsole", "-e", "snort -v"};
Process process = runtime.exec(cmd);
Message was edited by:
_helloWorld_
Also, have you tried giving the full file path to snort? ie /bin/snort or whatever its location might be? ls will work without the path as it will be in the system path, if snort is a 3rd party app for instance then the full path will be required if it does not reside in the default directory.
I will try the second option ; from the console it works fine while executing"snort -v" but from java no errors and no output;I have also tried with the full path and the same results : no errors and no output I will try also more options and I ll post later if it worked or