Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrieve local ip independently #888

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

merlinrabens
Copy link

Since MacOS doesn't necessarily name the cabled network interface en0 the localip command fails when WI-FI is down. This PR should fix this issue and resolve the local ip properly when connected via cable.

@blaues0cke
Copy link

In my case this then returns the ip address of a random virtual machine:

image

This is what my ifconfig -a returns:

lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
	options=1203<RXCSUM,TXCSUM,TXSTATUS,SW_TIMESTAMP>
	inet 127.0.0.1 netmask 0xff000000
	inet6 ::1 prefixlen 128
	inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1
	nd6 options=201<PERFORMNUD,DAD>
gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
stf0: flags=0<> mtu 1280
XHC20: flags=0<> mtu 0
VHC128: flags=0<> mtu 0
XHC1: flags=0<> mtu 0
XHC0: flags=0<> mtu 0
XHC3: flags=0<> mtu 0
XHC2: flags=0<> mtu 0
en7: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
	options=b<RXCSUM,TXCSUM,VLAN_HWTAGGING>
	ether 58:ef:68:e7:2f:8c
	nd6 options=201<PERFORMNUD,DAD>
	media: autoselect (<unknown type>)
	status: inactive
en5: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
	ether ac:de:48:00:11:22
	inet6 fe80::aede:48ff:fe00:1122%en5 prefixlen 64 scopeid 0xb
	nd6 options=201<PERFORMNUD,DAD>
	media: autoselect (100baseTX <full-duplex>)
	status: active
ap1: flags=8802<BROADCAST,SIMPLEX,MULTICAST> mtu 1500
	ether 3a:f9:d3:d5:89:4c
	media: autoselect
	status: inactive
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
	ether 38:f9:d3:d5:89:4c
	inet6 fe80::1058:3f6:aeb3:d24c%en0 prefixlen 64 secured scopeid 0xd
	inet 192.168.0.226 netmask 0xffffff00 broadcast 192.168.0.255
	inet6 2a02:8070:e287:e800:41e:3b99:ce56:5644 prefixlen 64 autoconf secured
	inet6 2a02:8070:e287:e800:20c7:7639:be41:b758 prefixlen 64 autoconf temporary
	nd6 options=201<PERFORMNUD,DAD>
	media: autoselect
	status: active
p2p0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 2304
	ether 0a:f9:d3:d5:89:4c
	media: autoselect
	status: inactive
awdl0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1484
	ether 4e:de:3e:bb:9d:12
	inet6 fe80::4cde:3eff:febb:9d12%awdl0 prefixlen 64 scopeid 0xf
	nd6 options=201<PERFORMNUD,DAD>
	media: autoselect
	status: active
en1: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
	options=60<TSO4,TSO6>
	ether fa:00:a4:69:8d:01
	media: autoselect <full-duplex>
	status: inactive
en2: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
	options=60<TSO4,TSO6>
	ether fa:00:a4:69:8d:00
	media: autoselect <full-duplex>
	status: inactive
en3: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
	options=60<TSO4,TSO6>
	ether fa:00:a4:69:8d:05
	media: autoselect <full-duplex>
	status: inactive
en4: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
	options=60<TSO4,TSO6>
	ether fa:00:a4:69:8d:04
	media: autoselect <full-duplex>
	status: inactive
bridge0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
	options=63<RXCSUM,TXCSUM,TSO4,TSO6>
	ether fa:00:a4:69:8d:01
	Configuration:
		id 0:0:0:0:0:0 priority 0 hellotime 0 fwddelay 0
		maxage 0 holdcnt 0 proto stp maxaddr 100 timeout 1200
		root id 0:0:0:0:0:0 priority 0 ifcost 0 port 0
		ipfilter disabled flags 0x2
	member: en1 flags=3<LEARNING,DISCOVER>
	        ifmaxaddr 0 port 16 priority 0 path cost 0
	member: en2 flags=3<LEARNING,DISCOVER>
	        ifmaxaddr 0 port 17 priority 0 path cost 0
	member: en3 flags=3<LEARNING,DISCOVER>
	        ifmaxaddr 0 port 18 priority 0 path cost 0
	member: en4 flags=3<LEARNING,DISCOVER>
	        ifmaxaddr 0 port 19 priority 0 path cost 0
	nd6 options=201<PERFORMNUD,DAD>
	media: <unknown type>
	status: inactive
utun0: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 2000
	inet6 fe80::bacc:13c4:63ad:e348%utun0 prefixlen 64 scopeid 0x15
	nd6 options=201<PERFORMNUD,DAD>
vboxnet0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
	ether 0a:00:27:00:00:00
	inet 192.168.99.1 netmask 0xffffff00 broadcast 192.168.99.255
utun1: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 1380
	inet6 fe80::f35a:3822:4c6e:2d62%utun1 prefixlen 64 scopeid 0x17
	nd6 options=201<PERFORMNUD,DAD>
utun2: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 1380
	inet6 fe80::348c:c953:6dad:2dc1%utun2 prefixlen 64 scopeid 0x18
	nd6 options=201<PERFORMNUD,DAD>
utun3: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 1380
	inet6 fe80::a633:8b30:5941:edd5%utun3 prefixlen 64 scopeid 0x19
	nd6 options=201<PERFORMNUD,DAD>
utun4: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 1380
	inet6 fe80::d6db:a371:6420:aada%utun4 prefixlen 64 scopeid 0x1a
	nd6 options=201<PERFORMNUD,DAD>

@merlinrabens
Copy link
Author

@blaues0cke Thanks for pointing this out! Let's find a better solution then. May I kindly ask you what

ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p'

returns in your case?

@rprimus
Copy link

rprimus commented Aug 18, 2019

Sun Aug 18 15:14:40 BST 2019

I use:

ipconfig getifaddr $(netstat -rn | grep default | awk '{print $NF}' | head -1)

ifconfig | sed -En 's/127.0.0.1//;s/.inet (addr:)?(([0-9].){3}[0-9])./\2/p'

@bluedigits - the above does give the correct IP address for the active connection.

@blaues0cke
Copy link

@bluedigits this seems to work :-)

image

@merlinrabens
Copy link
Author

ipconfig getifaddr $(netstat -rn | grep default | awk '{print $NF}' | head -1)

@rprimus That works! Thanks for this great solution! :clap
@blaues0cke indeed 💃

…-localip-independently

* 'master' of github.com:bluedigits/dotfiles:
  Create git alias for comparing branches
  Try to checkout first an existing branch
  Add dg git alias
  Change some .macos defaults
@merlinrabens
Copy link
Author

I added the localip command as a function to get around the eager substitution of the alias during shell start. Otherwise you would have to call reload in case you switch from WiFi to LAN or vice-versa.

@mathiasbynens mathiasbynens changed the base branch from master to main June 16, 2020 07:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants