Linux下的motion detection(最简单的办公室监控系统) 邮件自动发送

http://caspian.dotconf.net/menu/Software/SendEmail/   邮件的地点


http://skpsun.blog.163.com/blog/static/27600552009121109154/ 

  这篇结合前面三篇(关于Linux内核的,关于motion介绍的,关于安装usb摄像头驱动的),介绍了一个Linux下,用来探测运动的软件包motion,它可以按照需要进行配置。实现在摄像头静止情况下,对视场的监控。 

  所需硬件,一台pc,一个普通usb摄像头。然后在Windows XP里装一个虚拟机,下一个虚拟机下用的Linux(见前篇motion介绍的blog),就可以应用motion了。本文里讲motion设置为,一检测到视场里有运动,就向设定邮箱发送监控avi视频。我曾经插着USB摄像头,启动motion,然后运行了一晚上。本想拍下同事早晨来时的视频,结果早晨我来了检查邮箱,除了我走和来时的样子,竟然还有清洁工大妈的样子,一看时间是早晨5:30,这个意外发现解决了我一直以来的一个疑问,到底是谁,什么时间来打扫办公室的。而整晚上发送监控邮件共12封,拍到的视频总大小不到10Mb,确实是一个简单易行的好工具。 

  首先,确保USB摄像头能在Linux下正常工作。 

  在Debian下安装Motion也是相当的简单。http://www.debian.com/distrib/packages里有提供motion的安装包,所以直接在终端下运行 

  # apt-get install motion 

  装好后,根据安装提示,修改/etc/motion/里的相关文件名,得到这个文件/etc/motion/motion.conf。motion.conf就是motion程序与操作者的接口文件。通过修改motion.conf里的配置,来控制motion的运行。当在终端里运行#motion时,会先在终端所示当前目录下寻找motion.conf,没有找到时,去寻找/etc/motion/motion.conf。所以,我把这个文件copy一份,放到了/root下,然后每次都进root里运行motion。 

  运行前,先修改这个文件/root/motion.conf,我的motion.conf 

  # Minimal motion example config file provided by the 

  # Debian motion package - for basic webcam operation. 

  # 

  # You most certainly want to investigate 

  # /usr/share/doc/motion/examples/motion-dist.conf.gz 

  # for further configuration options. Also, refer to the 

  # motion man page and /usr/share/doc/motion/motion_guide.html 

  # for detailed information on configuration options. 

  daemon off 

  quiet on 

  locate on 

  # You may very well need to change this (check with 'dmesg' 

  # after plugging in your webcam). 

  videodevice /dev/video0 

  # Image size in pixels (valid range is camera dependent). 

  width 320 

  height 240 

  framerate 25 

  quality 85 

  auto_brightness off 

  # General threshold level and noise threshold 

  # level (for distinguishing between noise and motion). 

  threshold_tune off 

  threshold 4500 

  noise_level 64 

  # Initial brightness, contrast, hue (NTSC), and saturation. 

  # 0 = disabled (valid range 0-255). 

  brightness 0 

  contrast 0 

  saturation 0 

  hue 0 

  # Encode movies in real-time (install ffmpeg before enabling). 

  ffmpeg_cap_new on 

  # Codec to be used by ffmpeg for the video compression. 

  # Supported formats: mpeg4, msmpeg4. 

  ffmpeg_video_codec msmpeg4 

  # Target base directory for pictures and films (you may need 

  # to change this (or change its permissions) depending on 

  # which system user runs motion). 

  target_dir /root/motion/snapshots 

  # Define a port number (e.g. 8000) to enable the mini-http server. 

  # 0 = disabled. 

  webcam_port 8081 

  # Set to 'off' to allow anybody (not just localhost) to view the 

  # webcam via the mini-http server (http://hostname:port). 

  webcam_localhost off 

  snapshot_interval 1 

  snapshot_filename snapshot 

  webcam_quality 50 

  webcam_maxrate 8 

  on_event_start /root/motion/on_motion_detected 

  on_event_end /root/motion/on_motion_end 

  gap 10 

  关于这些配置的具体意义,参见motion官方wiki http://www.lavrsen.dk/twiki/bin/view/Motion/WebHome中的Config options。这里,我只对其中几个解释一下。 

  daemon off,关掉deamon模式。最好这项还是选off,否则运行motion后,就会直接在后台运行,需要用top命令查看出motion的进程号(pid),然后再手动kill掉这个进程。 

  locate on设置探测到图像中有运动时,把运动区域用矩形框起来。 

  videodevice /dev/video0 设置加载USB摄像头的设备,一般都是这个video0,当使用network webcam时,需要设置netcam_url,此时,videodevice选项自动失效。 

  threshold_tune off设置是否使用motion detection阈值自动调节。当设置为on时,下一个设置threshold 4500自动失效。设置off时,可以由threshold指定当探测到多少像素变化时,判断为图像中有运动。 

  ffmpeg_cap_new on这个选项是指,在detect到运动时,用视频纪录下来。 

  ffmpeg_video_codec msmpeg4 设定视频的编码器 

  target_dir /root/motion/snapshots当探测到运动时,图片和视频的保存路径,默认时为/var/lib/motion/snapshots。 

  snapshot_interval 1设定自动采集图片的周期,当有运动被检测到时,采集频率会自动变高。 

  on_event_start /root/motion/on_motion_detected当探测到运动时,执行所设定目录里的文件,这里设定为文件/root/motion/on_motion_detected,该文件可以是一个程序,可以是一段脚本,只要是能执行的就可以。 

  on_event_end /root/motion/on_motion_end当on_event_start开始后,即检测到运动后, 若有连续10秒不再能检测到运动时,执行该选项设定的文件。10秒参数是由以下gap 10语句设置而来。 

  以上两个设置参见http://www.lavrsen.dk/twiki/bin/view/Motion/ExternalCommands 

  gap 10设置,在探测到运动后,多长时间没有运动的话就触发运动结束指令on_event_end。 

  这里我的on_motion_detected和on_motion_end都是shell脚本。 

  on_motion_detected脚本的作用是,记录下探测到运动时的时间,即拍摄的监控视频文件的文件名的一部分。把这个时间存到/root/tmp/videotime文件中。on_motion_detected文件如下: 

  #!/bin/bash 

  echo "111111111111111on_motion_detected1111111111111111" 

  DATE=$(date +"%Y%m%d%H%M%S") 

  #DATE=$(date -d "-1 sec" +%Y%m%d%H%M%S) 

  ALARM_TIME="/root/tmp/videotime" 

  echo "$DATE" > $ALARM_TIME 

  而on_motion_end就是用来发送邮件的。它会在检测到的运动结束后,讲拍下来的运动的avi视频发送到指定邮箱里。avi视频的文件名为一个序号+检测到运动的时间+.avi,而检测到运动的时间,根据on_motion_detected脚本,存在/root/tmp/videotime里,理论上说只要从文件里读出时间,然后补全文件名(该序号由*号替代),便能发出邮件。但是,由于程序运动效率原因,有时会出现,记录的时间同开始录avi的时间差1秒的情况,虽然只有一秒,但是足以导致找不到avi文件,无法正确发出监控视频。由于我们设置了gap为10,即10秒内最多只有一个视频。所以,解决这个问题的办法可以是,去寻找videotime中所记录时间及其上一秒,连续两秒的视频,找到哪个发哪个。当然,结果永远是只会找到一个。on_motion_end这个shell脚本文件如下: 

  #!/bin/bash 

  echo "111111111111111on_motion_end1111111111111111" 

  DIRC="/root/motion/snapshots/" 

  VIDEOTIME="/root/tmp/videotime" 

  TIME=$(cat $VIDEOTIME) 

  ALARM_EMAIL="/root/tmp/myalarm.txt" 

  echo "Subject: Motion detected - $TIME - $DIRC" > $ALARM_EMAIL 

  echo "">> $ALARM_EMAIL 

  echo "Motion detected - check $TIME.avi">>$ALARM_EMAIL 

  MAILBODY=$(cat $ALARM_EMAIL) 

  #first trying of sending the avi video 

  echo $MAILBODY | mutt -s $TIME -a $DIRC*$TIME.avi [email protected] 

  #second trying of sending the avi video 

  TIME=$(expr $TIME - 1) 

  echo $MAILBODY | mutt -s $TIME -a $DIRC*$TIME.avi [email protected] 

  当然,在能成功自动发送邮件前,先要确认是否正确安装配置了mailx和ssmtp。否则,需要先安装这两个程序。 

  # apt-get install mailx ssmtp 

  然后在/etc/ssmtp/ssmtp.conf里如下进行配置: 

  In /etc/ssmtp/ssmtp.conf, set the following: 

  [email protected] 

  mailhub=smtp.gmail.com:587 

  rewriteDomain= 

  hostname=smtp.gmail.com:587 

  UseSTARTTLS=YES 

  AuthUser=youraccount 

  AuthPass=yourpasword 

  FromLineOverride=YES # optional 

  In /etc/ssmtp/revaliases: 

  root:[email protected]:smtp.gmail.com:587 

  mainuser:[email protected]:smtp.gmail.com:587 

  并在命令行里输入 

  # mail [email protected]进行测试,写完邮件正文后按ctrl+D发送,详见 

  http://www.kedy.cn/show-378-1.html 

  监控到清洁工大妈的视频截图 

  Reference webpages: 

  CLI Magic: Getting into Motion: http://www.linux.com/feature/114118 

  HOWTO Gmail and sSMTP: http://www.kedy.cn/show-378-1.html 

  send email with attachment: http://logjiang.itpub.net/post/38081/473118 

  Motion external commads: http://www.lavrsen.dk/twiki/bin/view/Motion/ExternalCommands 

  linux中怎么才能获得下一秒的时间: http://www.9php.com/FAQ/cxsjl/shell/2009/01/3120364137960.html 

  linux date 命令详解: http://www.admin99.net/read.php/188.htm 

  linux下date命令用法: http://pds911.itpub.net/post/782/454526 

  shell 十三問?: http://bbs.chinaunix.net/viewthread.php?tid=218853 

  [精华] 跟我一起写 Makefile: http://www.chinaunix.net/jh/23/408225.html 

  http://www.stlchina.org/twiki/bin/view.pl/ScriptProgram/LearnMakefile 



