File: get-all-nic-ip

package info (click to toggle)
drbl 5.8.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,996 kB
  • sloc: sh: 43,555; perl: 8,820; xml: 867; makefile: 135
file content (171 lines) | stat: -rwxr-xr-x 6,935 bytes parent folder | download | duplicates (4)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/bin/bash
# Authors: Steven Shiau <steven _at_ clonezilla org>, Ceasar Sun <ceasar _at_ nchc org tw>
# License: GPL

# Load DRBL setting and functions
DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}"

. $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions

# alias level
#ip_alias_list="0 1 2 3 4 5"

usage() {
    echo "Usage:"
    echo "To get the up network devices or IP address:"
    echo "`basename $0` [OPTION]"
    echo " Options:"
    echo " -c, --drbl-client-eth-port: Show the eth ports connected to DRBL clients (Used in DRBL server)"
    echo " -b, --drbl-client-ip: Show the IP addresses connected to DRBL clients (Used in DRBL server)"
    echo " -d, --all-net-dev:  Show only the up network devices, including alias IP address device."
    echo " -p, --public-ip-port:  Show only the public IP address ethernet port."
    echo " -t, --private-ip-port: Show only the private IP address ethernet port."
    echo " -a, --public-ip-address:  Show only the public IP address."
    echo " -r, --private-ip-address: Show only the private IP address."
    echo " -i, --all-ip-address:  Show all the IP addresses."
    echo " -g, --gateway-ip-add:      Show the gateway IP address"
    echo " -u, --uplink-eth-port:     Show the uplink eth port (Used in DRBL server)"
    echo " -v, --verbose           prints out verbose information"
}
#
get_uplink_and_client_eth_port() {
  uplink_eth_port="$(LC_ALL=C route -n | awk '/^0.0.0.0/ {print $8}' | sort | head -n 1)"
  # //NOTE// what if ${NETDEV[@]:0} is "eth0 eth0:1", then 
  # cat "eth0 eth0:1" | sed -e "s/eth0//g" 
  # will give eth_ports_for_drbl_clients=":1". WRONG!
  # //WRONG// eth_ports_for_drbl_clients="$(echo ${NETDEV[@]:0} | sed -e "s/$uplink_eth_port//g")"
  eth_ports_for_drbl_clients=
  for i in ${NETDEV[@]:0}; do
    # Exclude $uplink_eth_port
    [ "$i" = "$uplink_eth_port" ] && continue
    eth_ports_for_drbl_clients="$eth_ports_for_drbl_clients $i" 
  done
  if [ -z "$eth_ports_for_drbl_clients" ]; then
    # If by the above method eth_ports_for_drbl_clients is nothing, then the server must be with only 1 NIC.
    eth_ports_for_drbl_clients="$uplink_eth_port"
  fi
  gateway_IP_addr="$(LC_ALL=C route -n | awk '/^0.0.0.0/ {print $2}' | sort | head -n 1)"
} # end of get_uplink_and_client_eth_port

show_mode=""
# Parse command-line options
while [ $# -gt 0 ]; do
  case "$1" in
    -a|--public-ip-address) shift; show_mode="public_ip_address" ;;
    -d|--all-net-dev) shift; show_mode="all_net_dev" ;;
    -i|--all-ip-address) shift; show_mode="all_ip_address" ;;
    -p|--public-ip-port) shift; show_mode="public_ip_eth_port" ;;
    -r|--private-ip-address) shift; show_mode="private_ip_address" ;;
    -t|--private-ip-port) shift; show_mode="private_ip_eth_port" ;;
    -u|--uplink-eth-port) shift; show_mode="uplink_eth_port" ;;
    -g|--gateway-ip-add) shift; show_mode="gateway_ip_address" ;;
    -c|--drbl-client-eth-port) shift; show_mode="drbl_client_eth_port" ;;
    -b|--drbl-client-ip) shift; show_mode="drbl_client_ip" ;;
    -v|--verbose) shift; VERBOSE="on" ;;
    -*)     echo "${0}: ${1}: invalid option" >&2
            usage >& 2
            exit 2 ;;
    *)      break ;;
  esac
done

