how to separate/filter an output string and write the separated data into different text boxes?












0















So i have a button and two text boxes.
I want to click a button, it will execute nslookup then i want to :



-write the resolved hostname into one text box
-write the resolved ip adress into next text box


I have this so far



    System.Diagnostics.Process p = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
psi.FileName = "nslookup.exe";
psi.Arguments = "google.com";

psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;

psi.CreateNoWindow = false;
p.StartInfo = psi;
p.Start();

p.WaitForExit();

System.IO.StreamReader output = p.StandardOutput;

textbox1.Text = output.ReadToEnd().ToString();


So now it does the resolution and writes everything into one string.
How can i filter the output string and write specific parts of the string into separate boxes ?



Example output string will be : ( it is a single line string but i wrote it here in a table for easy understanding )



Server:  EXAMPLE //this i dont need
Address: EXAMPLE //this i dont need

Name: google.com //i need this to be written to TextBox1
Address: 172.217.21.206 //i need this to be written to TextBox2


So that in the end :



Textbox.Text = "google.com"
Textbox2.Text = "172.217.21.206"


Later i want to ping the ip in textbox2 and make the text box change color if its reachable, and a button to rdp connect to that ip if its reachable, so i need it to not have any spaces and just be a string



I was thinking to write each word that is separated by a space into an array and then read the array and write things that match into the boxes with something like this :



string words = outputstring.Split(' ');

foreach (var word in words)
{
System.Console.WriteLine($"<{word}>");
}


But before i continue with it i wanted to ask if there is an easier and faster way to do it and im going in the wrong direction alltogether ? maybe there is a way to just return specific parameters out of the nslookup command ?










share|improve this question

























  • nslookup will return several ip addresses, which one you want?

    – SeM
    Nov 23 '18 at 12:39











  • Bear in mind that, as it says in the [nslookup] wiki, general support for nslookup is off topic here.

    – Ian
    Nov 23 '18 at 12:41











  • Why not use Dns.GetHostByName ("google.com"); from System.Net and than use the result to fill the Text Boxes?

    – Daniel W.
    Nov 23 '18 at 12:41











  • Yeah dns.gethostbyname is the best and fastest way, i looked everywhere on the internet for something like it but couldnt find it, i am a beginner

    – John Linaer
    Nov 23 '18 at 13:09
















0















So i have a button and two text boxes.
I want to click a button, it will execute nslookup then i want to :



-write the resolved hostname into one text box
-write the resolved ip adress into next text box


I have this so far



    System.Diagnostics.Process p = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
psi.FileName = "nslookup.exe";
psi.Arguments = "google.com";

psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;

psi.CreateNoWindow = false;
p.StartInfo = psi;
p.Start();

p.WaitForExit();

System.IO.StreamReader output = p.StandardOutput;

textbox1.Text = output.ReadToEnd().ToString();


So now it does the resolution and writes everything into one string.
How can i filter the output string and write specific parts of the string into separate boxes ?



Example output string will be : ( it is a single line string but i wrote it here in a table for easy understanding )



Server:  EXAMPLE //this i dont need
Address: EXAMPLE //this i dont need

Name: google.com //i need this to be written to TextBox1
Address: 172.217.21.206 //i need this to be written to TextBox2


So that in the end :



Textbox.Text = "google.com"
Textbox2.Text = "172.217.21.206"


Later i want to ping the ip in textbox2 and make the text box change color if its reachable, and a button to rdp connect to that ip if its reachable, so i need it to not have any spaces and just be a string



I was thinking to write each word that is separated by a space into an array and then read the array and write things that match into the boxes with something like this :



string words = outputstring.Split(' ');

foreach (var word in words)
{
System.Console.WriteLine($"<{word}>");
}


But before i continue with it i wanted to ask if there is an easier and faster way to do it and im going in the wrong direction alltogether ? maybe there is a way to just return specific parameters out of the nslookup command ?










share|improve this question

























  • nslookup will return several ip addresses, which one you want?

    – SeM
    Nov 23 '18 at 12:39











  • Bear in mind that, as it says in the [nslookup] wiki, general support for nslookup is off topic here.

    – Ian
    Nov 23 '18 at 12:41











  • Why not use Dns.GetHostByName ("google.com"); from System.Net and than use the result to fill the Text Boxes?

    – Daniel W.
    Nov 23 '18 at 12:41











  • Yeah dns.gethostbyname is the best and fastest way, i looked everywhere on the internet for something like it but couldnt find it, i am a beginner

    – John Linaer
    Nov 23 '18 at 13:09














