forked from superhj1987/awesome-scripts
-
Notifications
You must be signed in to change notification settings - Fork 25
/
greys
executable file
·217 lines (165 loc) · 4.85 KB
/
greys
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/bin/bash
## start greys debugger
# program : greys
# author : oldmanpushcart@gmail.com
# date : 2015-05-04
# desc : write for july
# version : 1.7.6.4
base_dir=$(dirname "$(echo "$0" | sed -e '')")
# define greys's lib
GREYS_LIB_DIR=${base_dir}/lib
# last update greys version
DEFAULT_VERSION="1.7.6.4"
# greys remote url
GREYS_REMOTE_URL="http://ompc.oss.aliyuncs.com/greys"
# update timeout(sec)
SO_TIMEOUT=5
# define default target ip
DEFAULT_TARGET_IP="127.0.0.1"
# define default target port
DEFAULT_TARGET_PORT="3658"
# define JVM's OPS
JVM_OPS="";
# the option to control greys.sh check the permission
OPTION_CHECK_PERMISSION=1
# the option to control greys.sh attach target jvm
OPTION_ATTACH_JVM=1
# the option to control greys.sh active greys-console
OPTION_ACTIVE_CONSOLE=1
# exit shell with err_code
# $1 : err_code
# $2 : err_msg
exit_on_err()
{
[[ ! -z "${2}" ]] && echo "${2}" 1>&2
exit ${1}
}
# get with default value
# $1 : target value
# $2 : default value
default()
{
[[ ! -z "${1}" ]] \
&& echo "${1}" \
|| echo "${2}"
}
# check greys permission
check_permission()
{
[ ! -w ${HOME} ] \
&& exit_on_err 1 "permission denied, ${HOME} is not writeable."
}
# reset greys work environment
# reset some options for env
reset_for_env()
{
# if env don't define the JAVA_HOME, exit
[ -z ${JAVA_HOME} ] && exit_on_err 1 "NO JAVA_HOME ENV"
# check the jvm version, we need 1.6+
local JAVA_VERSION=$(${JAVA_HOME}/bin/java -version 2>&1|awk -F '"' '/version/&&$2>"1.5"{print $2}')
[[ ! -x ${JAVA_HOME} || -z ${JAVA_VERSION} ]] && exit_on_err 1 "illegal ENV, please set \$JAVA_HOME to JDK6+"
# reset BOOT_CLASSPATH
[ -f ${JAVA_HOME}/lib/tools.jar ] && BOOT_CLASSPATH=-Xbootclasspath/a:${JAVA_HOME}/lib/tools.jar
}
# the usage
usage()
{
echo "
greys usage:
the format was <PID>[@IP:PORT]
<PID> : the target Java Process ID
[IP] : the target's IP
[PORT] : the target's PORT
example:
./greys.sh <PID>
./greys.sh <PID>@[IP]
./greys.sh <PID>@[IP:PORT]
"
}
# parse the argument
parse_arguments()
{
TARGET_PID=$(echo ${1}|awk '/^[0-9]*@/;/^[^@]+:/{print "@"$1};/^[0-9]+$/'|awk -F "@" '{print $1}');
TARGET_IP=$(echo ${1}|awk '/^[0-9]*@/;/^[^@]+:/{print "@"$1};/^[0-9]+$/'|awk -F "@|:" '{print $2}');
TARGET_PORT=$(echo ${1}|awk '/^[0-9]*@/;/^[^@]+:/{print "@"$1};/^[0-9]+$/'|awk -F ":" '{print $2}');
# check pid
if [ -z ${TARGET_PID} ];then
# echo "illegal arguments, the <PID> is required." 1>&2
# return 1
OPTION_ATTACH_JVM=0
fi
# reset ${ip} to default if empty
[ -z ${TARGET_IP} ] && TARGET_IP=${DEFAULT_TARGET_IP}
# reset ${port} to default if empty
[ -z ${TARGET_PORT} ] && TARGET_PORT=${DEFAULT_TARGET_PORT}
return 0
}
# attach greys to target jvm
# $1 : greys_local_version
attach_jvm()
{
local greys_lib_dir=${GREYS_LIB_DIR}/greys-$DEFAULT_VERSION
# if [ ${TARGET_IP} = ${DEFAULT_TARGET_IP} ]; then
if [ ! -z ${TARGET_PID} ]; then
${JAVA_HOME}/bin/java \
${BOOT_CLASSPATH} ${JVM_OPTS} \
-jar ${greys_lib_dir}/greys-core.jar \
-pid ${TARGET_PID} \
-target ${TARGET_IP}":"${TARGET_PORT} \
-core "${greys_lib_dir}/greys-core.jar" \
-agent "${greys_lib_dir}/greys-agent.jar"
fi
}
# active console
# $1 : greys_local_version
active_console()
{
local greys_lib_dir=${GREYS_LIB_DIR}/greys-$DEFAULT_VERSION
if type ${JAVA_HOME}/bin/java 2>&1 >> /dev/null; then
# use default console
${JAVA_HOME}/bin/java \
-cp ${greys_lib_dir}/greys-core.jar \
com.github.ompc.greys.core.GreysConsole \
${TARGET_IP} \
${TARGET_PORT}
elif type telnet 2>&1 >> /dev/null; then
# use telnet
telnet ${TARGET_IP} ${TARGET_PORT}
elif type nc 2>&1 >> /dev/null; then
# use netcat
nc ${TARGET_IP} ${TARGET_PORT}
else
echo "'telnet' or 'nc' is required." 1>&2
return 1
fi
}
# the main
main()
{
echo '==========Thanks to https://github.com/oldmanpushcart/greys-anatomy=========='
while getopts "PUJC" ARG
do
case ${ARG} in
P) OPTION_CHECK_PERMISSION=0;;
J) OPTION_ATTACH_JVM=0;;
C) OPTION_ACTIVE_CONSOLE=0;;
?) usage;exit 1;;
esac
done
shift $((OPTIND-1));
if [[ ${OPTION_CHECK_PERMISSION} -eq 1 ]]; then
check_permission
fi
reset_for_env
parse_arguments "${@}" \
|| exit_on_err 1 "$(usage)"
if [[ ${OPTION_ATTACH_JVM} -eq 1 ]]; then
attach_jvm \
|| exit_on_err 1 "attach to target jvm(${TARGET_PID}) failed."
fi
if [[ ${OPTION_ACTIVE_CONSOLE} -eq 1 ]]; then
active_console \
|| exit_on_err 1 "active console failed."
fi
}
main "${@}"