user1983395 user1983395 - 2 months ago 10
R Question

Function for retrieving own ip address from within R?

Does anyone know of an R function that is able to retrieve ones own ip-address (of the pc you're working on)? It would be very helpful! Many thanks in advance.

Answer

You can issue a system() command to your operating system:

  • In Windows you can use ipconfig
  • In Linux, use ifconfig

For example, on Windows try calling system() with the argument intern=TRUE to return the results to R:

x <- system("ipconfig", intern=TRUE)

This returns:

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  . : "                            

Now you can use grep to find the lines with IPv4:

x[grep("IPv4", x)]
[1] "   IPv4 Address. . . . . . . . . . . : 10.201.120.184"
[2] "   IPv4 Address. . . . . . . . . . . : 10.201.120.157"

And to extract just the ip address:

z <- x[grep("IPv4", x)]
gsub(".*? ([[:digit:]])", "\\1", z)
"10.201.120.184" "10.201.120.157"