bash脚本 --- ssh登录弹窗警告
Tofloor
poster avatar
151******18
deepin
2018-11-27 07:13
Author
> 大佬不要嘲笑我,我还是个新手

* 在纠结了半个多月,测试了好久,最后决定还是使用死循环实现吧,内存占用四百多k,还是可以接受的。
* 功能:当有人使用ssh登录你的电脑,因情况而定最迟不超过10秒,会有右上角提示,登出也会有。
* 优化与新增功能:每次循环睡眠的时间根据内存占用情况而定,安装后开机自启动(参考了随机更换壁纸脚本lswc的安装与卸载脚本)。
* 功能脚本代码:
  1. #!/bin/bash

  2. #===================================================Description and processing of Global variables. START

  3. # 获取脚本所在位置
  4. path="$(dirname $0)";

  5. # 跳转到脚本所在位置
  6. cd ${path};

  7. # 信息存储文件 : logFile

  8. # 当前登陆者信息 : nowInfo

  9. # 登录者信息 : nowLogin

  10. # 登出者信息 : nowLogout

  11. #===================================================Description and processing of Global variables. END

  12. #===================================================personal function START

  13. # Determine whether the information storage file exists, if it does not exist, create the file START
  14. function touchInfofile() {
  15.     logFile="${path}/log_monitor.info";
  16.     if [[ -e ${logFile} ]];then
  17.         return 0;
  18.     fi

  19.     # 创建信息存储文件
  20.     touch ${logFile};

  21.     # 获取登陆者信息
  22.     echo "$(who | tr -s ' ')" > ${logFile};
  23.     return 1;
  24. }
  25. # Determine whether the information storage file exists, if it does not exist, create the file END

  26. # send information function START
  27. function send_info() {
  28.     # ${1} : nowLogin / nowLogout
  29.     # ${2} : 1 : log in , 2 : log out.

  30.     local LogTTY=$(echo "${1}" | cut -d ' ' -f 2);
  31.     local LogTime=$(echo "${1}" | cut -d ' ' -f 4);
  32.     local LogerIP=$(echo "${1}" | cut -d ' ' -f 5);
  33.     local str1;
  34.     if [[ ${2} -eq 0 ]];then
  35.         if [[ "${LogerIP}" == "(:0)" || "${LogerIP}" == "(127.0.0.1)" ]];then
  36.             str1="本机登出${LogTTY}"
  37.         else
  38.             str1="来自${LogerIP}的登陆者从${LogTTY}登出";
  39.         fi
  40.         LogTime="$(date +"%H:%M")";
  41.     else
  42.         if [[ "${LogerIP}" == "(:0)" || "${LogerIP}" == "(127.0.0.1)" ]];then
  43.             str1="本机登入${LogTTY}"
  44.         else
  45.             str1="来自${LogerIP}的登录者登入${LogTTY}";
  46.         fi
  47.     fi

  48.     notify-send -i gtk-dialog-info "${str1}" "时间 : ${LogTime}";
  49.     return $?;
  50. }
  51. # send information function END

  52. # Let the program sleep for a while START
  53. function sleepSometime() {
  54.     local sltime=$(cat /proc/meminfo | grep -E '^Mem([T]|[A])' | grep -Eo '[0-9]*' | tr -s '\n' ' ' | awk '{
  55.         mem = $2 / $1 * 100;
  56.         if (mem > 75) {
  57.             printf("1");
  58.         } else if (mem > 55) {
  59.             printf("3");
  60.         } else {
  61.             printf("5");
  62.         }
  63.     }');
  64.     sleep ${sltime};
  65.     return 0;
  66. }
  67. # Let the program sleep for a while END

  68. # Determine if the login information has changed START
  69. function ifContinue() {
  70.     local now_info="$(who | tr -s ' ')";
  71.     if [[ "${now_info}" == "$(cat ${logFile})" ]];then
  72.         sleepSometime;
  73.     else
  74.         nowInfo=${now_info};
  75.         return 0;
  76.     fi
  77. }
  78. # Determine if the login information has changed END

  79. # Determine if someone is logged in START
  80. function haveLogin() {
  81.     local now_info=${1};
  82.     local info_file="$(cat ${logFile})";
  83.     local login_info="$(echo "${now_info}" | grep -v "${info_file}")";
  84.     if [[ "x${login_info}" != "x" ]];then
  85.         echo "${login_info}" >> ${logFile};
  86.         nowLogin=${login_info};
  87.         return 1;
  88.     else
  89.         # no one.
  90.         return 0;
  91.     fi
  92. }
  93. # Determine if someone is logged in END

  94. # Determine if someone is logged out START
  95. function haveLogout() {
  96.     local now_info=${1};
  97.     local logout_info="$(grep -vn "${now_info}" ${logFile})";
  98.     local line=$(echo "${logout_info}" | cut -d ':' -f 1);
  99.     local logout_info=$(echo "${logout_info}" | sed 's/^[0-9]*://g');
  100.     if [[ "x${logout_info}" != "x" ]];then
  101.         echo "$(cat ${logFile} | sed "${line}d")" > ${logFile};
  102.         nowLogout=${logout_info};
  103.         return 1;
  104.     else
  105.         # no one.
  106.         return 0;
  107.     fi
  108. }
  109. # Determine if someone is logged out END

  110. #===================================================personal function END

  111. function main() {
  112.     while true;
  113.     do
  114.         # Determine if the information storage file exists.
  115.         # Create this file if it does not exist.
  116.         # prevent the file from being deleted.

  117.         touchInfofile;
  118.    
  119.         # Determine the sleep time based on the memory usage.
  120.         
  121.         sleepSometime;
  122.    
  123.         # Determine whether the login information has changed.
  124.         # If there is no change, it will not return directly.
  125.    
  126.         ifContinue;
  127.    
  128.         haveLogin "${nowInfo}";
  129.         if [[ ${?} -eq 1 ]];then
  130.             send_info "${nowLogin}" 1;
  131.         fi
  132.    
  133.         haveLogout "${nowInfo}";
  134.         if [[ ${?} -eq 1 ]];then
  135.             send_info "${nowLogout}" 0;
  136.         fi
  137.    
  138.     done
  139.     return 0;
  140. }

  141. main;
  142. exit 0;

