«IP detector» by prko

on 22 Oct'22 00:09 in ipip detector

This code outputs an array consisting of local IPs (or an array with one local IP if there is only one) except “127.0.0.1”.

It successfully detects multiple IPv4 addresses as well as one IPv4 address under macOS 12.2.1 and Windows 11. However, under Ubuntu 20.04.3, which ran over Parallels Desktop, I strangely could not assign IPv4 to the second network card. Thus, I confirmed only the output of an array with one IP, but I hope it also could output multiple IPs.

related post: https://scsynth.org/t/code-to-get-local-ip-on-all-platforms/5791/3?u=prko

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// The following strings are to avoid listing network bridges which seem to be created by Parallels Desktop:
// | grep -Fv "inet 10."  

(
~ip = { 
	var winCMD, macCMD, linCMD, ips;
	
	winCMD =  { var ipInfo; 
		ipInfo = "ipconfig | findstr /C:IPv4"
		.unixCmdGetStdOut
		.replace("   IPv4 Address. . . . . . . . . . . : ", "") };
	macCMD = { (
		"ifconfig | grep -Fv" + "inet 10.".quote + 
		"| grep" + 
		"inet ".quote + 
		"| grep -Fv 127.0.0.1 | awk '{print $2}'"
	).unixCmdGetStdOut };
	linCMD = { (
		"ifconfig | grep" + "inet ".quote +
		"| grep -Fv 127.0.0.1 | awk '{print $2}'"	
	).unixCmdGetStdOut };
	
	ips = Platform.case(
		\osx,       { macCMD.() },
		\linux,     { linCMD.() },
		\windows,   { winCMD.() }
	);
	
	ips = ips.split(Char.nl);
	ips.removeAt(ips.size-1);
	ips
}.()
)
raw 824 chars (focus & ctrl+a+c to copy)
reception
comments