首页 > 编程 > Python > 正文

DIY个人智能家庭网关—— python篇之获取路由器外网地址

2019-11-06 09:31:43
字体:
来源:转载
供稿:网友

免费的DDNS域名服务有时候刷新不及时,解析不到路由器的ip,就可以采用这种方法来解决

#!/usr/bin/env python# -*- coding: utf-8 -*- import socket  import fcntl  import struct  import subPRocessimport jpushfrom time import sleepSIOCGIFADDR = 0x8915      def get_ip_address(ifname):      s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)      try:        ip = socket.inet_ntoa(fcntl.ioctl(              s.fileno(),              SIOCGIFADDR,              struct.pack('256s', ifname[:15])          )[20:24])          return ip    except:        return ''      old_ip =''while True:      new_ip = get_ip_address('pppoe-wan')     if new_ip != old_ip and new_ip != '':        msg = '你的路由器IP:%s'%new_ip        old_ip = new_ip        try:            jpush.push_msg(msg)        except:            sleep(2)            continue    sleep(10*60)
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表