全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
查看: 689|回复: 1

[Windows VPS] 这个shell怎么改成无人值守安装呀

[复制链接]
发表于 2021-3-16 20:22:02 | 显示全部楼层 |阅读模式
  1. #!/bin/bash
  2. #
  3. # EHEH Agent Installation Script
  4. #
  5. # @version                1.0.0
  6. # @date                        2020-09-21
  7. # @copyright: The script baseon nodequery.com/nq-agent project under mit license
  8. #
  9. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  10. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  11. # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
  12. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  13. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  14. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  15. # THE SOFTWARE.

  16. # Set environment
  17. PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

  18. _yellow() {
  19.     printf '\033[0;31;33m%b\033[0m' "$1"
  20. }
  21. _blue() {
  22.     printf '\033[0;31;36m%b\033[0m' "$1"
  23. }

  24. clear

  25. # Prepare output
  26. echo -e "|\n|   EHEH 一键安装\n|   ===================\n|"

  27. # Root required
  28. if [ $(id -u) != "0" ];
  29. then
  30.         echo -e "|   Error: 你需要用root账号执行该脚本\n|"
  31.         echo -e "|          安装脚本将创建一个EHEH账号用来定时执行脚本\n|"
  32.         exit 1
  33. fi

  34. # Parameters required
  35. if [ $# -lt 1 ]
  36. then
  37.         echo -e "|   用法: bash $0 'token'\n|"
  38.         exit 1
  39. fi


  40. #delete exits data
  41. echo "|" && read -p "|   安装将删除本地的以前记录  [Y/n] " input_del
  42. if [ -z $input_del ] || [ $input_del == "Y" ] || [ $input_del == "y" ]
  43. then
  44.   #uninstall
  45.   rm -Rf /etc/EHEH
  46.   crontab -u EHEH -r 1>/dev/null 2>/dev/null
  47.   userdel EHEH     1>/dev/null 2>/dev/null
  48. else
  49.   exit 1
  50. fi


  51. # Check if crontab is installed
  52. if [ ! -n "$(command -v crontab)" ]
  53. then

  54.         # Confirm crontab installation
  55.         echo "|" && read -p "|   当前系统没安装Crontab,无法定时推送数据,安装吗??  [Y/n] " input_variable_install

  56.         # Attempt to install crontab
  57.         if [ -z $input_variable_install ] || [ $input_variable_install == "Y" ] || [ $input_variable_install == "y" ]
  58.         then
  59.                 if [ -n "$(command -v apt-get)" ]
  60.                 then
  61.                         # echo -e "|\n|   Notice: Installing required package 'cron' via 'apt-get'"
  62.                     apt-get -y update
  63.                     apt-get -y install cron
  64.                 elif [ -n "$(command -v yum)" ]
  65.                 then
  66.                         # echo -e "|\n|   Notice: Installing required package 'cronie' via 'yum'"
  67.                     yum -y install cronie

  68.                     if [ ! -n "$(command -v crontab)" ]
  69.                     then
  70.                             # echo -e "|\n|   Notice: Installing required package 'vixie-cron' via 'yum'"
  71.                             yum -y install vixie-cron
  72.                     fi
  73.                 elif [ -n "$(command -v pacman)" ]
  74.                 then
  75.                         # echo -e "|\n|   Notice: Installing required package 'cronie' via 'pacman'"
  76.                     pacman -S --noconfirm cronie
  77.                 fi
  78.         fi

  79.         if [ ! -n "$(command -v crontab)" ]
  80.         then
  81.             # Show error
  82.             echo -e "|\n|   Error: Crontab is required and could not be installed\n|"
  83.             exit 1
  84.         fi
  85. fi


  86. # Check if netstat is installed
  87. if [ ! -n "$(command -v netstat)" ]
  88. then

  89.         echo "|" && read -p "|   当前系统没安装netstat,安装吗??  [Y/n] " input_variable_install

  90.         # Attempt to install crontab
  91.         if [ -z $input_variable_install ] || [ $input_variable_install == "Y" ] || [ $input_variable_install == "y" ]
  92.         then
  93.                 if [ -n "$(command -v apt-get)" ]
  94.                 then
  95.                     apt-get -y update
  96.                     apt-get -y install net-tools
  97.                 elif [ -n "$(command -v yum)" ]
  98.                 then
  99.                     yum -y install net-tools
  100.                 elif [ -n "$(command -v pacman)" ]
  101.                 then
  102.                     pacman -S --noconfirm netstat-nat
  103.                 fi
  104.         fi

  105.         if [ ! -n "$(command -v netstat)" ]
  106.         then
  107.             # Show error
  108.             echo -e "|\n|   错误:自动安装netstat过程中出现错误,一般是你取消了安装或源或者依赖问题,请处理后再进行\n|"
  109.             echo -e "|\n|        如果你系统是Ubuntu 20.04, 19.10 或者 19.04,且提示错误是libpng12-0缺失,可参考以下解决\n|"
  110.             echo -e "|        add-apt-repository ppa:linuxuprising/libpng12|"
  111.             echo -e "|        apt update|"
  112.             echo -e "|        apt install libpng12-0\n|"
  113.             echo -e "|\n|        如果你系统是其他版本的ubuntu,或其他操作系统,请自行搜索解决\n|"
  114.             printf  "$(_blue "|   netstat命令没安装好,将无法统计MYSQL基本指标、Web端口信息,如您不需要统计这2项,可不做处理。") \n\n"
  115. #            exit 1
  116.         fi
  117. fi


  118. # Check if netstat is installed
  119. if [ ! -n "$(command -v ping)" ]
  120. then

  121.         echo "|" && read -p "|   当前系统没安装ping,安装吗??  [Y/n] " input_variable_install

  122.         # Attempt to install crontab
  123.         if [ -z $input_variable_install ] || [ $input_variable_install == "Y" ] || [ $input_variable_install == "y" ]
  124.         then
  125.                 if [ -n "$(command -v apt-get)" ]
  126.                 then
  127.                     apt-get -y update
  128.                     apt-get -y install iputils-ping
  129.                 elif [ -n "$(command -v yum)" ]
  130.                 then
  131.                     yum -y install iputils
  132.                 elif [ -n "$(command -v pacman)" ]
  133.                 then
  134.                     pacman -S --noconfirm iputils
  135.                 fi
  136.         fi

  137.         if [ ! -n "$(command -v ping)" ]
  138.         then
  139.             # Show error
  140.              echo -e "|\n|   错误:自动安装ping过程中出现错误,一般是源或者依赖问题,请根据提示修复后再进行\n|"
  141.             exit 1
  142.         fi
  143. fi

  144. # Check if cron is running
  145. if [ -z "$(ps -Al | grep cron | grep -v grep)" ]
  146. then

  147.         # Confirm cron service
  148.         echo "|" && read -p "|   系统定时任务Crontab没启动,要启动吗? [Y/n] " input_variable_service

  149.         # Attempt to start cron
  150.         if [ -z $input_variable_service ] || [ $input_variable_service == "Y" ] || [ $input_variable_service == "y" ]
  151.         then
  152.                 if [ -n "$(command -v apt-get)" ]
  153.                 then
  154.                         # echo -e "|\n|   Notice: Starting 'cron' via 'service'"
  155.                         service cron start
  156.                 elif [ -n "$(command -v yum)" ]
  157.                 then
  158.                         # echo -e "|\n|   Notice: Starting 'crond' via 'service'"
  159.                         chkconfig crond on
  160.                         service crond start
  161.                 elif [ -n "$(command -v pacman)" ]
  162.                 then
  163.                         # echo -e "|\n|   Notice: Starting 'cronie' via 'systemctl'"
  164.                     systemctl start cronie
  165.                     systemctl enable cronie
  166.                 fi
  167.         fi

  168.         # Check if cron was started
  169.         if [ -z "$(ps -Al | grep cron | grep -v grep)" ]
  170.         then
  171.                 # Show error
  172.                 echo -e "|\n|   Error: Crontab定时任务没启动\n|"
  173.                 exit 1
  174.         fi
  175. fi

  176. # Attempt to delete previous agent
  177. if [ -f /etc/EHEH/eheh-agent.sh ]
  178. then
  179.         # Remove agent dir
  180.         rm -Rf /etc/EHEH

  181.         # Remove cron entry and user
  182.         if id -u EHEH >/dev/null 2>&1
  183.         then
  184.                 (crontab -u EHEH -l | grep -v "/etc/EHEH/eheh-agent.sh") | crontab -u EHEH - && userdel EHEH
  185.         else
  186.                 (crontab -u root -l | grep -v "/etc/EHEH/eheh-agent.sh") | crontab -u root -
  187.         fi
  188. fi

  189. # Create agent dir
  190. mkdir -p /etc/EHEH

  191. # Download agent -offical website
  192. #echo -e "|   下载脚本到 /etc/EHEH\n|\n|    + $(wget -nv -o /dev/stdout -O /etc/EHEH/eheh-agent.sh --no-check-certificate https://eheh.org/shell/eheh-agent.sh)"

  193. #github link
  194. echo -e "|   下载脚本到 /etc/EHEH\n|\n|   + $(wget -nv -o /dev/stdout -O /etc/EHEH/eheh-agent.sh --no-check-certificate https://raw.github.com/eheh-org/agent/master/eheh-agent.sh)"

  195. if [ -f /etc/EHEH/eheh-agent.sh ]
  196. then
  197.         # Create auth file
  198.         echo "$1" > /etc/EHEH/eheh-auth.log

  199.         # Create user
  200.         useradd EHEH -r -d /etc/EHEH -s /bin/false

  201.         # Modify user permissions
  202.         chown -R EHEH:EHEH /etc/EHEH && chmod -R 700 /etc/EHEH

  203.         # Modify ping permissions
  204.         chmod +s `type -p ping`

  205.         # Configure cron
  206.         crontab -u EHEH -l 2>/dev/null | { cat; echo "*/3 * * * * bash /etc/EHEH/eheh-agent.sh > /etc/EHEH/eheh-cron.log 2>&1"; } | crontab -u EHEH -

  207.         # Show success
  208.         echo -e "|\n|   小提示:如果你的服务器安装有宝塔,crontab可能被停用了,请参考:https://eheh.org/article/10 进行设置下"
  209.         echo -e "|\n|   小提示:如果你的服务使用了Cpanel,ping可能缺少权限,请参考https://eheh.org/article/6 进行设置下"
  210.         echo -e "|\n|"
  211.         printf  "$(_blue "|   安装完成,开始设置") \n\n"

  212.   echo "|" && read -p "|   你想监控mysql状态吗,如果要请输入Y,我们将帮你设置  [Y/n] " input_mysql
  213.   if [ -z $input_mysql ] || [ $input_mysql == "Y" ] || [ $input_mysql == "y" ]
  214.   then
  215.         echo "|" && read -p "|  请输入mysql用户名  " input_mu
  216.         $(echo "$input_mu" > /etc/EHEH/mysql-user)

  217.         echo "|" && read -p "|  请输入mysql密码  " input_mp
  218.         $(echo "$input_mp" > /etc/EHEH/mysql-pass)

  219.         echo -e "|\n|   Done,完成设置"
  220.   fi


  221. if [ -n "$(command -v apt-get)" ]
  222. then
  223.     printf  "$(_yellow "|   Debian/ubuntu的默认shell已经切换成了dash。如需马上推送一次数据,请手动执行以下2条命令")\n\n"
  224.     printf  "$(_blue "|   /etc/EHEH/eheh-agent.sh ") \n\n"
  225.     printf  "$(_blue "|   chown -Rf EHEH.EHEH /etc/EHEH/") \n\n"
  226. else
  227.       #push data at once ?
  228.       echo "|" && read -p "|   需要马上推送数据吗?或等5/6分钟  [Y/n] " push
  229.       if [ $push == "Y" ] || [ $push == "y" ]
  230.       then
  231.         sh /etc/EHEH/eheh-agent.sh
  232.         chown -Rf EHEH.EHEH /etc/EHEH/
  233.       fi
  234. fi




  235.   echo -e "|\n| 完成安装!EHEH.ORG 上见\n|"

  236.         # Attempt to delete installation script
  237.         if [ -f $0 ]
  238.         then
  239.                 rm -f $0
  240.         fi
  241. else
  242.         # Show error
  243.         echo -e "|\n|  错误,无法安装\n|"
  244. fi
复制代码

shell地址:https://raw.githubusercontent.com/eheh-org/agent/master/install.sh
发表于 2021-3-17 12:36:49 来自手机 | 显示全部楼层
带read的直接赋值
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2026-5-9 07:57 , Processed in 0.067083 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表