0












0








0








So i have a button and two text boxes.
I want to click a button, it will execute nslookup then i want to :



-write the resolved hostname into one text box
-write the resolved ip adress into next text box


I have this so far



    System.Diagnostics.Process p = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
psi.FileName = "nslookup.exe";
psi.Arguments = "google.com";

psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;

psi.CreateNoWindow = false;
p.StartInfo = psi;
p.Start();

p.WaitForExit();

System.IO.StreamReader output = p.StandardOutput;

textbox1.Text = output.ReadToEnd().ToString();


So now it does the resolution and writes everything into one string.
How can i filter the output string and write specific parts of the string into separate boxes ?



Example output string will be : ( it is a single line string but i wrote it here in a table for easy understanding )



Server:  EXAMPLE //this i dont need
Address: EXAMPLE //this i dont need

Name: google.com //i need this to be written to TextBox1
Address: 172.217.21.206 //i need this to be written to TextBox2


So that in the end :



Textbox.Text = "google.com"
Textbox2.Text = "172.217.21.206"


Later i want to ping the ip in textbox2 and make the text box change color if its reachable, and a button to rdp connect to that ip if its reachable, so i need it to not have any spaces and just be a string



I was thinking to write each word that is separated by a space into an array and then read the array and write things that match into the boxes with something like this :



string words = outputstring.Split(' ');

foreach (var word in words)
{
System.Console.WriteLine($"<{word}>");
}


But before i continue with it i wanted to ask if there is an easier and faster way to do it and im going in the wrong direction alltogether ? maybe there is a way to just return specific parameters out of the nslookup command ?










share|improve this question
















So i have a button and two text boxes.
I want to click a button, it will execute nslookup then i want to :



-write the resolved hostname into one text box
-write the resolved ip adress into next text box


I have this so far



    System.Diagnostics.Process p = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
psi.FileName = "nslookup.exe";
psi.Arguments = "google.com";

psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;

psi.CreateNoWindow = false;
p.StartInfo = psi;
p.Start();

p.WaitForExit();

System.IO.StreamReader output = p.StandardOutput;

textbox1.Text = output.ReadToEnd().ToString();


So now it does the resolution and writes everything into one string.
How can i filter the output string and write specific parts of the string into separate boxes ?



Example output string will be : ( it is a single line string but i wrote it here in a table for easy understanding )



Server:  EXAMPLE //this i dont need
Address: EXAMPLE //this i dont need

Name: google.com //i need this to be written to TextBox1
Address: 172.217.21.206 //i need this to be written to TextBox2


So that in the end :



Textbox.Text = "google.com"
Textbox2.Text = "172.217.21.206"


Later i want to ping the ip in textbox2 and make the text box change color if its reachable, and a button to rdp connect to that ip if its reachable, so i need it to not have any spaces and just be a string



I was thinking to write each word that is separated by a space into an array and then read the array and write things that match into the boxes with something like this :



string words = outputstring.Split(' ');

foreach (var word in words)
{
System.Console.WriteLine($"<{word}>");
}


But before i continue with it i wanted to ask if there is an easier and faster way to do it and im going in the wrong direction alltogether ? maybe there is a way to just return specific parameters out of the nslookup command ?







c# .net string nslookup






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 12:39









Konamiman

43.1k1598128




43.1k1598128










asked Nov 23 '18 at 12:31









John LinaerJohn Linaer

54




54













  • nslookup will return several ip addresses, which one you want?

    – SeM
    Nov 23 '18 at 12:39











  • Bear in mind that, as it says in the [nslookup] wiki, general support for nslookup is off topic here.

    – Ian
    Nov 23 '18 at 12:41











  • Why not use Dns.GetHostByName ("google.com"); from System.Net and than use the result to fill the Text Boxes?

    – Daniel W.
    Nov 23 '18 at 12:41











  • Yeah dns.gethostbyname is the best and fastest way, i looked everywhere on the internet for something like it but couldnt find it, i am a beginner

    – John Linaer
    Nov 23 '18 at 13:09



















  • nslookup will return several ip addresses, which one you want?

    – SeM
    Nov 23 '18 at 12:39











  • Bear in mind that, as it says in the [nslookup] wiki, general support for nslookup is off topic here.

    – Ian
    Nov 23 '18 at 12:41











  • Why not use Dns.GetHostByName ("google.com"); from System.Net and than use the result to fill the Text Boxes?

    – Daniel W.
    Nov 23 '18 at 12:41











  • Yeah dns.gethostbyname is the best and fastest way, i looked everywhere on the internet for something like it but couldnt find it, i am a beginner

    – John Linaer
    Nov 23 '18 at 13:09

















