gpt4 book ai didi

r - 从R中检索自己的IP地址的功能?

转载 作者:行者123 更新时间:2023-12-03 12:35:09 25 4
gpt4 key购买 nike

有谁知道可以检索自己的 IP 地址(您正在使用的 PC 的 IP 地址)的 R 函数?这将非常有帮助!提前谢谢了。

最佳答案

您可以发出 system()命令到您的操作系统:

  • 在 Windows 中,您可以使用 ipconfig
  • 在 Linux 中,使用 ifconfig

  • 例如,在 Windows 上尝试调用 system()与参数 intern=TRUE将结果返回到 R:
    x <- system("ipconfig", intern=TRUE)

    这将返回:
    x
    [1] ""
    [2] "Windows IP Configuration"
    [3] ""
    [4] ""
    [5] "Wireless LAN adapter Wireless Network Connection:"
    [6] ""
    [7] " Connection-specific DNS Suffix . : tbglondon.local"
    [8] " Link-local IPv6 Address . . . . . : fe80::c0cb:e470:91c7:abb9%14"
    [9] " IPv4 Address. . . . . . . . . . . : 10.201.120.184"
    [10] " Subnet Mask . . . . . . . . . . . : 255.255.255.0"
    [11] " Default Gateway . . . . . . . . . : 10.201.120.253"
    [12] ""
    [13] "Ethernet adapter Local Area Connection:"
    [14] ""
    [15] " Connection-specific DNS Suffix . : tbglondon.local"
    [16] " Link-local IPv6 Address . . . . . : fe80::9d9b:c44c:fd4d:1c77%11"
    [17] " IPv4 Address. . . . . . . . . . . : 10.201.120.157"
    [18] " Subnet Mask . . . . . . . . . . . : 255.255.255.0"
    [19] " Default Gateway . . . . . . . . . : 10.201.120.253"
    [20] ""
    [21] "Tunnel adapter Local Area Connection* 13:"
    [22] ""
    [23] " Media State . . . . . . . . . . . : Media disconnected"
    [24] " Connection-specific DNS Suffix . : "
    [25] ""
    [26] "Tunnel adapter isatap.tbglondon.local:"
    [27] ""
    [28] " Media State . . . . . . . . . . . : Media disconnected"
    [29] " Connection-specific DNS Suffix . : tbglondon.local"
    [30] ""
    [31] "Tunnel adapter Teredo Tunneling Pseudo-Interface:"
    [32] ""
    [33] " Media State . . . . . . . . . . . : Media disconnected"
    [34] " Connection-specific DNS Suffix . : "

    现在您可以使用 grep找到带有 IPv4 的行:
    x[grep("IPv4", x)]
    [1] " IPv4 Address. . . . . . . . . . . : 10.201.120.184"
    [2] " IPv4 Address. . . . . . . . . . . : 10.201.120.157"

    并仅提取 ip 地址:
    z <- x[grep("IPv4", x)]
    gsub(".*? ([[:digit:]])", "\\1", z)
    "10.201.120.184" "10.201.120.157"

    关于r - 从R中检索自己的IP地址的功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14357219/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com