未加星标

Keeping up with IP number changes

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二03 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

November 9, 2018 at 8:52 AM by Dr. Drang

Last weekend I was at a hotel working on my iPad and launched Prompt to connect to my iMac at home and run a command. It wouldn’t connect. I launched Transmit (which still works well, despite its zombie status ) and couldn’t connect there, either. Obviously, my service provider had changed my home IP number. This happens rarely,but I didn’t want to be caught like this again.

My first thought was to write a script that would check the IP number periodically and save it in a file on the leancrew.com server. But a bit of searching led me to this script be Charlie Hawker, which checks for the IP number and sends an email if it’s changed, a much better idea.

There were, however, two things about the script I didn’t like:

It’s written inphp. I don’t disklike PHP―I use it to build this blog―but it’s not a language I’m deeply familiar with, and I’d prefer a python script so I can easily make changes if necessary. It gets the IP number by loading a website ( ip6.me ) and scraping it. I wanted something a little more direct.

The solution to the second problem was to use dig , the domain information groper (a program that was undoubtedly named by a man). Here’s a command that does it by returning information from OpenDNS

dig +short myip.opendns.com @resolver1.opendns.com

With that command in mind, the script, checkip , was easy to write:

python: 1: #!/usr/bin/python 2: 3: import smtplib 4: import subprocess as sb 5: import os 6: import sys 7: 8: # Parameters 9: ipFile = '{}/.ipnumber'.format(os.environ['HOME']) 10: mailFrom = 'user@gmail.com' 11: mailTo = 'user@gmail.com' 12: gmailUser = 'user' 13: gmailPassword = 'kltpzyxm' 14: cmd = '/usr/bin/dig +short myip.opendns.com @resolver1.opendns.com' 15: msgTemplate = '''From: {} 16: To: {} 17: Subject: Home IP number has changed 18: Content-Type: text/plain 19: 20: {} 21: ''' 22: 23: # Get the current IP number and the filed IP number 24: dig = sb.check_output(cmd.split(), universal_newlines=True) 25: currentIP = dig.strip() 26: with open(ipFile, 'r') as ip: 27: oldIP = ip.read().strip() 28: 29: # Update the file and send an email if they differ 30: if currentIP != oldIP: 31: with open(ipFile, 'w') as ip: 32: ip.write(currentIP) 33: 34: msg = msgTemplate.format(mailFrom, mailTo, currentIP) 35: smtp = smtplib.SMTP_SSL('smtp.gmail.com', 465) 36: smtp.ehlo() 37: smtp.login(gmailUser, gmailPassword) 38: smtp.sendmail(mailFrom, mailTo, msg)

Lines 9 21 set the parameters that are used later in the script. As you can see from Line 9, I save the IP number in the hidden file .ipnumber in my home directory. Line 14 is the dig command we just talked about. Lines 15 21 is the template for the email that the script sends when the IP number change, and Lines 10 13 are the personal settings needed to send the email.

Lines 24 25 run the dig commandand store its result (after stripping the trailing newline) in the currentIP variable. Lines 26 27 read the .ipnumber file, which contains the IP number from the last time the script was run, and store the contents in oldIP .

Line 30 then compares the two numbers and continues the script only if they differ. Lines 31 32 puts the new IP number into the .ipnumber file, and Lines 34 38 build an email message and send it via GMail. This script works only if you have a GMail account―if you don’t, you’ll have to tweak these lines to get your email service to send the message.

The email it sends is nothing fancy: a subject line telling me the IP number has changed and a body with the new number (which I’ve obscured in this screenshot).


Keeping up with IP number changes

To avoid an error the first time this script is run, create the .ipnumber file and put any old IP number in it. I seeded mine with an incorrect value, 11.11.11.11 , so I could make sure the file changed and the email was sent when the script ran.

I decided to have the script run every hour. If I were using linux, I’d set this up through cron , but the (more complicated) Mac way is with launchd . Here’s the plist file that controls the running of the script. It’s called com.leancrew.checkip.plist and is saved in my ~/Library/LaunchAgents directory:

xml: 1: <?xml version="1.0" encoding="UTF-8"?> 2: <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 3: <plist version="1.0"> 4: <dict> 5: <key>Label</key> 6: <string>com.leancrew.checkip</string> 7: <key>ProgramArguments</key> 8: <array> 9: <string>/Users/drang/Dropbox/bin/checkip</string> 10: </array> 11: <key>StartCalendarInterval</key> 12: <array> 13: <dict> 14: <key>Minute</key> 15: <integer>12</integer> 16: </dict> 17: </array> 18: </dict> 19: </plist>

The key entries here are Line 9, which gives the full path to the checkip script, and Lines 11 17, which tell launchd to run the script at 12 minutes past the hour.Because there are no Hour, Day, or Month entries in this section, it runs every hour of every day of every month.

Because of where it’s stored, this Launch Agent gets loaded into the launchd system every time I log in. To get it to load without logging out and logging back it, I ran these two commands:

launchctl load ~/Library/LaunchAgents/com.leancrew.checkip.plist launchctl start com.leancrew.checkip

With it in the system, the agent will continue to run as long as I’m logged in. For me, this is essentially forever, as I keep this computer running all the time specifically so I can log into it whenever I need to.

I’ve thought of improving the script by adding an error-handling section to alert me when the dig command fails. This would let me know, for example, if OpenDNS has stopped running its service. But the possibility of being inundated by false positive emails has kept me from trying that out.

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

分页:12
转载请注明
本文标题:Keeping up with IP number changes
本站链接:https://www.codesec.net/view/611855.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(11)