-
Notifications
You must be signed in to change notification settings - Fork 1
/
myip
74 lines (59 loc) · 1.87 KB
/
myip
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
#!/bin/sh
#
# @author JC <https://github.com/joncutrer>
# @version 0.1.1
# @license MIT
VERSION="0.1.1"
USAGE=$(cat <<-EOF
Get the default IP address of the machine.
The one you are always after when you type ifconfig
The IP assigned to the interface that the default route belongs to
Syntax & Example Usage
myip Print the primary IP Address
myip ip Verbose, same as above cmd
myip if Print only the default interface name
myip sm Print only the subnet mask
myip gw Print only the default gateway IP
myip all Print all; ifname ipaddr mask gw
myip help Print this information
Command Line Options
-v,--version: Print Version Number
-h,--help: Alias for help
-a,--all: TODO: Print all configured IP addresses
-l,--list: TODO: Print all Interface Name and IPs, one per line
--cidr: TODO: Show IP with CIDR subnet mask i.e. /24
--csv: TODO: Output in CSV format
EOF
)
def_if=`ip route list | grep -e "^default" | awk '{print $5}'`
def_gw=`ip route list | grep -e "^default" | awk '{print $3}'`
def_ip=`ifconfig $def_if | grep "inet addr" | cut -d ':' -f 2 | cut -d ' ' -f 1`
def_sm=`ifconfig $def_if | grep "inet addr" | cut -d ':' -f 4 | cut -d ' ' -f 1`
if [ -z "$1" ] || [ "$1" == "ip" ]; then
echo "$def_ip"
exit
fi
if [ "$1" == "if" ]; then
echo $def_if
exit
fi
if [ "$1" == "sm" ]; then
echo $def_sm
exit
fi
if [ "$1" == "gw" ]; then
echo $def_gw
exit
fi
if [ "$1" == "all" ]; then
echo "$def_if $def_ip $def_sm $def_gw"
exit
fi
if [ "$1" == "help" ] || [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
echo "$USAGE"
exit
fi
if [ "$1" == "-v" ] || [ "$1" == "--version" ]; then
echo "$VERSION"
exit
fi