-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrsh_daemon.sh
executable file
·122 lines (109 loc) · 2.67 KB
/
rsh_daemon.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
#!/bin/bash
# the script using for start/stop remote shell daemon server to replace the ssh server
PORT=19999
FILTER=~/tmp/_tmp_rsh_filter.sh
# the tsung master's hostname or ip
TSUNG_MASTER=tsung_master
SPECIAL_PATH=""
PROG=`basename $0`
prepare() {
mkdir -p ~/tmp/
cat << EOF > $FILTER
#!/bin/bash
ERL_PREFIX="erl"
while true
do
read CMD
case \$CMD in
ping)
echo "pong"
exit 0
;;
*)
if [[ \$CMD == *"\${ERL_PREFIX}"* ]]; then
exec $SPECIAL_PATH\${CMD}
fi
exit 0
;;
esac
done
EOF
chmod a+x $FILTER
}
start() {
NUM=$(ps -ef|grep ncat | grep ${PORT} | grep -v grep | wc -l)
if [ $NUM -gt 0 ];then
echo "$PROG already running ..."
exit 1
fi
if [ -x "$(command -v ncat)" ]; then
echo "$PROG starting now ..."
ncat -4 -k -l $PORT -e $FILTER --allow $TSUNG_MASTER &
else
echo "no exists ncat command, please install it ..."
fi
}
stop() {
NUM=$(ps -ef|grep ncat | grep rsh | grep -v grep | wc -l)
if [ $NUM -eq 0 ]; then
echo "$PROG had already stoped ..."
else
echo "$PROG is stopping now ..."
ps -ef|grep ncat | grep rsh | grep -v grep | awk '{print $2}' | xargs kill
fi
}
status() {
NUM=$(ps -ef|grep ncat | grep rsh | grep -v grep | wc -l)
if [ $NUM -eq 0 ]; then
echo "$PROG had already stoped ..."
else
echo "$PROG is running ..."
fi
}
usage() {
echo "Usage: $PROG <options> start|stop|status|restart"
echo "Options:"
echo " -a <hostname/ip> allow only given hosts to connect to the server (default is tsung_master)"
echo " -p <port> use the special port for listen (default is 19999)"
echo " -s <the_erl_path> use the special erlang's erts bin path for running erlang (default is blank)"
echo " -h display this help and exit"
exit
}
while getopts "a:p:s:h" Option
do
case $Option in
a) TSUNG_MASTER=$OPTARG;;
p) PORT=$OPTARG;;
s) TMP_ERL=$OPTARG
if [ "$OPTARG" != "" ]; then
if [[ "$OPTARG" == *"/" ]]; then
SPECIAL_PATH=$OPTARG
else
SPECIAL_PATH=$OPTARG"/"
fi
fi
;;
h) usage;;
*) usage;;
esac
done
shift $(($OPTIND - 1))
case $1 in
start)
prepare
start
;;
stop)
stop
;;
status)
status
;;
restart)
stop
start
;;
*)
usage
;;
esac