Linux远程执⾏Shell代码
被控端的代码
1 #!/bin/bash
2
3if false;then
4yum install -y jq
5fi
6 #配置服务器密钥
7 server_key='1'
8 sleep_interval=5
9echo -e 'WEB_SPACE [START] \c';
10while true;do
11 i=0
12echo -e "\n$(date +"%Y-%m-%d %H:%M:%S") \c";
13while true;do
14 post="server_key=${server_key}"
15if [[ -n $shell_id ]];then
16 post="${post}&shell_id=${shell_id}"
17fi
18if [[ -n $result_errno ]];then
19 post="${post}&result_errno=${result_errno}"
20fi
21if [[ -n $result_stdout ]];then
22 result_stdout=$(echo -e "${result_stdout}\c" | xxd -plain | tr -d '\n' | sed's/\(..\)/%\1/g')
23 post="${post}&result_stdout=${result_stdout}"
24fi
25if [[ -n $result_stderr ]];then
26 result_stderr=$(echo -e "${result_stderr}\c" | xxd -plain | tr -d '\n' | sed's/\(..\)/%\1/g')
27 post="${post}&result_stderr=${result_stderr}"
28fi
29 json=$(echo ${post}|curl --connect-timeout 5 -m 5 -X POST -d @- 'supplier.feieryun/fapi/index.php/web_server'2>/tmp/stderr) 30if [[ $? != 0 ]];then
31 #cURL执⾏失败了以后要不断重试
32cat /tmp/stderr
33rm -f /tmp/stderr
34sleep1
35 break
36fi
37rm -f /tmp/stderr
38 #cURL执⾏成功后清理缓冲
39 shell_id=''
40 result_errno=''
41 result_stdout=''
42 result_stderr=''
43 message=$(echo ${json}|jq -r '.message')
44if [[ $? != 0 ]];then
45echo ${json}
46 break
47fi
48if [[ $message != 'NULL' && $message != 'null' ]];then
49echo"message: ${message}"
50fi
51 shell_id=$(echo ${json}|jq -r '.shell_id')
52if [[ $shell_id == 'null' ]];then
53echo -e '.\c';
54sleep ${sleep_interval}
55 ((i++))
56if [ $i -ge 60 ];then
57 break
58fi
59 continue;
60fi
61 #取得要执⾏的Shell命令
62 shell_command=$(echo ${json}|jq -r '.shell_command')
63if true;then
64 #兼容任何形式的shell语句
65 #echo ${json}|jq -r '.shell_command'>/tmp/shell_command.sh
66echo ${shell_command}>/tmp/shell_command.sh
67 result_stdout=$(bash /tmp/shell_command.sh2>/tmp/stderr)
68 result_errno=$?
69rm -f /tmp/shell_command.sh
70fi
71if false;then
72 #这个⽅法执⾏含有引号的代码就会失效
73 #shell_command=$(echo ${json}|jq -r '.shell_command')
74 result_stdout=$(${shell_command} 2>/tmp/stderr)
75 result_errno=$?
76fi
77 #result_stdout=`cat /tmp/stdout`
78 result_stderr=`cat /tmp/stderr`
79rm -f /tmp/stderr
80echo -e "\nshell_id: ${shell_id}\c"
81echo -e "\nshell_command: ${shell_command}\c" 82echo -e "\nresult_errno: ${result_errno}\c"
83 #echo -e "\nresult_stdout: ${result_stdout}\c"
84 #echo -e "\nresult_stderr: ${result_stderr}\c"
85 break
86done;shell代码
87done;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论