Site-wide Tags:  Linux(17) | CommandLine(12) | Ubuntu(10) | RemoteAccess(7) | Tools(7) | Vim(7) | LiftWeb(5) | SBT(5) | SoftwareDev(5) | Mac(5) | Scripts(4) | WebDev(4) | Diagrams(4) | Lifty(3) | NetworkDrives(3) | Processwire(3) | Security(3) | Fog(3) | VCS(3) | BestPractices(3) | RaspberryPi(2) | WebDesign(2) | Encryption(2) | Windows(2) | SSH(2) | WinCommandPrompt(2) | GitHubRepos(2) | Emacs(2) | PHP(2) | IDE(2) | ErrorMsgs(2) | JVM(2) | Hardware(2) | Bash(2) | Networks(2) | Graphviz(2) | Cloning | Cygwin | Graphics | Java | SystemRecovery | lessc | Maven | Python | PXE | Samba | LXDE | PackageManagement | LifeHacks | LESS |

This site has been archived and will no longer be updated.
You can find my new profile at neilpahl.com. My new blog is at 808.ninja.

Python Script For Sending My External IP (To Myself) via Email (Gmail)

Tags:  Tools   Python   Scripts   
Created on Thu, 03 May 2012.

Motivation Behind This Cheatsheet

Used when I was in a Jam: No physical access to the host.  Host had a dynamic ip.  Router without ddns support. Luckily not behind a firewall.

The Cheatsheet

I used the following python script to send myself the IP address of the host.


This script requires:
  - Another web host to host a "whatismyip" page. see Host Your Own "What is my IP" Service.
  -A gmail account (i used a dummy gmail account so that I didn't have to setup email on my server, Also because my email credentials are clearly in clear text in the script)

#! /usr/bin/env python
import urllib
import smtplib  
url = urllib.URLopener()
#resp = url.open('http://whatismyip.org')       #no longer provides pure text response
resp = url.open('http://agenthost.com/myip.php')
html = resp.readline()
fromaddr = 'myserverhasemail@gmail.com'
toaddrs  = ['addy1@gmail.com', 'someotheraddy@gmail.com']
msg = 'My IP is' + html + ' Right NOW!'
  
# Credentials (if needed)  
username = 'mygmailaccountname@gmail.com'
password = 'mygmailpw'
  
# The actual mail send  
server = smtplib.SMTP('smtp.gmail.com:587')  
server.starttls()  
server.login(username,password)
for toad in toaddrs:
    server.sendmail(fromaddr, toad, msg)  
server.quit()

Put this script somewhere and give it a name (~/my_ip_now),

add executable permissions:

chmod a+x ~/my_ip_now

Then, depending on how often you want updates, add a cron job to run the script periodically

Further Discussion

This script is not very efficient, but can be usefull when a quick solution is needed. Also, the email format is pretty crude and doesn't show up in thunderbird. I was able to see my msg using my iphone email, so it did the job at the time.



PLEASE let me know if I'm doing something wrong, or if you have any suggestions or requests~

blog comments powered by Disqus