原文地址: http://user.qzone.qq.com/50638047/blog/1276827358
http://cha.homeip.net/blog/2007/07/318.html
http://www.ijg.org/
http://www.cnblogs.com/hillywolf/archive/2006/03/18/353071.html
http://www.linux-cn.com/html/linux/beginner/20070505/26523.shtml
发邮件: http://www.360doc.com/content/10/1228/21/4530689_82151383.shtml

ubuntu下使用mutt+msmtp发送邮件(转)

博客分类:  ubuntu linux
Ubuntu Oracle Gmail 脚本 Bash

每天需要多次备份文件,所做操作比较机械化,所以就想在shell下自动帮我备份文件,开始想使用mail命令但是mail命令发邮件需要开启sendmail服务,我只是一台客户端,开个sendmail没有必要,于是乎,在网上找了好久,才发现mutt是个好东西。

在ubuntu下安装mutt很方便,只需要sudo apt-get install mutt,另外需要安装msmtp,一个发邮件的小工具,sudo apt-get insall msmtp。这两个文件都很小,一下子就装完了。

接下来是配置文件,也很简单,这里我引用网上别人的配置和命令行。另外在文件的最后,附上一个脚本文件,是通过发送电子邮件自动备份。

=====================================引用================================

MUTT

系统全局设置/etc/Muttrc,如果使用某个系统用户,可以在~/.muttrc中设置。