nslookup will return several ip addresses, which one you want?

– SeM
Nov 23 '18 at 12:39





nslookup will return several ip addresses, which one you want?

– SeM
Nov 23 '18 at 12:39













Bear in mind that, as it says in the [nslookup] wiki, general support for nslookup is off topic here.

– Ian
Nov 23 '18 at 12:41





Bear in mind that, as it says in the [nslookup] wiki, general support for nslookup is off topic here.

– Ian
Nov 23 '18 at 12:41













Why not use Dns.GetHostByName ("google.com"); from System.Net and than use the result to fill the Text Boxes?

– Daniel W.
Nov 23 '18 at 12:41





Why not use Dns.GetHostByName ("google.com"); from System.Net and than use the result to fill the Text Boxes?

– Daniel W.
Nov 23 '18 at 12:41













Yeah dns.gethostbyname is the best and fastest way, i looked everywhere on the internet for something like it but couldnt find it, i am a beginner

– John Linaer
Nov 23 '18 at 13:09





Yeah dns.gethostbyname is the best and fastest way, i looked everywhere on the internet for something like it but couldnt find it, i am a beginner

– John Linaer
Nov 23 '18 at 13:09












2 Answers
2






active

oldest

votes


















2














You can use Dns.GetHostEntry instead of manually calling external process:



IPHostEntry hostInfo = Dns.GetHostEntry("example.com");
textbox1.Text = hostInfo.HostName;
textbox2.Text = hostInfo.AddressList[yourIndex].ToString();





share|improve this answer
























  • so what do i put in "yourIndex" ? if i put 1 or 2 it throws an index out of range exception

    – John Linaer
    Nov 23 '18 at 12:59











  • @JohnLinaer It depends what you want to get, for example for google.com it returns about 10 addresses, what you want to show? What is your task?

    – SeM
    Nov 23 '18 at 13:01













  • Also you maybe will need to put this in try/catch if it will be user input, cause it will throw SocketException if host address will not be found.

    – SeM
    Nov 23 '18 at 13:03











  • well i have servers that are in my network, so a server host name would be example.domain.com, so then i wanna do an nslookup on "example" and it would return example.domain.com and that should be written into the text box. when i do nslookup using cmd it shows 2 names, first the domain one which usually is the first, and then the second which is the hostname that i am looking for, so basically i want it to return the hostname of the server + domain there is no user input, i just wanna make a server status checker, to see if the server is online + show the info like hostname and ip adress

    – John Linaer
    Nov 23 '18 at 13:07













  • @JohnLinaer What happened to ip address in question?

    – SeM
    Nov 23 '18 at 13:09



















0














Regular expressions can help you here. Here's how you could retrieve the name, for the address it would be similar:



Regex.Match(text, @"Name: *(?<name>[^ ]+)").Groups["name"].Value





share|improve this answer
























  • How does this cope with multiple lines with "Name: " ?

    – Ian
    Nov 23 '18 at 12:42














Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446781%2fhow-to-separate-filter-an-output-string-and-write-the-separated-data-into-differ%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You can use Dns.GetHostEntry instead of manually calling external process:



IPHostEntry hostInfo = Dns.GetHostEntry("example.com");
textbox1.Text = hostInfo.HostName;
textbox2.Text = hostInfo.AddressList[yourIndex].ToString();





