-
Notifications
You must be signed in to change notification settings - Fork 35
/
lcVIRL.sh
executable file
·120 lines (99 loc) · 2.46 KB
/
lcVIRL.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
#
# Live packet capture from VIRL
#
VERSION="0.1"
while getopts ":hvp:cw" opt; do
case $opt in
h)
echo "usage: $(basename "$0") [-h] [-v] [-c | -w] -p PORT [Virl_IP]"
echo "Options:"
echo " -h --- Show this help message"
echo " -v --- Show Version number"
echo " -c --- Create pipe file to be listen on"
echo " -w --- Capture packets with wireshark"
echo " -p --- Specify port to capture packets"
echo " Virl_IP VIRL mgmt ip address"
echo ""
echo "-----------"
echo " Virl_IP is only optional if VIRL_HOST env variable is set!"
echo ""
exit 0
;;
v)
echo "Version: $VERSION"
exit 0
;;
p)
PORT=$OPTARG
;;
c)
PIPE_USE=1
;;
w)
WIRESHARK_USE=1
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
*)
echo "Unimplemented option: -$OPTARG" >&2
exit 1
;;
esac
done
shift $((OPTIND-1))
# Check VIRL_HOST environment variable
if [ -z "$VIRL_HOST" ]; then
if [ -z "$1" ]; then
echo "Virl mgmt ip address must be set" >&2
exit 1
else
HOST=$1
fi
else
HOST=$VIRL_HOST
fi
# PORT is mandatory!
if [ -z "$PORT" ]; then
echo "Port parameter must be set" >&2
exit 1
fi
# Verify that both WIRESHARK_USE and PIPE_USE aren't set at the same time
if [ -n "$WIRESHARK_USE" ] && [ -n "$PIPE_USE" ]; then
echo "Can't set both flags [-w and -c] at the same time."
exit 1
fi
# Verify that at least WIRESHARK_USE or PIPE_USE are set
if [ -z "$WIRESHARK_USE" ] && [ -z "$PIPE_USE" ]; then
echo "Need to set one of these flags -w or -c"
exit 1
fi
# Use wireshark
if [ -n "$WIRESHARK_USE" ] && [ "$WIRESHARK_USE" -eq 1 ]; then
nc $HOST $PORT | wireshark -ki -
fi
# Open pipe file
if [ -n "$PIPE_USE" ] && [ "$PIPE_USE" -eq 1 ]; then
PIPE=/tmp/lcvirl
printf -v PIPE_NAME "%s_%s_%s" $PIPE $PORT $RANDOM
if [[ ! -p $PIPE_NAME ]]; then
mkfifo $PIPE_NAME
fi
echo "Pipe: $PIPE_NAME"
# Capture sigTerm [Ctrl-C]
trap "echo -e '\n==> Removing Pipe'; rm $PIPE_NAME" SIGINT SIGTERM
command -v xclip > /dev/null 2>&1
if [ "$?" -eq "0" ]; then
echo $PIPE_NAME | xclip -selection c
echo "==> Pipe filename copied to clipboard."
else
echo "==> warning: xclip not found. Please consider installing it."
fi
nc $HOST $PORT > $PIPE_NAME
fi