Created
November 12, 2018 13:50
-
-
Save ugnb/593de7bff11ee27238e7f61b8930477a to your computer and use it in GitHub Desktop.
Execute external program (nslookup) in c# asynchronously to resolve domain MX records.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.Threading.Tasks; | |
namespace DNSResolve | |
{ | |
class Program | |
{ | |
public static async Task Main(string[] args) | |
{ | |
var lookupResponse = await RunProcessAsync("google.com"); | |
Console.WriteLine(lookupResponse); | |
} | |
private static async Task<string> RunProcessAsync(string domain) | |
{ | |
var tcs = new TaskCompletionSource<string>(); | |
var proc = new Process | |
{ | |
StartInfo = new ProcessStartInfo | |
{ | |
FileName = "nslookup", | |
Arguments = $"-q=mx {domain}", | |
UseShellExecute = false, | |
RedirectStandardOutput = true, | |
CreateNoWindow = true | |
}, | |
EnableRaisingEvents = true | |
}; | |
proc.Exited += (sender, args) => | |
{ | |
tcs.SetResult(proc.StandardOutput.ReadToEnd()); | |
proc.Dispose(); | |
}; | |
proc.Start(); | |
return await tcs.Task; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment