树莓派定时上报公网IP

来源:互联网 发布:福能集团怎么样 知乎 编辑:程序博客网 时间:2024/04/29 14:48

linux的通用方法:

(1)需要软件:msmtp , mutt

(2)去注册一个邮箱(需要开启smtp服务,不然发不了邮件(这个过程可能要填写手机信息(当然也可以找一家不需要登记的)))

(3)编写shell :getipx命令(http://members.3322.org/dyndns/getip

#!/bin/sh
rm getip
wget -q  http://members.3322.org/dyndns/getip


(4)编写c语言代码,定时轮询ip发现有变化就上报

#include <stdio.h>#include <stdlib.h>#include <math.h>void send_mail(char* ip){char cmd[1000] = {};sprintf(cmd,"./sendmail \"%s\"", ip);printf("cmd is: \"%s\"\n", cmd);system(cmd);}int main(){FILE* fpRead      = NULL;char  tmIP[1000]  = {};char  tmIP2[1000] = {};int   ran         = 0;while(1){printf("------------------------------get ip ...--------------------------\n");system("./getipx");fpRead = fopen("getip", "r");if(fpRead){fscanf(fpRead, "%s", tmIP);printf("get ip is: %s\n", tmIP);if(strcmp(tmIP, tmIP2) != 0){printf("ip changed to: %s , sending mail...\n", tmIP);strcpy(tmIP2,tmIP);//send mail send_mail(tmIP2);}else{printf("IP NO Change!\n");}fclose(fpRead);}ran = rand()%15;ran = 20+ran;printf("sleep %d s\n",ran );sleep( ran);}return 0;}

(5)编写sendmail

#!/bin/bash

echo "=====================sending emal============="

ip_addr=$1

echo $ip_addr

cat getip | mutt -s "change_ip" super_getip@126.com


(6)配置文件

------------------------------.msmtprc

account default

host smtp.sina.com

from super_send@sina.com

auth login

user super_send

password supersendips

-----.muttrc

set sendmail="/usr/bin/msmtp"

set use_from=yes

set realname="super_send"

set from=super_send@sina.com

set envelope_from=yes

set charset="utf-8"



0 0
原创粉丝点击