1

I have a requirement to check DNS configuration using nslookup command. The proper way to check has been given in the following link. https://technet.microsoft.com/en-us/library/aa997324(v=exchg.65).aspx

Now the actual problem in java is how to execute the commands one by one in the same process and I have to capture the output and based upon the output I may have to execute some other commands. The screenshot is given below.

Screenshot given below

It is easy to execute a simple command like "nslookup someIpAdrs". But here the requirement is different. First of all we have to enter the shell and in the same shell, I have to execute commands.

Please suggest me about how to do in Java processBuilder or if there is any java library to achieve.

2
  • mkyong.com/java/how-to-execute-shell-command-from-java Commented Nov 9, 2017 at 16:13
  • I have already gone through those sites. The requirement is not similar, it is different here. First apply the command "nslookup", you will get a shell. Then in that shell execute different types of commands and capture the output and then execute other commands. Commented Nov 9, 2017 at 16:18

1 Answer 1

3

You need to open a new PrintWriter with the Process that executed your original command and print your arguments there.

For Example:

final Process p = Runtime.getRuntime().exec("nslookup");
final BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
final char[] buf = new char[20];
System.out.println(new String(buf, 0, in.read(buf)));
//Send next command here
new PrintWriter(p.getOutputStream(),true).println(<next command>);

You can then flush the buffer and read the output thereafter.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.