On 2023-03-30 13:38, Emanuel Berg wrote:
fh wrote:
In my shell script, how to get the localhost's IPs (eth0 and
eth1) correctly? I know I can run 'ifconfig' and grep etc,
but it's maybe not that graceful.
Here is what I do, now idea if it's a good idea but maybe it
can help:
#! /bin/zsh
#
# this file:
# https://dataswamp.org/~incal/conf/.zsh/ip
public-ip () {
local name=$funcstack[1]
local url='http://checkip.amazonaws.com'
local open_dns=opendns.com
echo $(curl --no-progress-meter $url) "($name)"
dig +short myip.${open_dns} @resolver1.${open_dns}
}
inet () {
local name=$funcstack[1]
echo $(hostname -I | awk '{print $1}') "($name)"
ifconfig $net | awk '/mask/{print $2}'
ip addr show $net | awk '/inet /{print $2}' | cut -d '/' -f 1
}
list-ip () {
public-ip
inet
}
alias lip=list-ip
# $ lip
# 92.34.142.23
# 92.34.142.23
# 192.168.10.224
# 192.168.10.224
# 192.168.10.224
i like the JSON output just b/c I am a programmer knowing JSON pretty
well. :)