#NDVS="$(get-nic-devs)"
# For physical NIC, the format is like:
# eth0      Link encap:Ethernet  HWaddr 00:11:aa:bb:cc:dd  
# For ppp0 NIC, it's like:
# ppp0 Link encap:Point-to-Point Protocol
# Ref: http://sourceforge.net/projects/drbl/forums/forum/394008/topic/3855959
# For Fedora 17, the output of "ifconfig -a" is like (//Note// An extra ":"):
# eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
#       inet 192.168.120.192  netmask 255.255.255.0  broadcast 192.168.120.255
#       inet6 fe80::20c:29ff:fe97:5d0f  prefixlen 64  scopeid 0x20<link>
#       ether 00:0c:29:97:5d:0f  txqueuelen 1000  (Ethernet)
#       RX packets 1998  bytes 179313 (175.1 KiB)
#       RX errors 0  dropped 0  overruns 0  frame 0
#       TX packets 1305  bytes 198243 (193.5 KiB)
#       TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
# //NOTE// for alias NIC, it's like:
# For traditional one (e.g. Ubuntu 12.10):
# eth1:1   Link encap:Ethernet  HWaddr 00:0c:29:12:72:60
#          inet addr:192.168.37.254  Bcast:192.168.37.255  Mask:255.255.255.0
#          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
# For Fedora 17:
# eth1:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
#        inet 192.168.61.254  netmask 255.255.255.0  broadcast 192.168.61.255
#        ether 00:0c:29:e9:f2:4e  txqueuelen 1000  (Ethernet)
#        device interrupt 16  base 0x2080
# Therefore we have to put [[:space:]]+ after ^[[:alnum:]]+[[:digit:]]+\:.

NDVS="$(LC_ALL=C ifconfig -a | grep -iE '(HWaddr|encap:Point-to-Point Protocol)'| awk -F' ' '{print $1}')"
#NDVS="$NDVS $(LC_ALL=C ifconfig -a | grep -iE '(^[[:alnum:]]+[[:digit:]]+(:[[:digit:]]+|)\:[[:space:]]+)'| awk -F': ' '{print $1}')"
NDVS="$NDVS $(LC_ALL=C get-nic-devs | xargs echo)"
# Exclude some device if it's assigned in drbl.conf.
if [ -n "$exclude_eth_nics" ]; then
  for exn in $exclude_eth_nics; do
    NDVS="$(LC_ALL=C echo $NDVS | sed -r -e "s/\b$exn\b//g")"
  done
fi
#
i=0
for idev in $NDVS; do
  # physical network device IP
  IP="$(LC_ALL=C drbl-get-ipadd $idev)"
  if [ -n "$IP" ]; then
    i=$((i+1))
    NETDEV[$i]="$idev"
    IPADDRS[$i]="$IP"
  fi
  # try alias IP
  #for id in $ip_alias_list; do
  #  IP="$(drbl-get-ipadd $idev:$id)"
  #  if [ -n "$IP" ]; then
  #    i=$((i+1))
  #    NETDEV[$i]="$idev:$id"
  #    IPADDRS[$i]="$IP"
  #  fi
  #done
done
imax=$i

# found the public IP and ethx
i=0
public_IP_port=
public_IP_addr=
private_IP_port=
private_IP_addr=
while [ $i -le $imax ]; do
  i=$((i+1))
  if [ -n "$(echo ${IPADDRS[$i]} | grep -vE "^(192.168\..*|172\.(1[6-9]|2[0-9]|3[01])\..*|10\..*)")" ]; then
    public_IP_port="$public_IP_port ${NETDEV[$i]}"
    public_IP_addr="$public_IP_addr ${IPADDRS[$i]}"
  else
    private_IP_port="$private_IP_port ${NETDEV[$i]}"
    private_IP_addr="$private_IP_addr ${IPADDRS[$i]}"
  fi
done

# Find the uplink ethernet port. This is only useful in DRBL server
case "$show_mode" in
  uplink_eth_port|drbl_client_eth_port|gateway_ip_address) get_uplink_and_client_eth_port ;;
  drbl_client_ip)
     get_uplink_and_client_eth_port
     drbl_client_ip=""
     for idev in $eth_ports_for_drbl_clients; do
      IP="$(LC_ALL=C drbl-get-ipadd $idev)"
      [ -n "$IP" ] && drbl_client_ip="$drbl_client_ip $IP"
     done
   ;;
esac

case "$show_mode" in
  all_ip_address) echo ${IPADDRS[@]:0} ;;
  all_net_dev) echo ${NETDEV[@]:0}  ;;
  public_ip_address) echo $public_IP_addr ;;
  public_ip_eth_port) echo $public_IP_port ;;
  private_ip_address) echo $private_IP_addr ;;
  private_ip_eth_port) echo $private_IP_port ;;
  uplink_eth_port) echo $uplink_eth_port ;;
  drbl_client_eth_port) echo $eth_ports_for_drbl_clients ;;
  drbl_client_ip)  echo $drbl_client_ip ;;
  gateway_ip_address) echo $gateway_IP_addr ;;
esac