树莓派定时上报公网IP

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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值