-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathip_em.sh
executable file
·142 lines (116 loc) · 3.54 KB
/
ip_em.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env bash
##################################################################################
#
# Copyright (C) 2018 Craig Miller
#
# See the file "LICENSE" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
# Distributed under GPLv2 License
#
##################################################################################
#
# Script approsimate the functionality of the linux IP command
# Designed to be more portable (e.g. BSD, MacOS X) than linux IP command
#
# by Craig Miller 5 May 2018
#
# Assumptions:
# All prefixes are assumed /64
#
#
# TODO:
#
#
#
# Moved self test netstat command within the self test block - 20 July 2024
VERSION=1.0
# check OS type
OS=$(uname -s)
if [ "$OS" == "Darwin" ] || [ "$OS" == "FreeBSD" ]; then
OS="BSD"
fi
#
# IP command emulator function - returns status similar to the linux IP command
#
function ip {
# set up default warning message - terminal colours - http://wiki.bash-hackers.org/scripting/terminalcodes
result=$(echo -e "\033[1;91m WARNING: function not implemented: ip $* \033[00m")
# set up -6 or -4 option
inet_opt="inet"
if (( "$1" == "-6")) || (( "$1" == "-4" )); then
if (( $1 == "-6" )); then
inet_opt="inet6"
neigh_cmd="ndp"
else
inet_opt="inet "
neigh_cmd="arp"
fi
# move arguemnts down 1
shift
fi
# Parse dev from 'ip addr show dev eth0'
if [ -n "$2" ]; then
# parse dev
dev=""
if (( "$2" == "show" )); then
if [ -n "$4" ]; then
dev="$4"
fi
fi
fi
case $1 in
"addr" )
result=$(ifconfig $dev | grep -E "$inet_opt|ether" );;
"link" )
result=$(ifconfig $dev | grep -E 'flags|ether'| tr '\n' '|' | sed 's/1500|//g' |sed 's/0 mtu//g' | tr '|' '\n' | awk '{print "1: "$1 " " $2 "\n" $4 " " $5 " " $6 " " $7}' );;
"neigh" )
if [ "$OS" != "Linux" ]; then
# OS is BSD
if [ "$neigh_cmd" == "ndp" ]; then
result=$($neigh_cmd -an | awk '{print $1 " dev " $3 " lladdr " $2}'); fi
if [ "$neigh_cmd" == "arp" ]; then
result=$($neigh_cmd -an | tr -d '()' | awk '{print $2 " dev " $6 " lladdr " $4}'); fi
else
# can't get Linux neigh table without IP command
if [ "$neigh_cmd" == "ndp" ]; then result=$(/usr/bin/env ip -6 neigh); fi
if [ "$neigh_cmd" == "arp" ]; then result=$(/usr/bin/env ip -4 neigh); fi
fi
;;
esac
echo "$result"
}
# self test section
if [ -n "$1" ]; then
if [ "$1" == "test" ]; then
# get self test interface
INTF=$(netstat -i -n | tail -1 | awk '{print $1}')
if [ "$2" != "" ]; then
INTF="$2"
fi
echo "Running self test"
echo "---- ip addr"
ip addr
echo "---- ip link"
ip link
echo "---- ip link show dev $INTF | grep ether | awk '{print $2}'"
ip link show dev "$INTF" | grep ether | awk '{print $2}'
echo "---- ip link long (show interfaces)"
ip link | grep -E -i '(state up|multicast,up|up,)' | grep -v -i no-carrier | cut -d ":" -f 2 | cut -d "@" -f 1
echo "---- ip addr show dev $INTF"
ip addr show dev "$INTF"
echo "---- ip -6 addr show dev $INTF"
ip -6 addr show dev "$INTF"
echo "---- ip -4 addr show dev $INTF"
ip -4 addr show dev "$INTF"
echo "---- ip really long (show GUAs)"
ip addr show dev "$INTF" | grep -v temp | grep inet6 | grep -v fe80 | awk '{print $2}' | cut -d "/" -f 1
echo "---- ip -6 neigh"
ip -6 neigh
echo "---- ip -4 neigh"
ip -4 neigh
echo "---- ip -4 route"
ip -4 route
echo "---- ip addr show dev $INTF | grep 'inet ' | awk '{print $4}' | sort -u (show netmask)"
ip addr show dev $INTF | grep 'inet ' | awk '{print $4}' | sort -u
fi
fi