| 1 | #!/bin/bash |
| 2 | # Authors: Steven Shiau <steven _at_ clonezilla org>, Ceasar Sun <ceasar _at_ nchc org tw> |
| 3 | # License: GPL |
| 4 | |
| 5 | # Load DRBL setting and functions |
| 6 | DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}" |
| 7 | |
| 8 | . $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions |
| 9 | |
| 10 | # alias level |
| 11 | #ip_alias_list="0 1 2 3 4 5" |
| 12 | |
| 13 | usage() { |
| 14 | echo "Usage:" |
| 15 | echo "To get the up network devices or IP address:" |
| 16 | echo "`basename $0` [OPTION]" |
| 17 | echo " Options:" |
| 18 | echo " -c, --drbl-client-eth-port: Show the eth ports connected to DRBL clients (Used in DRBL server)" |
| 19 | echo " -b, --drbl-client-ip: Show the IP addresses connected to DRBL clients (Used in DRBL server)" |
| 20 | echo " -d, --all-net-dev: Show only the up network devices, including alias IP address device." |
| 21 | echo " -p, --public-ip-port: Show only the public IP address ethernet port." |
| 22 | echo " -t, --private-ip-port: Show only the private IP address ethernet port." |
| 23 | echo " -a, --public-ip-address: Show only the public IP address." |
| 24 | echo " -r, --private-ip-address: Show only the private IP address." |
| 25 | echo " -i, --all-ip-address: Show all the IP addresses." |
| 26 | echo " -g, --gateway-ip-add: Show the gateway IP address" |
| 27 | echo " -u, --uplink-eth-port: Show the uplink eth port (Used in DRBL server)" |
| 28 | echo " -v, --verbose prints out verbose information" |
| 29 | } |
| 30 | # |
| 31 | get_uplink_and_client_eth_port() { |
| 32 | uplink_eth_port="$(LC_ALL=C ip route show default 2>/dev/null | awk '/^default/ {for(i=1;i<=NF;i++) if($i=="dev") print $(i+1)}' | sort | head -n 1)" |
| 33 | # //NOTE// what if ${NETDEV[@]:0} is "eth0 eth0:1", then |
| 34 | # cat "eth0 eth0:1" | sed -e "s/eth0//g" |
| 35 | # will give eth_ports_for_drbl_clients=":1". WRONG! |
| 36 | # //WRONG// eth_ports_for_drbl_clients="$(echo ${NETDEV[@]:0} | sed -e "s/$uplink_eth_port//g")" |
| 37 | eth_ports_for_drbl_clients= |
| 38 | for i in ${NETDEV[@]:0}; do |
| 39 | # Exclude $uplink_eth_port |
| 40 | [ "$i" = "$uplink_eth_port" ] && continue |
| 41 | eth_ports_for_drbl_clients="$eth_ports_for_drbl_clients $i" |
| 42 | done |
| 43 | if [ -z "$eth_ports_for_drbl_clients" ]; then |
| 44 | # If by the above method eth_ports_for_drbl_clients is nothing, then the server must be with only 1 NIC. |
| 45 | eth_ports_for_drbl_clients="$uplink_eth_port" |
| 46 | fi |
| 47 | gateway_IP_addr="$(LC_ALL=C ip route show default 2>/dev/null | awk '/^default/ {for(i=1;i<=NF;i++) if($i=="via") print $(i+1)}' | sort | head -n 1)" |
| 48 | } # end of get_uplink_and_client_eth_port |
| 49 | |
| 50 | show_mode="" |
| 51 | # Parse command-line options |
| 52 | while [ $# -gt 0 ]; do |
| 53 | case "$1" in |
| 54 | -a|--public-ip-address) shift; show_mode="public_ip_address" ;; |
| 55 | -d|--all-net-dev) shift; show_mode="all_net_dev" ;; |
| 56 | -i|--all-ip-address) shift; show_mode="all_ip_address" ;; |
| 57 | -p|--public-ip-port) shift; show_mode="public_ip_eth_port" ;; |
| 58 | -r|--private-ip-address) shift; show_mode="private_ip_address" ;; |
| 59 | -t|--private-ip-port) shift; show_mode="private_ip_eth_port" ;; |
| 60 | -u|--uplink-eth-port) shift; show_mode="uplink_eth_port" ;; |
| 61 | -g|--gateway-ip-add) shift; show_mode="gateway_ip_address" ;; |
| 62 | -c|--drbl-client-eth-port) shift; show_mode="drbl_client_eth_port" ;; |
| 63 | -b|--drbl-client-ip) shift; show_mode="drbl_client_ip" ;; |
| 64 | -v|--verbose) shift; VERBOSE="on" ;; |
| 65 | -*) echo "${0}: ${1}: invalid option" >&2 |
| 66 | usage >& 2 |
| 67 | exit 2 ;; |
| 68 | *) break ;; |
| 69 | esac |
| 70 | done |
| 71 | |
| 72 | #NDVS="$(get-nic-devs)" |
| 73 | # For physical NIC, the format is like: |
| 74 | # eth0 Link encap:Ethernet HWaddr 00:11:aa:bb:cc:dd |
| 75 | # For ppp0 NIC, it's like: |
| 76 | # ppp0 Link encap:Point-to-Point Protocol |
| 77 | # Ref: http://sourceforge.net/projects/drbl/forums/forum/394008/topic/3855959 |
| 78 | # For Fedora 17, the output of "ifconfig -a" is like (//Note// An extra ":"): |
| 79 | # eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 |
| 80 | # inet 192.168.120.192 netmask 255.255.255.0 broadcast 192.168.120.255 |
| 81 | # inet6 fe80::20c:29ff:fe97:5d0f prefixlen 64 scopeid 0x20<link> |
| 82 | # ether 00:0c:29:97:5d:0f txqueuelen 1000 (Ethernet) |
| 83 | # RX packets 1998 bytes 179313 (175.1 KiB) |
| 84 | # RX errors 0 dropped 0 overruns 0 frame 0 |
| 85 | # TX packets 1305 bytes 198243 (193.5 KiB) |
| 86 | # TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 |
| 87 | # //NOTE// for alias NIC, it's like: |
| 88 | # For traditional one (e.g. Ubuntu 12.10): |
| 89 | # eth1:1 Link encap:Ethernet HWaddr 00:0c:29:12:72:60 |
| 90 | # inet addr:192.168.37.254 Bcast:192.168.37.255 Mask:255.255.255.0 |
| 91 | # UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 |
| 92 | # For Fedora 17: |
| 93 | # eth1:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 |
| 94 | # inet 192.168.61.254 netmask 255.255.255.0 broadcast 192.168.61.255 |
| 95 | # ether 00:0c:29:e9:f2:4e txqueuelen 1000 (Ethernet) |
| 96 | # device interrupt 16 base 0x2080 |
| 97 | # Therefore we have to put [[:space:]]+ after ^[[:alnum:]]+[[:digit:]]+\:. |
| 98 | |
| 99 | NDVS="$(LC_ALL=C ip -o link show | awk -F': ' '{print $2}' | cut -d@ -f1 | grep -v "lo")" |
| 100 | #NDVS="$NDVS $(LC_ALL=C ifconfig -a | grep -iE '(^[[:alnum:]]+[[:digit:]]+(:[[:digit:]]+|)\:[[:space:]]+)'| awk -F': ' '{print $1}')" |
| 101 | NDVS="$NDVS $(LC_ALL=C get-nic-devs | xargs echo)" |
| 102 | NDVS="$(echo $NDVS | tr ' ' '\n' | awk '!x[$0]++' | xargs echo)" |
| 103 | # Exclude some device if it's assigned in drbl.conf. |
| 104 | if [ -n "$exclude_eth_nics" ]; then |
| 105 | for exn in $exclude_eth_nics; do |
| 106 | NDVS="$(LC_ALL=C echo $NDVS | sed -r -e "s/\b$exn\b//g")" |
| 107 | done |
| 108 | fi |
| 109 | # |
| 110 | i=0 |
| 111 | for idev in $NDVS; do |
| 112 | # physical network device IP |
| 113 | IP="$(LC_ALL=C drbl-get-ipadd $idev)" |
| 114 | if [ -n "$IP" ]; then |
| 115 | i=$((i+1)) |
| 116 | NETDEV[$i]="$idev" |
| 117 | IPADDRS[$i]="$IP" |
| 118 | fi |
| 119 | # try alias IP |
| 120 | #for id in $ip_alias_list; do |
| 121 | # IP="$(drbl-get-ipadd $idev:$id)" |
| 122 | # if [ -n "$IP" ]; then |
| 123 | # i=$((i+1)) |
| 124 | # NETDEV[$i]="$idev:$id" |
| 125 | # IPADDRS[$i]="$IP" |
| 126 | # fi |
| 127 | #done |
| 128 | done |
| 129 | imax=$i |
| 130 | |
| 131 | # found the public IP and ethx |
| 132 | i=0 |
| 133 | public_IP_port= |
| 134 | public_IP_addr= |
| 135 | private_IP_port= |
| 136 | private_IP_addr= |
| 137 | while [ $i -le $imax ]; do |
| 138 | i=$((i+1)) |
| 139 | if [ -n "$(echo ${IPADDRS[$i]} | grep -vE "^(192.168\..*|172\.(1[6-9]|2[0-9]|3[01])\..*|10\..*)")" ]; then |
| 140 | public_IP_port="$public_IP_port ${NETDEV[$i]}" |
| 141 | public_IP_addr="$public_IP_addr ${IPADDRS[$i]}" |
| 142 | else |
| 143 | private_IP_port="$private_IP_port ${NETDEV[$i]}" |
| 144 | private_IP_addr="$private_IP_addr ${IPADDRS[$i]}" |
| 145 | fi |
| 146 | done |
| 147 | |
| 148 | # Find the uplink ethernet port. This is only useful in DRBL server |
| 149 | case "$show_mode" in |
| 150 | uplink_eth_port|drbl_client_eth_port|gateway_ip_address) get_uplink_and_client_eth_port ;; |
| 151 | drbl_client_ip) |
| 152 | get_uplink_and_client_eth_port |
| 153 | drbl_client_ip="" |
| 154 | for idev in $eth_ports_for_drbl_clients; do |
| 155 | IP="$(LC_ALL=C drbl-get-ipadd $idev)" |
| 156 | [ -n "$IP" ] && drbl_client_ip="$drbl_client_ip $IP" |
| 157 | done |
| 158 | ;; |
| 159 | esac |
| 160 | |
| 161 | case "$show_mode" in |
| 162 | all_ip_address) echo ${IPADDRS[@]:0} ;; |
| 163 | all_net_dev) echo ${NETDEV[@]:0} ;; |
| 164 | public_ip_address) echo $public_IP_addr ;; |
| 165 | public_ip_eth_port) echo $public_IP_port ;; |
| 166 | private_ip_address) echo $private_IP_addr ;; |
| 167 | private_ip_eth_port) echo $private_IP_port ;; |
| 168 | uplink_eth_port) echo $uplink_eth_port ;; |
| 169 | drbl_client_eth_port) echo $eth_ports_for_drbl_clients ;; |
| 170 | drbl_client_ip) echo $drbl_client_ip ;; |
| 171 | gateway_ip_address) echo $gateway_IP_addr ;; |
| 172 | esac |