`
thecloud
  • 浏览: 872402 次
文章分类
社区版块
存档分类
最新评论

C#利用dig命令解析SRV方法的具体实现

 
阅读更多

利用dig命令查询SRV服务器的方法
1. 什么是SRV
SRV是DNS服务器的数据库中支持的一种资源记录的类型,它记录了哪台计算机提供了哪个服务这么一个简单的信息。在DNS服务器上可以做SRV的配置,使用SRV的好处是需要使用SRV服务的客户端程序不再需要固定SRV服务器的IP地址,这样就增加了灵活性。SRV的详细说明可以参考:http://en.wikipedia.org/wiki/SRV_record

2. 如何解析SRV服务
查询和解析SRV服务可以使用dig命令和nslookup,其中dig既支持windows平台又支持linux平台,是比较好的选择。在各类应用程序中可以通过采用执行系统命令(dig.exe或dig)的方式来解析SRV记录。

3. C# 例子

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Diagnostics;

namespace SRVTest
{
class Program
{
static void Main(string[] args)
{
string digCmd = "dig.exe";
string srv = "@192.168.0.10 srv _test._tcp.testsrv.com +short";
ArrayList serverList = new ArrayList();

ArrayList outputList = executeCmd(digCmd, srv); //第一次获取SRV服务名称字符串
for (int i = 0; i < outputList.Count; i++)
{
string output = (string)outputList[i];
if (output.Contains("timed out"))
continue;
System.Console.WriteLine("Test srv:" + output);
string[] parts = output.Split(' ');
if (parts.Length < 4)
continue;
string srvName = parts[3];
string srv2 = "@192.168.0.10 " + srvName.Substring(0, srvName.Length - 1) + " " + " +short";
ArrayList outputList2 = executeCmd(digCmd, srv2); //第二次根据SRV服务名称获取对应的IP地址字符串
serverList.AddRange(outputList2);
}

for (int i = 0; i < serverList.Count; i++)
{
string server = (string)serverList[i];
System.Console.WriteLine("server " + i + ": " + server); //真正提供testsrv服务的服务器地址
}

}

//执行系统命令获取输出结果
public static ArrayList executeCmd(String cmdName, String args)
{
ArrayList outputList = new ArrayList();
try
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.RedirectStandardError = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.WindowStyle = ProcessWindowStyle.Maximized;
psi.FileName = cmdName;
psi.Arguments = args;

System.Console.WriteLine("execute command \"" + psi.FileName + " " + psi.Arguments + "\"");

Process prc = Process.Start(psi);
string output = "", errorOutput = "";

while ((output = prc.StandardOutput.ReadLine()) != null)
{
System.Console.WriteLine("Output:" + output);
outputList.Add(output);
}
while ((errorOutput = prc.StandardError.ReadLine()) != null)
{
System.Console.WriteLine("Error output:" + errorOutput);
}
prc.WaitForExit(10000);
}
catch (Exception e)
{
System.Console.WriteLine(e.Message);
}
return outputList;
}

}
}

执行结果:

C:\xxx\dig>SRVTest.exe
execute command "dig.exe @192.168.0.10 srv _test._tcp.testsrv.com +short"
Output:0 0 24633 test200.testsrv.com.
Output:0 0 24633 test201.testsrv.com.
Test srv:0 0 24633 test200.testsrv.com.
execute command "dig.exe @192.168.0.10 test200.testsrv.com +short"
Output:192.168.0.200
Test srv:0 0 24633 test201.testsrv.com.
execute command "dig.exe @192.168.0.2 test201.testsrv.com +short"
Output:192.168.0.201
server 0: 192.168.0.200
server 1: 192.168.0.201

分享到:
评论

相关推荐

    linux dig命令详解

    linux dig命令详解linux dig命令详解linux dig命令详解linux dig命令详解linux dig命令详解linux dig命令详解linux dig命令详解linux dig命令详解linux dig命令详解

    linux_下的dig_命令详解

    linux_下的dig_命令详解

    dig命令使用详解

    dig命令使用方法详解,很实用的资料,包括各参数的说明,dns解释过程追踪的命令。

    DNS dig命令详解

    详细描述了DNS中dig命令的详细用法.帮做做链路负载均衡,GSLB等用户快速测试DNS应用

    DIG命令学习小结,还包括windows下的工具安装

    DIG命令学习小结 还包括一个windows下的dig的运行工具

    dig命令windows版.rar

    dig(域信息搜索器)命令是个用于询问 dns 域名服务器的灵活的工具。他执行 dns 搜索,显示从受请求的域名服务器返回的答复。多数 dns 管理员利用 dig 作为 dns 问题的故障诊断,因为他灵活性好、易用、输出清晰。...

    dig命令windows平台使用包

    在 unix 和 linux 下,建议大家使用 dig 命令来代替 nslookup。 dig 命令的功能比 nslookup 强大很多。 这个是dig命令在windows平台上的安装包,安装完需手动设置环境变量

    使用dig/nslookup命令查看dns解析的方法步骤

    dig – DNS lookup utility 当域名出现访问故障时,可以通过检查域名解析来判断是否是由错误的解析导致的问题。 1.域名解析无结果(不存在或被HOLD(未实名认证等)) ...在命令后加上+trace可以跟踪整个解析流

    windows下使用dig命令(附详细教程)

    windows下使用dig命令(附详细教程): 教程为打包文件中的说明文档,按照此教程步骤完成,即可使用,本人亲自体验使用

    Windows下dig命令

    解压,把解压的的内容放到C盘Windows目录下;在cmd(Win+R)下就可以使用。

    windows系统下用dig命令的安装包

    windows系统下用dig命令的安装包

    dig_win32 在windows平台上使用的dig命令

    Dig命令不是只有在linux下才可以使用,现在这个包可以在windows下使用此命令,所得信息比nslookup命令要全面,如果大家在查询DNS的时候,想得到更详尽的资料,可下载此包!

    linux dig命令使用手册

    dig (domain information groper) is a flexible tool for interrogating DNS name servers. It performs DNS lookups and displays the answers that are returned from the name server(s) that were queried. ...

    dig命令工具

    Windows 10 安装BIND Tools完美使用dig命令 Dig是一个在类Unix命令行模式下查询DNS包括NS记录,A记录,MX记录等相关信息的工具。Dig的源码是ISC BIND大包的一部分,但是大多编译和安装Bind的文档都不把它包括在内。

    dig命令 查询域名DNS信息

    dig命令的全称是“domain information groper”,它是一个用来灵活查询DNS的工具,它会打印出DNS域名服务器的回应,dig命令主要用来从DNS域名服务器查询主机地址信息。 dig命令与nslookup命令功能基本相同,但是dig...

    curl和dig命令的win安装包

    windows下安装这两个命令的安装包,请右键管理员运行,自动解压,程序的自解压模块会误报毒,勿慌张,实际无毒,不放心可以手动解压即可。

    windows 系统下安装 dig 命令和nslookup的用法及详例

    dig [@server] [-b address] [-c class] [-f filename] [-k filename] [ -n ][-p port#] [-t type] [-x addr] [-y name:key] [name] [type] [class] [queryopt...] dig @202.99.224.8 www.12530.com nslookup -qt=...

    dig脚本,实现批量dig域名

    批量dig,前段时间来了几千个域名,让看看是不是在本公司解析,dig -f 太慢,写个小教本弄一弄看看

    dig域名解析查询

    dig 比nslookup好用的多,可以递归查询,linux上的工具,您懂的

Global site tag (gtag.js) - Google Analytics