Copy the Code


* 安装脚本代码:
  1. #!/bin/bash

  2. #        File Name:    install.sh
  3. #        Author:       sunowsir
  4. #        Mail:         sunowsir@protonmail.com
  5. #        Created Time: 2018年10月22日 星期一 21时40分13秒

  6. mkdir -p ~/.local/scripts/log_monitor

  7. cp ./log_monitor.sh ~/.local/scripts/log_monitor/

  8. chmod u+x ~/.local/scripts/log_monitor/log_monitor.sh

  9. cat <
    >  ${HOME}/.config/autostart/log_monitor.desktop
  10. [Desktop Entry]
  11. Name=log_monitor
  12. Exec=${HOME}/.local/scripts/log_monitor/log_monitor.sh
  13. Icon=bash
  14. Terminal=false
  15. Type=Application
  16. Categories=GNOME;GTK;System;Utility
  17. Comment[en_US.UTF-8]=Ssh stalker monitoring
  18. DT

  19. bash ~/.local/scripts/log_monitor/log_monitor.sh &

  20. echo -e "\033[1;32mSuccessful installation! Self-starting every time.\033[0m"

Copy the Code
* 卸载脚本代码:
  1. #!/bin/bash

  2. #        File Name:    uninstall.sh
  3. #        Author:       sunowsir
  4. #        Mail:         sunowsir@protonmail.com
  5. #        Created Time: 2018年10月22日 星期一 21时44分23秒


  6. ps -aux | grep -E '\S*bash\S*\s\S*\/log_monitor.sh' | tr -s ' ' | cut -d ' ' -f 2 | xargs -I {} kill -9 {} 2> /dev/null
  7. ps -aux | grep -E '\S*bash\S*\s\S*\/log_monitor.sh' | tr -s ' ' | cut -d ' ' -f 2 | xargs -I {} kill -9 {} 2> /dev/null
  8. rm -rf ~/.local/scripts/log_monitor/
  9. rm -rf ~/.config/autostart/log_monitor.desktop
  10. echo -e "\033[1;32mBye. \033[0m"
Copy the Code
https://github.com/sunowsir/SSH_log_reminder

Reply Favorite View the author
All Replies

No replies yet