rdatasculptor
rdatasculptor

Reputation: 8447

Function for retrieving own ip address from within R?

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

Upvotes: 29

Views: 14424

Answers (4)

ayush varshney
ayush varshney

Reputation: 537

Though @andrie explained it in very layman language and i am sure it helped us a lot to understand the functionality of it.

So from there only sharing a one liner code without installing any other package.

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

Hope this would be helpful!

Upvotes: 2

Greg
Greg

Reputation: 346

I recently created a minimal package using ipify.org to do this exact thing.

Usage is easy, you can install using devtools and github.

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

once installed, its as easy as loading the library and one function call...

library(ipify) get_ip()

Upvotes: 10

Ansjovis86
Ansjovis86

Reputation: 1555

This retrieves exactly what you want:

system('ipconfig getifaddr en0')

192.168.1.73

Upvotes: -5

Andrie
Andrie

Reputation: 179468

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"

Upvotes: 42

Related Questions