加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 通过USB连接设备时执行命令

发布时间:2020-12-14 02:31:39 所属栏目:Linux 来源:网络整理
导读:我目前有一个系统可以将我的 Linux服务器备份到/ media / usb上的USB存储设备上.一切正常.但是,我实际上有2个驱动器,我们每天尝试交换. 是否有插入USB设备的日志记录以及使用sendmail或类似的方式向管理员发送电子邮件(如果驱动器未被交换3天)? 解决方法 你
我目前有一个系统可以将我的 Linux服务器备份到/ media / usb上的USB存储设备上.一切正常.但是,我实际上有2个驱动器,我们每天尝试交换.

是否有插入USB设备的日志记录以及使用sendmail或类似的方式向管理员发送电子邮件(如果驱动器未被交换3天)?

解决方法

你可以解析/ var / log / messages,但我不会.

当你连接/断开设备时,我会编写一个运行脚本的udev规则.有关它的更多信息here

我已经复制了重点,以防网站出现故障:

Rule files are stored in the /etc/udev/rules.d/ directory.

There’s some advice from the README in that directory on how to name
rule files:

Files should be named xx-descriptive-name.rules,the xx should be
chosen first according to the following sequence points:

< 60 most user rules; if you want to prevent an assignment being
overriden by default rules,use the := operator.

these cannot access persistent information such as that from vol_id

< 70 rules that run helpers such as vol_id to populate the udev db

< 90 rules that run other programs (often using information in the
udev db)

=90 rules that should run last


udev规则的第一部分是匹配键.我们将使用
KERNEL从链的最顶端进入以及idVendor,
idProduct,以及来自设备特定信息的串行属性.
这将肯定地识别这个特定的拇指驱动器并忽略
所有其他人.内核参数使用问号作为通配符
如果我们的驱动器安装在不同的节点上(即:sda1,sdb1,
sdc1等)它仍然可以被识别出来.

KERNEL=="sd?1",ATTRS{idVendor}=="13fe",ATTRS{idProduct}=="1f00",ATTRS{serial}=="50E6920B000AE8"

Now that we have the keys necessary to
identify the particular hardware we’re looking for we can add
assignment arguments. In our case we added two. The first creates a
symlink to this device inside of the /dev/ directory. The second
executes a script in our home directory:

SYMLINK+="hackaday",RUN+="/home/mike/notify-plugin.sh 'HackaDay Thumbdrive:' 'Connected as: $KERNEL'"

Here is the final rule assembled
into one line:

KERNEL=="sd?1",ATTRS{serial}=="50E6920B000AE8",SYMLINK+="hackaday",RUN+="/home/mike/notify-plugin.sh 'HackaDay Thumbdrive:' 'Connected as: $KERNEL'"

We added this as the only line in our rule file and then
restarted udev using these commands:

sudo nano /etc/udev/rules.d/81-thumbdrive.rules 
sudo /etc/init.d/udev restart

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读