-
Notifications
You must be signed in to change notification settings - Fork 62
/
yf_get_ip_address.function
37 lines (37 loc) · 2.57 KB
/
yf_get_ip_address.function
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
34
35
36
37
#! /bin/true
# vi: set tabstop=4 syntax=sh : # colorize it in 'vi' and use shorter tabstops
# SPDX-License-Identifier: GPL-2.0-or-later
#######################################################################################
# #
# get_ip_address - retrieve the IPv4 addresses for the specified interface #
# #
# parameters: #
# $1 - interface to be used #
# #
# stdout: #
# a list of IPv4 addresses available at the interface #
# #
# return code: #
# 0 - stdout is valid and contains at least one entry #
# 1 - the interface name is invalid or the interface has no IPv4 address assigned #
# #
# WARNING: #
# This script needs a 'ip' binary, which isn't part of POSIX standards. #
# #
#######################################################################################
# #
# U: ip sed printf #
# W: - #
# F: - #
# I: - #
# K: network #
# #
#######################################################################################
yf_get_ip_address()
(
intf="$1"
addrs="$(ip -o -f inet addr show dev $intf 2>/dev/null | sed -n -e "s|^[0-9]*: $intf.*inet \([0-9./]*\).*|\1|p")"
[ ${#addrs} -eq 0 ] && return 1
printf "$addrs"
return 0
)