share|improve this answer
























  • so what do i put in "yourIndex" ? if i put 1 or 2 it throws an index out of range exception

    – John Linaer
    Nov 23 '18 at 12:59











  • @JohnLinaer It depends what you want to get, for example for google.com it returns about 10 addresses, what you want to show? What is your task?

    – SeM
    Nov 23 '18 at 13:01













  • Also you maybe will need to put this in try/catch if it will be user input, cause it will throw SocketException if host address will not be found.

    – SeM
    Nov 23 '18 at 13:03











  • well i have servers that are in my network, so a server host name would be example.domain.com, so then i wanna do an nslookup on "example" and it would return example.domain.com and that should be written into the text box. when i do nslookup using cmd it shows 2 names, first the domain one which usually is the first, and then the second which is the hostname that i am looking for, so basically i want it to return the hostname of the server + domain there is no user input, i just wanna make a server status checker, to see if the server is online + show the info like hostname and ip adress

    – John Linaer
    Nov 23 '18 at 13:07













  • @JohnLinaer What happened to ip address in question?

    – SeM
    Nov 23 '18 at 13:09
















2














You can use Dns.GetHostEntry instead of manually calling external process:



IPHostEntry hostInfo = Dns.GetHostEntry("example.com");
textbox1.Text = hostInfo.HostName;
textbox2.Text = hostInfo.AddressList[yourIndex].ToString();





share|improve this answer
























  • so what do i put in "yourIndex" ? if i put 1 or 2 it throws an index out of range exception

    – John Linaer
    Nov 23 '18 at 12:59











  • @JohnLinaer It depends what you want to get, for example for google.com it returns about 10 addresses, what you want to show? What is your task?

    – SeM
    Nov 23 '18 at 13:01













  • Also you maybe will need to put this in try/catch if it will be user input, cause it will throw SocketException if host address will not be found.

    – SeM
    Nov 23 '18 at 13:03











  • well i have servers that are in my network, so a server host name would be example.domain.com, so then i wanna do an nslookup on "example" and it would return example.domain.com and that should be written into the text box. when i do nslookup using cmd it shows 2 names, first the domain one which usually is the first, and then the second which is the hostname that i am looking for, so basically i want it to return the hostname of the server + domain there is no user input, i just wanna make a server status checker, to see if the server is online + show the info like hostname and ip adress

    – John Linaer
    Nov 23 '18 at 13:07













  • @JohnLinaer What happened to ip address in question?

    – SeM
    Nov 23 '18 at 13:09














2












2








2







You can use Dns.GetHostEntry instead of manually calling external process:



IPHostEntry hostInfo = Dns.GetHostEntry("example.com");
textbox1.Text = hostInfo.HostName;
textbox2.Text = hostInfo.AddressList[yourIndex].ToString();





share|improve this answer













You can use Dns.GetHostEntry instead of manually calling external process:



IPHostEntry hostInfo = Dns.GetHostEntry("example.com");
textbox1.Text = hostInfo.HostName;
textbox2.Text = hostInfo.AddressList[yourIndex].ToString();






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 12:46









SeMSeM

4,66011631




4,66011631













  • so what do i put in "yourIndex" ? if i put 1 or 2 it throws an index out of range exception

    – John Linaer
    Nov 23 '18 at 12:59











  • @JohnLinaer It depends what you want to get, for example for google.com it returns about 10 addresses, what you want to show? What is your task?

    – SeM
    Nov 23 '18 at 13:01













  • Also you maybe will need to put this in try/catch if it will be user input, cause it will throw SocketException if host address will not be found.

    – SeM
    Nov 23 '18 at 13:03











  • well i have servers that are in my network, so a server host name would be example.domain.com, so then i wanna do an nslookup on "example" and it would return example.domain.com and that should be written into the text box. when i do nslookup using cmd it shows 2 names, first the domain one which usually is the first, and then the second which is the hostname that i am looking for, so basically i want it to return the hostname of the server + domain there is no user input, i just wanna make a server status checker, to see if the server is online + show the info like hostname and ip adress

    – John Linaer
    Nov 23 '18 at 13:07













  • @JohnLinaer What happened to ip address in question?

    – SeM
    Nov 23 '18 at 13:09



















  • so what do i put in "yourIndex" ? if i put 1 or 2 it throws an index out of range exception

    – John Linaer
    Nov 23 '18 at 12:59











  • @JohnLinaer It depends what you want to get, for example for google.com it returns about 10 addresses, what you want to show? What is your task?

    – SeM
    Nov 23 '18 at 13:01













  • Also you maybe will need to put this in try/catch if it will be user input, cause it will throw SocketException if host address will not be found.

    – SeM
    Nov 23 '18 at 13:03











  • well i have servers that are in my network, so a server host name would be example.domain.com, so then i wanna do an nslookup on "example" and it would return example.domain.com and that should be written into the text box. when i do nslookup using cmd it shows 2 names, first the domain one which usually is the first, and then the second which is the hostname that i am looking for, so basically i want it to return the hostname of the server + domain there is no user input, i just wanna make a server status checker, to see if the server is online + show the info like hostname and ip adress

    – John Linaer
    Nov 23 '18 at 13:07













  • @JohnLinaer What happened to ip address in question?

    – SeM
    Nov 23 '18 at 13:09

