vi /etc/Muttrc

set sendmail="/usr/bin/msmtp"

set use_from=yes

set realname="FengYuBin"

set [email protected]

set envelope_from=yes

MSMTP

创建~/.msmtprc和~/.msmtp.log,分别为配置和日志文件。

vi .msmtprc

account default

host smtp.163.com

from [email protected]

auth plain

user fyb

password 123456

logfile ~/.msmtp.log

由于password是明码,所以我们需要修改此文件的权限。

chmod 600 .msmtprc

touch ~/.msmtp.log

查看SMTP服务器是否支持认证的TLS加密:

[oracle@oracle ~]$ msmtp --host=smtp.163.com --serverinfo

SMTP server at smtp.163.com (m5-86.163.com [202.108.5.86]), port 25:

    163.com Anti-spam GT for Coremail System (163com[20050206])

Capabilities:

    PIPELINING:

        Support for command grouping for faster transmission

    AUTH:

        Supported authentication methods:

        PLAIN LOGIN

到这里,你可以使用mutt来发送邮件了,我们测试一下。

echo "test" |mutt -s "my_first_test" [email protected]

-s "subject"

-c "carbon-copy"

echo "test" |mutt -a dbms_stats.txt -s my_first_test [email protected]

echo -e "文字描述,可以带参数$allname" | mutt -a "附件(写好路径)" -a "附件2(写好路径)"   [email protected],[email protected](收信人) -c [email protected],[email protected] (抄送)

