从R内检索自己的IP地址的功能?

问题描述 投票:19回答:4

有没有人知道一个R函数能够检索自己的IP地址(你正在处理的PC)?这将是非常有帮助的!提前谢谢了。

r
4个回答
29
投票

您可以向操作系统发出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"

5
投票

我最近使用ipify.org创建了一个最小的包来做这件事。

用法很简单,你可以使用devtools和github安装。

library(devtools) install_github("gregce/ipify")

一旦安装,它就像加载库和一个函数调用一样简单......

library(ipify) get_ip()


1
投票

虽然@andrie用非常外行的语言解释它,但我相信它帮助我们理解它的功能。

所以从那里只共享一个单行代码而不安装任何其他包。

gsub(".*? ([[:digit:]])", "\\1", system("ipconfig", intern=T)[grep("IPv4", system("ipconfig", intern = T))])

希望这会有所帮助!


-2
投票

这将准确检索您想要的内容:

system('ipconfig getifaddr en0')

192.168.1.73
© www.soinside.com 2019 - 2024. All rights reserved.