Shell常用脚本:Frp内网穿透服务启动、关闭、重启、查看状态

x33g5p2x  于2022-02-18 转载在 Shell  
字(1.6k)|赞(0)|评价(0)|浏览(1026)

使用

  1. # 使用
  2. # 启动
  3. sh frps.sh start
  4. sh frps.sh stop
  5. sh frps.sh restart
  6. sh frps.sh status

frps.sh脚本 == frps.sh脚本作为服务进行运行

  1. # 将frps.sh脚本作为服务进行运行
  2. # 1. frps.sh的头3行注释添加下面3行
  3. #!/bin/bash
  4. #
  5. #description: 内网穿透服务工具
  6. #chkconfig:2345 99 99
  7. #将frps.sh 移动到 /etc/init.d
  8. cp frps脚本文件 /etc/init.d/
  9. cd /etc/init.d/
  10. #脚本后缀取消掉,好看点
  11. mv frps.sh frps
  12. #修改脚本权限
  13. chmod 777 frps
  14. #将frps脚本文件作为服务
  15. chkconfig --add frps
  16. #查看所有服务
  17. chkconfig --list
  18. #服务运行
  19. service frps 脚本内的几个参数

frps.sh

  1. #!/bin/bash
  2. if [ ! $1 ]; then
  3. echo '缺乏参数:restart|start|stop|status 中的任何一个'
  4. exit 1
  5. fi
  6. getFrpPID () {
  7. frpPID=$(netstat -nlp | grep frp | awk '{print $7}' | awk -F "/" '{print $1}' | awk '!a[$0]++{print $0}')
  8. return $frpPID
  9. }
  10. # 停止Frp进程
  11. stopFrp () {
  12. frpPID=$(netstat -nlp | grep frp | awk '{print $7}' | awk -F "/" '{print $1}' | awk '!a[$0]++{print $0}')
  13. if [ ! "$frpPID" ]; then
  14. echo '当前无Frp内网穿透程序运行'
  15. else
  16. kill -9 "$frpPID"
  17. echo "关闭Frp内网穿透进程【$frpPID】"
  18. fi
  19. }
  20. # 启动Frp进程
  21. startFrp () {
  22. if [ ! -d "/www/server/frp/frp_0.38.0_linux_386" ]; then
  23. echo '缺乏目录:/www/server/frp/frp_0.38.0_linux_386'
  24. exit 1
  25. fi
  26. frpPID=$(netstat -nlp | grep frp | awk '{print $7}' | awk -F "/" '{print $1}' | awk '!a[$0]++{print $0}')
  27. if [ ! $frpPID ]; then
  28. # 进入frp目录
  29. cd /www/server/frp/frp_0.38.0_linux_386
  30. # 守护进程运行frp
  31. nohup ./frps -c ./frps.ini > nohup.out 2>&1 &
  32. echo '=====启动中,耐心等待====='
  33. sleep 4s
  34. statusFrp
  35. else
  36. echo "当前已存有Frp【$frpPID】内网穿透进程,如果想重启请使用restart参数"
  37. fi
  38. }
  39. statusFrp () {
  40. frpPID=$(netstat -nlp | grep frp | awk '{print $7}' | awk -F "/" '{print $1}' | awk '!a[$0]++{print $0}')
  41. if [ ! $frpPID ]; then
  42. echo '当前无Frp内网穿透程序运行'
  43. else
  44. echo "Frp内网穿透程序正在进行中:进程PID【$frpPID】"
  45. fi
  46. }
  47. case $1 in
  48. start)
  49. startFrp
  50. ;;
  51. stop)
  52. stopFrp
  53. ;;
  54. restart)
  55. stopFrp
  56. startFrp
  57. ;;
  58. status)
  59. statusFrp
  60. ;;
  61. *)
  62. echo '参数错误:请输入参数restart|start|stop|status 中的任何一个'
  63. esac

相关文章