-
Notifications
You must be signed in to change notification settings - Fork 10
/
ipaddress.sh
executable file
·120 lines (100 loc) · 3.8 KB
/
ipaddress.sh
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
#!/bin/sh
#
# Written by Denilson Figueiredo de Sa <[email protected]>
#
# Future ideas:
# * Rewrite this to use iproute2 tools:
# ip link show
# ip address show
THISSCRIPT=`basename $0`
print_help() {
cat << EOF
Usage: $THISSCRIPT <interface|all|realip> [ -imnh ]
Where:
interface print info only from the interface passed as argument (e.g. eth0)
all print info from all available interfaces
realip use an external HTTP service to discover the external IP address
-i print IP address (default)
-m print netmask in 255.0.0.0 format
-n print netmask in numeric format (number of 1-bits)
-im, -in combination of above options, print both IP and netmask
-h print hardware address (MAC address)
Examples:
$THISSCRIPT all
$THISSCRIPT eth0 -in
$THISSCRIPT eth1 -h
$THISSCRIPT realip
EOF
}
if [ -z "$1" -o "$1" = "--help" -o "$1" = "-help" -o "$1" = "-h" ]; then
print_help
elif [ "$1" = "realip" ]; then
if [ -n "$2" ]; then
echo "$THISSCRIPT: 'realip' pseudo-interface does not support extra options."
exit 1
else
# Uncomment the service you want to use
wget -q -O - http://checkip.dyndns.org/ | sed -n 's/^.*Current IP Address: *\([0-9.]\+\).*$/\1/p'
# <!--Do not scrape your IP from here, go to www.whatismyip.com/faq/automation.asp for more information on our automation rules.-->
#wget -q -O - http://www.whatismyip.com/ | sed -n '/Your \+IP \+Is/{;s/^.*Your \+IP \+Is \+\([0-9.]\+\).*$/\1/p;q;}'
#wget -q -O - http://automation.whatismyip.com/n09230945.asp
# Other services:
# http://checkmyip.org/
# http://checkmyip.com/
# However, the next one shows the IP behind the proxy:
# http://www.checkmyip.net/
fi
else
# Try to locate the ifconfig command
IFCONFIG=`which ifconfig 2>/dev/null`
if [ ! -x "$IFCONFIG" ] ; then
# In case ifconfig was not found in $PATH (using which), let's try manually:
# The following line has been pre-expanded to remove this bash-ism.
#for dir in {/,/usr/{,local/}}{sbin,bin} ; do
# With this pre-expanded list, this script will also work with dash.
for dir in /sbin /bin /usr/sbin /usr/bin /usr/local/sbin /usr/local/bin ; do
if [ -x "$dir/ifconfig" ] ; then
IFCONFIG="$dir/ifconfig"
break
fi
done
fi
if [ ! -x "$IFCONFIG" ]; then
echo "$THISSCRIPT: Could not find 'ifconfig' program."
exit 1
fi
if [ "$1" = "all" ]; then
IFACE=
else
IFACE="$1"
fi
if [ "$2" = "-h" ]; then
# This section prints MAC address
$IFCONFIG $IFACE | sed -n '/HWaddr\|ether /{s/.*\(HWaddr\|ether\) \([0-9A-Fa-f:]\+\).*/\2/;p}'
else
# This section prints IP or IP/netmask or netsmask
#LINES=`$IFCONFIG $IFACE | fgrep 'inet addr'`
LINES=`$IFCONFIG $IFACE | fgrep 'inet '`
# If no lines were found, quit.
if [ $? != 0 ]; then
exit 0
fi
if [ "$2" = "-im" -o "$2" = "-m" ]; then
LINES=$(echo "$LINES" | sed -n 's/.*\(inet addr:\|inet \)\([0-9.]\+\).*\(Mask:\| netmask \)\([0-9.]\+\).*/\2\/\4/;p')
# Now LINES contains things like this: 127.0.0.1/255.0.0.0
elif [ "$2" = "-in" -o "$2" = "-n" ]; then
# DAMN IT... With backticks (`) this doesn't work. With $(), it does!
# Within ``, the double backslashes are replaced by single backslashes,
# leading to incorrect code being passed to awk.
LINES=$(echo "$LINES" | gawk '{IP=gensub(/.*(inet addr:|inet )([0-9.]+).*/,"\\2",""); NETMASK=gensub(/.*(Mask:|netmask )([0-9.]+).*/,"\\2",""); split(NETMASK,V,"."); N=0; for(i=1; i<=4; i++){ while(V[i]) { N+=V[i]%2; V[i]=int(V[i]/2); } }; print IP "/" N }')
# Now LINES contains things like this: 127.0.0.1/8
fi
if [ "$2" = "-i" -o "$2" = "" ]; then
echo "$LINES" | sed -n 's/.*\(inet addr:\|inet \)\([0-9.]\+\).*/\2/;p'
elif [ "$2" = "-im" -o "$2" = "-in" ]; then
echo "$LINES"
elif [ "$2" = "-m" -o "$2" = "-n" ]; then
echo "$LINES" | sed -n 's,^.*/,,;p'
fi
fi
fi