Linux远程执行Shell代码

 

被控端的代码

 1 #!/bin/bash
 2 
 3 if false;then
 4     yum install -y jq
 5 fi
 6 #配置服务器密钥
 7 server_key='1'
 8 sleep_interval=5
 9 echo -e 'WEB_SPACE [START] \c';
10 while true;do
11     i=0
12     echo -e "\n$(date +"%Y-%m-%d %H:%M:%S") \c";
13     while true;do
14         post="server_key=${server_key}"
15         if [[ -n $shell_id ]];then
16             post="${post}&shell_id=${shell_id}"
17         fi
18         if [[ -n $result_errno ]];then
19             post="${post}&result_errno=${result_errno}"
20         fi
21         if [[ -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}"
24         fi
25         if [[ -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}"
28         fi
29         json=$(echo ${post}|curl --connect-timeout 5 -m 5 -X POST -d @- 'http://supplier.feieryun.cn/fapi/index.php/web_server' 2>/tmp/stderr)
30         if [[ $? != 0 ]];then
31             #cURL执行失败了以后要不断重试
32             cat /tmp/stderr
33             rm -f /tmp/stderr
34             sleep 1
35             break
36         fi
37         rm -f /tmp/stderr
38         #cURL执行成功后清理缓冲
39         shell_id=''
40         result_errno=''
41         result_stdout=''
42         result_stderr=''
43         message=$(echo ${json}|jq -r '.message')
44         if [[ $? != 0 ]];then
45             echo ${json}
46             break
47         fi
48         if [[ $message != 'NULL' && $message != 'null' ]];then
49             echo "message: ${message}"
50         fi
51         shell_id=$(echo ${json}|jq -r '.shell_id')
52         if [[ $shell_id == 'null' ]];then
53             echo -e '.\c';
54             sleep ${sleep_interval}
55             ((i++))
56             if [ $i -ge 60 ];then
57                 break
58             fi
59             continue;
60         fi
61         #取得要执行的Shell命令
62         shell_command=$(echo ${json}|jq -r '.shell_command')
63         if true;then
64             #兼容任何形式的shell语句
65             #echo ${json}|jq -r '.shell_command'>/tmp/shell_command.sh
66             echo ${shell_command}>/tmp/shell_command.sh
67             result_stdout=$(bash /tmp/shell_command.sh 2>/tmp/stderr)
68             result_errno=$?
69             rm -f /tmp/shell_command.sh
70         fi
71         if false;then
72             #这个方法执行含有引号的代码就会失效
73             #shell_command=$(echo ${json}|jq -r '.shell_command')
74             result_stdout=$(${shell_command} 2>/tmp/stderr)
75             result_errno=$?
76         fi
77         #result_stdout=`cat /tmp/stdout`
78         result_stderr=`cat /tmp/stderr`
79         rm -f /tmp/stderr
80         echo -e "\nshell_id: ${shell_id}\c"
81         echo -e "\nshell_command: ${shell_command}\c"
82         echo -e "\nresult_errno: ${result_errno}\c"
83         #echo -e "\nresult_stdout: ${result_stdout}\c"
84         #echo -e "\nresult_stderr: ${result_stderr}\c"
85         break
86     done;
87 done;

 

posted on 2021-10-06 22:07  项希盛  阅读(599)  评论(0)    收藏  举报