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

fix: use the ip command #84

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 42 additions & 21 deletions bin/detect_ips.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,59 @@ if [ -z "$PUBLIC_IP" ]; then
PUBLIC_IP=`dig -4 TXT +short o-o.myaddr.l.google.com @ns1.google.com | sed 's|"||g'`
fi

runOnMac=false
int2ip() { printf ${2+-v} $2 "%d.%d.%d.%d" \
$(($1>>24)) $(($1>>16&255)) $(($1>>8&255)) $(($1&255)) ;}
ip2int() { local _a=(${1//./ }) ; printf ${2+-v} $2 "%u" $(( _a<<24 |
${_a[1]} << 16 | ${_a[2]} << 8 | ${_a[3]} )) ;}
while IFS=$' :\t\r\n' read a b c d; do
[ "$a" = "usage" ] && [ "$b" = "route" ] && runOnMac=true
if $runOnMac ;then

case "$(uname -s)" in
Darwin*) runOnMac=true ;;
*) runOnMac=false ;;
esac

if $runOnMac; then
while IFS=$' :\t\r\n' read a b c d; do
case $a in
gateway ) gWay=$b ;;
interface ) iFace=$b ;;
esac
else
[ "$a" = "0.0.0.0" ] && [ "$c" = "$a" ] && iFace=${d##* } gWay=$b
fi
done < <(/sbin/route -n 2>&1 || /sbin/route -n get 0.0.0.0/0)
ip2int $gWay gw
while read lhs rhs; do
[ "$lhs" ] && {
[ -z "${lhs#*:}" ] && iface=${lhs%:}
[ "$lhs" = "inet" ] && [ "$iface" = "$iFace" ] && {
mask=${rhs#*netmask }
mask=${mask%% *}
[ "$mask" ] && [ -z "${mask%0x*}" ] &&
printf -v mask %u $mask ||
ip2int $mask mask
ip2int ${rhs%% *} ip
done < <(/sbin/route -n get 0.0.0.0/0)
ip2int $gWay gw

while read lhs rhs; do
[ "$lhs" ] && {
[ -z "${lhs#*:}" ] && iface=${lhs%:}
[ "$lhs" = "inet" ] && [ "$iface" = "$iFace" ] && {
mask=${rhs#*netmask }
mask=${mask%% *}
[ "$mask" ] && [ -z "${mask%0x*}" ] &&
printf -v mask %u $mask ||
ip2int $mask mask
ip2int ${rhs%% *} ip
(( ( ip & mask ) == ( gw & mask ) )) &&
int2ip $ip myIp && int2ip $mask netMask
}
}
done < <(/sbin/ifconfig)
else
while IFS=$' :\t\r\n' read a b c d e rest; do
gWay=$c
iFace=$e
done < <(ip -4 -c=never route show default)
BoboTiG marked this conversation as resolved.
Show resolved Hide resolved
ip2int $gWay gw

while IFS=$' :\t\r\n' read iface state rhs; do
[ "$iface" = "$iFace" ] && {
ip2int "$(echo $rhs | cut -d '/' -f1)" ip
while IFS=$' :\t\r\n' read before netmask after; do
mask=$netmask
done < <(/usr/bin/ipcalc -n -b $rhs | grep Netmask)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code will convert IP/24 to 255.255.255.0.

ip2int $mask mask
(( ( ip & mask ) == ( gw & mask ) )) &&
int2ip $ip myIp && int2ip $mask netMask
}
}
done < <(/sbin/ifconfig)
done < <(ip -4 -br addr)
fi

echo "KADCAST_PUBLIC_ADDRESS=$PUBLIC_IP:9000"
if [ -z "$myIp" ]; then
Expand Down
2 changes: 1 addition & 1 deletion node-installer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ echo "Checking prerequisites"
update_pkg_database
check_installed unzip unzip
check_installed curl curl
check_installed ipcalc ipcalc
check_installed jq jq
check_installed route net-tools
check_installed logrotate logrotate
check_installed dig dnsutils

Expand Down