so what do i put in "yourIndex" ? if i put 1 or 2 it throws an index out of range exception

– John Linaer
Nov 23 '18 at 12:59





so what do i put in "yourIndex" ? if i put 1 or 2 it throws an index out of range exception

– John Linaer
Nov 23 '18 at 12:59













@JohnLinaer It depends what you want to get, for example for google.com it returns about 10 addresses, what you want to show? What is your task?

– SeM
Nov 23 '18 at 13:01







@JohnLinaer It depends what you want to get, for example for google.com it returns about 10 addresses, what you want to show? What is your task?

– SeM
Nov 23 '18 at 13:01















Also you maybe will need to put this in try/catch if it will be user input, cause it will throw SocketException if host address will not be found.

– SeM
Nov 23 '18 at 13:03





Also you maybe will need to put this in try/catch if it will be user input, cause it will throw SocketException if host address will not be found.

– SeM
Nov 23 '18 at 13:03













well i have servers that are in my network, so a server host name would be example.domain.com, so then i wanna do an nslookup on "example" and it would return example.domain.com and that should be written into the text box. when i do nslookup using cmd it shows 2 names, first the domain one which usually is the first, and then the second which is the hostname that i am looking for, so basically i want it to return the hostname of the server + domain there is no user input, i just wanna make a server status checker, to see if the server is online + show the info like hostname and ip adress

– John Linaer
Nov 23 '18 at 13:07







well i have servers that are in my network, so a server host name would be example.domain.com, so then i wanna do an nslookup on "example" and it would return example.domain.com and that should be written into the text box. when i do nslookup using cmd it shows 2 names, first the domain one which usually is the first, and then the second which is the hostname that i am looking for, so basically i want it to return the hostname of the server + domain there is no user input, i just wanna make a server status checker, to see if the server is online + show the info like hostname and ip adress

– John Linaer
Nov 23 '18 at 13:07















@JohnLinaer What happened to ip address in question?

– SeM
Nov 23 '18 at 13:09





@JohnLinaer What happened to ip address in question?

– SeM
Nov 23 '18 at 13:09













0














Regular expressions can help you here. Here's how you could retrieve the name, for the address it would be similar:



Regex.Match(text, @"Name: *(?<name>[^ ]+)").Groups["name"].Value





share|improve this answer
























  • How does this cope with multiple lines with "Name: " ?

    – Ian
    Nov 23 '18 at 12:42


















0














Regular expressions can help you here. Here's how you could retrieve the name, for the address it would be similar:



Regex.Match(text, @"Name: *(?<name>[^ ]+)").Groups["name"].Value





share|improve this answer
























  • How does this cope with multiple lines with "Name: " ?

    – Ian
    Nov 23 '18 at 12:42
















0












0








0







Regular expressions can help you here. Here's how you could retrieve the name, for the address it would be similar:



Regex.Match(text, @"Name: *(?<name>[^ ]+)").Groups["name"].Value





share|improve this answer













Regular expressions can help you here. Here's how you could retrieve the name, for the address it would be similar:



Regex.Match(text, @"Name: *(?<name>[^ ]+)").Groups["name"].Value






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 12:41









KonamimanKonamiman

43.1k1598128




43.1k1598128













  • How does this cope with multiple lines with "Name: " ?

    – Ian
    Nov 23 '18 at 12:42





















  • How does this cope with multiple lines with "Name: " ?

    – Ian
    Nov 23 '18 at 12:42



















How does this cope with multiple lines with "Name: " ?

– Ian
Nov 23 '18 at 12:42







How does this cope with multiple lines with "Name: " ?

– Ian
Nov 23 '18 at 12:42




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446781%2fhow-to-separate-filter-an-output-string-and-write-the-separated-data-into-differ%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()