Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function for retrieving own ip address from within R?

Tags:

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.

like image 611
rdatasculptor Avatar asked Jan 16 '13 11:01

rdatasculptor


People also ask

How are IP addresses obtained?

Your IP address is assigned to your device by your ISP. Your internet activity goes through the ISP, and they route it back to you, using your IP address. Since they are giving you access to the internet, it is their role to assign an IP address to your device.


Video Answer


3 Answers

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"
like image 94
Andrie Avatar answered Oct 19 '22 18:10

Andrie


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()

like image 31
Greg Avatar answered Oct 19 '22 19:10

Greg


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!

like image 3
ayush varshney Avatar answered Oct 19 '22 18:10

ayush varshney