-
Notifications
You must be signed in to change notification settings - Fork 0
/
testnet.sh
executable file
·120 lines (106 loc) · 2.63 KB
/
testnet.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
#!/usr/bin/env bash
# A testing script for my sockets.
# options
# -n => network
# -f => filename to read into socket | -t => literal text to read into socket
# -all => test all available network types
#
helpp() {
echo "# Usage"
echo "# test -f <filename> -n <networkname(s)> -a <addrinfo> OR"
echo "# test -t <text> -n <networkname(s)> -a <addrinfo> OR"
echo "# test -all -a <addrinfo>"
echo "#"
echo "# <networkname> can be a space separated string of networks"
echo "# Posible network names are:"
echo "# tcp, tcp6, udp, udp6, unix-connect, unix-client"
echo "#"
echo "# The -all option test all the network names available,"
echo "# make sure there is a server running on the address else it will error."
}
if [[ -z "$@" ]]; then
helpp
exit 1
fi
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-all)
ALL="yes"
shift
;;
-u)
UNIXADDR="$2"
shift
shift
;;
-a)
IPADDR="$2"
shift
shift
;;
-n)
NET="$2"
shift
shift
;;
-f)
FILE="$2"
shift
shift
;;
-t)
TEXT="$2"
shift
shift
;;
*)
echo Unknown argument $1
helpp
exit 1
;;
esac
done
# parse network types to test
if [[ "$ALL" == "yes" ]]; then
NET="tcp tcp6 udp udp6 unix-connect unix-client"
# if you are testing on all network types, you need a separate
# unix address for the unix domain sockets.
if [[ -z "$UNIXADDR" ]]; then
echo You need a unix address to test unix domain sockets
helpp
exit 1
fi
elif [[ -z "$NET" ]]; then
echo you need a network type to connect to
helpp
exit 1
fi
# check the address to connect to
if [[ -z "$IPADDR" ]] && [[ -z "$UNIXADDR" ]]; then
echo You need an address to connect to
helpp
fi
# check if data to be sent is a file or text
if [[ -n "$FILE" ]]; then
CMD="cat ${FILE}"
elif [[ -n "$TEXT" ]]; then
CMD="echo ${TEXT}"
else
echo you need some kind of data to write into the connection
helpp
fi
debug() {
echo "---------- executing '$1 | socat $2:$3 - > /dev/null' ----------"
}
for net in $NET; do
if [[ "$net" == *"unix"* ]]; then
ADDR="$UNIXADDR"
else
ADDR="$IPADDR"
fi
debug "$CMD" $net "$ADDR"
# socat [options] <address> <address>
$CMD | socat $net:"$ADDR" - > /dev/null
done
#cat $1 | socat $net:$2 - > /dev/null