下次ssh登录时的通知 [英] Notifications on next ssh login

查看:135
本文介绍了下次ssh登录时的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个假设性的问题,因为我想在研究脚本之前是否甚至有可能,但是从理论上讲,有可能获得脚本/进程的输出(特别是通过<$ c运行)例如$ c> cron )在下一次 ssh 登录时吐出到终端?

This is a hypothetical question because I'd like to know if it's even possible before I delve in to scripting it, but is it theoretically possible to have the output of a script/process (in particular one run via cron for instance) spit out in to terminal on the next ssh login?

我希望一些伪代码能说明我的观点:

Some pseudocode that I hope illustrates my point:

#!/bin/bash

# Download latest example of a database (updated automatically and periodically)
wget -mirror "http://somedatabase/database_latest

# Run a command that generates an output for a set of files queried against the latest database)
for file in /some/dir/*;
do
    command -output $file.txt -database database_latest
done

# Now for the bit I'm more interested in.
# If the database has been updated, the 'output.txt' 
# for each file will be different.
# So, using diff...:

if [ diff $file.txt $file_old.txt == 1 ] # where file_old.txt is 
                                         # the output of the command the 
                                         # last time it ran for that file.
    then
        mv $file_old ./archive/ # Keep the old file but stash it in a separate dir
    else
        break
fi

 # Make some report file from all of the outputs
 cat *.txt > report.txt

所以我的问题是,下次是否可以让脚本通知我我登录到我们的服务器,是否发现每个文件有任何区别?文件很多,并且'report.txt'会迅速变大,因此我只想检查是否存在差异。

So my question being, is it possible to have the script 'inform me' next time I log in to our server, if any differences were found for each file? There are a lot of files, and the 'report.txt' would become large quickly, so I only want to check it if differences are found.

推荐答案

这是怎么回事:


  • 创建三个目录: new cur old

  • 您的每周cronjob将数据写入。该脚本应在写入新数据之前从new中删除所有内容。否则,您将无法注意到文件丢失

  • cur 包含您查找的数据的最新版本或考虑到

  • 包含数据的先前版本

  • create three directories: new, cur, old
  • your weekly cronjob writes data to new. This script should delete everything from new before writing new data. Or else you won't be able to notice that a file is missing
  • cur contains the last version of the data that you looked at or consideret
  • old contains the previous version of the data

每次登录时,运行:

#!/bin/bash

# clear the archive
rm old/*

# move all the old files to the archive
cp cur/* old

# move all the new files to the location of the old
cp new/* cur

# show which files have changed between
diff -q cur old | tee report.txt

diff -命令将打印哪些文件是新文件,哪些文件已丢失以及哪些文件已更改。 diff 的输出也将在 report.txt 中。 cur -目录将包含上次运行的所有文件,您可以在编辑器中仔细查看它们,也可以将它们与<$ c $中的先前版本进行比较。 c>旧。请注意,如果新建中缺少文件,则不会从 cur 中删除​​该文件。下次登录时,您将丢失 old 目录的内容。如果要保留所有先前结果的历史记录,则应由每周的cronjob而不是登录脚本来管理(您希望每次生成数据时都存储一个单独的版本,而不是每次登录时都存储一个单独的版本)

The diff-command will print which files are new, which are missing and which are changed. Output from diff will also be in report.txt. The cur-directory will contain all files from the last run and you can look closer at these in an editor, or you can compare them to the previous version in old. Note that if a file is missing in new, it won't be deleted from cur. The next time you log on, you will lose the contents of the old-directory. If you want to keep a history of all previous results, this should be managed by the weekly cronjob, not the login-script (you want to store a separate version each time you generate the data, not each time you log in)

这篇关于下次ssh登录时的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