观察.msmtp.log文件,如果有错会在日志文件中被报告出来,当然,成功发送的日志也会出现在此日志文件内。

到这里发送邮件的过程已经全部完成,接下来要做的事就简单了,我们假设每天凌晨定时发送信件给公司某用户,可以编写脚本处理。

mail.sh

#!/bin/sh

content="you can tell your colleague what something to do at tomorrow"

echo "$content" |mutt -s "hi Jacky" Jacky's_email

OK,上面这些就是我实现自动发送邮件的全部过程,怎么样,很轻松吧!

=================================下面是我脚本文件=======================

#!/bin/bash

cd

fileName=bak\($(date +%Y%m%d%H%M)\).zip #产生文件名

zip $fileName -r fw2 #将需要备份的目录fw2打包

sleep 1 #睡眠一秒,让zip打包完成的文件写入硬盘

echo "Sending mail..."

#调用mutt发送文件

echo "firewall code src backup" | mutt -s "$fileName" [email protected] -a "$fileName"

echo "Sent OK"

http://www.unixmen.com/linux-tutorials/1018-how-to-turn-your-webcam-into-a-motion-detecting-security-spy-camera-in-linux
这个链接里里面有个问题不懂,

After motion is installed, we have to configure Motion to save captured images in a remote server, to do that we need to install wput a command line FTP client that upload the captured photos by motion , to an remote FTP server. You can install wput by entering the following command into a terminal :

 sudo apt-get install wput

- Now you can configure Motion to use wput to upload captured photos by adding the lines bellow to motion.conf file :

gedit /etc/motion/motion.conf

And add these lines at the end of the file:

# Command to be executed when a picture (.ppm|.jpg) is saved (default: none)# The filename of the picture is appended as an argument for the command.on_picture_save wputftp://USERNAME:PASSWORD@REMOTE SERVER %f

Now save and close.


After motion is installed, we have to configure Motion to save captured images in a remote server, to do that we need to install wput a command line FTP client that upload the captured photos by motion , to an remote FTP server. You can install wput by entering the following command into a terminal :

 sudo apt-get install wput

- Now you can configure Motion to use wput to upload captured photos by adding the lines bellow to motion.conf file :

gedit /etc/motion/motion.conf

And add these lines at the end of the file:

# Command to be executed when a picture (.ppm|.jpg) is saved (default: none)# The filename of the picture is appended as an argument for the command.on_picture_save wputftp://USERNAME:PASSWORD@REMOTE SERVER %f

Now save and close.

You should now be able to open a web interface for Motion now by typing in localhost:8000 from the server itself or XXX.XXX.X.XXX:8000 from another computers web browser. To view a live stream of your webcam type localhost:8001 from the server itself or XXX.XXX.X.XXX:8001 from another computers web browser. You can change settings from this interface here too, you can even make Motion generate it’s own config files to fit your setup exactly.





你可能感兴趣的:(基于linux,arm的家庭安防系统。)