可以在 mac os 上以编程方式更改 DNS 吗?

发布于 2024-11-03 08:55:45 字数 145 浏览 0 评论 0原文

有使用 SystemConfiguration 框架或其他框架的示例吗? (类似的问题在 Mac OS X 上以编程方式查找 DNS 服务器设置的答案非常令人困惑)

Any example using the the SystemConfiguration framework or other frameworks ? (similar question Finding DNS server settings programmatically on Mac OS X has quite confusing answers )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寻找我们的幸福 2024-11-10 08:55:45

我最近也遇到了同样的问题。我在这里发布了我的解决方案:

http://blog.notampering.com/

这是片段......希望这有帮助。

#include <stdio.h>
#include <SystemConfiguration/SCPreferences.h>
#include <SystemConfiguration/SCDynamicStore.h>


int main (int argc, const char * argv[])
{
    //get current values
    SCDynamicStoreRef dynRef=SCDynamicStoreCreate(kCFAllocatorSystemDefault, CFSTR("iked"), NULL, NULL);
CFDictionaryRef ipv4key = SCDynamicStoreCopyValue(dynRef,CFSTR("State:/Network/Global/IPv4"));
CFStringRef primaryserviceid = CFDictionaryGetValue(ipv4key,CFSTR("PrimaryService"));
CFStringRef primaryservicepath = CFStringCreateWithFormat(NULL,NULL,CFSTR("State:/Network/Service/%@/DNS"),primaryserviceid);
CFDictionaryRef dnskey = SCDynamicStoreCopyValue(dynRef,primaryservicepath);

//create new values
CFMutableDictionaryRef newdnskey = CFDictionaryCreateMutableCopy(NULL,0,dnskey);
CFDictionarySetValue(newdnskey,CFSTR("DomainName"),CFSTR("example.com"));

CFMutableArrayRef dnsserveraddresses = CFArrayCreateMutable(NULL,0,NULL);
CFArrayAppendValue(dnsserveraddresses, CFSTR("8.8.8.8"));
CFArrayAppendValue(dnsserveraddresses, CFSTR("4.2.2.2"));
CFDictionarySetValue(newdnskey, CFSTR("ServerAddresses"), dnsserveraddresses);

//set values
bool success = SCDynamicStoreSetValue(dynRef, primaryservicepath, newdnskey);

//clean up
CFRelease(dynRef);
CFRelease(primaryservicepath);
CFRelease(dnskey);
CFRelease(dnsserveraddresses);
CFRelease(newdnskey);
}

I recently had the same issue. I posted my solution here:

http://blog.notampering.com/

Here's the snippet... hope it helps.

#include <stdio.h>
#include <SystemConfiguration/SCPreferences.h>
#include <SystemConfiguration/SCDynamicStore.h>


int main (int argc, const char * argv[])
{
    //get current values
    SCDynamicStoreRef dynRef=SCDynamicStoreCreate(kCFAllocatorSystemDefault, CFSTR("iked"), NULL, NULL);
CFDictionaryRef ipv4key = SCDynamicStoreCopyValue(dynRef,CFSTR("State:/Network/Global/IPv4"));
CFStringRef primaryserviceid = CFDictionaryGetValue(ipv4key,CFSTR("PrimaryService"));
CFStringRef primaryservicepath = CFStringCreateWithFormat(NULL,NULL,CFSTR("State:/Network/Service/%@/DNS"),primaryserviceid);
CFDictionaryRef dnskey = SCDynamicStoreCopyValue(dynRef,primaryservicepath);

//create new values
CFMutableDictionaryRef newdnskey = CFDictionaryCreateMutableCopy(NULL,0,dnskey);
CFDictionarySetValue(newdnskey,CFSTR("DomainName"),CFSTR("example.com"));

CFMutableArrayRef dnsserveraddresses = CFArrayCreateMutable(NULL,0,NULL);
CFArrayAppendValue(dnsserveraddresses, CFSTR("8.8.8.8"));
CFArrayAppendValue(dnsserveraddresses, CFSTR("4.2.2.2"));
CFDictionarySetValue(newdnskey, CFSTR("ServerAddresses"), dnsserveraddresses);

//set values
bool success = SCDynamicStoreSetValue(dynRef, primaryservicepath, newdnskey);

//clean up
CFRelease(dynRef);
CFRelease(primaryservicepath);
CFRelease(dnskey);
CFRelease(dnsserveraddresses);
CFRelease(newdnskey);
}
dawn曙光 2024-11-10 08:55:45

shell脚本版本记录如下: http://osxdaily.com/2015/06/02/change-dns-command-line-mac-os-x/

简短版本是:

# Template:
networksetup -setdnsservers (Network Service) (DNS IP) (DNS IP) ...

# Example: set DNS for Wi-Fi to 8.8.8.8  8.8.4.4  1.1.1.1
sudo networksetup -setdnsservers Wi-Fi  8.8.8.8  8.8.4.4  1.1.1.1

# Example: Clear the manually assigned DNS so that the default values can take over
sudo networksetup -setdnsservers Wi-Fi  Empty

The shell-script version is documented here: http://osxdaily.com/2015/06/02/change-dns-command-line-mac-os-x/

The short version is:

# Template:
networksetup -setdnsservers (Network Service) (DNS IP) (DNS IP) ...

# Example: set DNS for Wi-Fi to 8.8.8.8  8.8.4.4  1.1.1.1
sudo networksetup -setdnsservers Wi-Fi  8.8.8.8  8.8.4.4  1.1.1.1

# Example: Clear the manually assigned DNS so that the default values can take over
sudo networksetup -setdnsservers Wi-Fi  Empty
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文