Function for retrieving own ip address from within R?
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"
How to get client IP address in R Plumber
To close out the question, I'll restate the comment:
Since plumber uses
httpuv
, it's possible you can reach thereq$REMOTE_ADDR
property of the request handle.
Get local IP address in Node.js
This information can be found in os.networkInterfaces()
, — an object, that maps network interface names to its properties (so that one interface can, for example, have several addresses):
'use strict';
const { networkInterfaces } = require('os');
const nets = networkInterfaces();
const results = Object.create(null); // Or just '{}', an empty object
for (const name of Object.keys(nets)) {
for (const net of nets[name]) {
// Skip over non-IPv4 and internal (i.e. 127.0.0.1) addresses
// 'IPv4' is in Node <= 17, from 18 it's a number 4 or 6
const familyV4Value = typeof net.family === 'string' ? 'IPv4' : 4
if (net.family === familyV4Value && !net.internal) {
if (!results[name]) {
results[name] = [];
}
results[name].push(net.address);
}
}
}
// 'results'
{
"en0": [
"192.168.1.101"
],
"eth0": [
"10.0.0.101"
],
"<network name>": [
"<ip>",
"<ip alias>",
"<ip alias>",
...
]
}
// results["en0"][0]
"192.168.1.101"
Related Topics
Sources on S4 Objects, Methods and Programming in R
Do I Need to Normalize (Or Scale) Data for Randomforest (R Package)
Does the Ternary Operator Exist in R
Ggplot: How to Increase Spacing Between Faceted Plots
Compare Two Character Vectors in R
Coding Practice in R:What Are the Advantages and Disadvantages of Different Styles
Avoid Wasting Space When Placing Multiple Aligned Plots Onto One Page
Recommended Package for Very Large Dataset Processing and MAChine Learning in R
Easier Way to Plot the Cumulative Frequency Distribution in Ggplot
How to Create, Structure, Maintain and Update Data Codebooks in R
Plot Size and Resolution with R Markdown, Knitr, Pandoc, Beamer
Error in Fetch(Key):Lazy-Load Database
R Reading in a Zip Data File Without Unzipping It
Transform Only One Axis to Log10 Scale with Ggplot2
How to Find Row Number of a Value in R Code