Home Closet Python+ TwitterAPI.Always know your IP

Python+ TwitterAPI.Always know your IP

by admin

Suppose you need to access your system from afar, and let’s say you have Stream, and hence a dynamic IP.That said, use a service like No-IP you do not want to use a service like for some reason but you want a simple and direct ssh/scp access to your machine which is always on, of course.
So, what we need. First of all, the aforementioned always-on machine with an ssh server (you should of course disable the ability to connect to it with a root/admin account). If the computer is behind the router, you need to configure the ssh port, because there is a good chance that the router has port 22 assigned to it. In addition, do not forget to open it, the port on the firewall.
Then of course we put Python (I used the 2.x.x branch, the latest stable version). Put in python-twitter
Then register a new user at Twitter ‘e and make it closed from left-handed followering.
Create a .py file and write something like this :
import twitter
import urllib
import re
# Logging in as a freshly created user
twapi = twitter.Api(username = "xxxxxxxx", password = "yyyyyyy")
# Rip out IP from myip.ru
myipF = urllib.urlopen("http://www.myip.ru/get_ip.php?loc=")
myipContent = myipF.read()
resIP = re.search('\d+.\d+.\d+.\d+', myipContent)
# Post it on Twitter
twapi.PostUpdate("My IP now is " + resIP.group(0))

Execute the script in your favorite scheduler (cron), set a suitable time interval (it will be fine to use a small time interval, because duplicate messages can not be posted for 12 hours, so the bot will not litter unnecessarily).
Foul with your regular user of this newborn (or use the new account as your primary account now).
Install on your smartphone/phone/communicator/netbook the twitter client (or don’t install anything, but just use the web-interface wherever you like).
We bring the portable version on a flash drive putty and Winscp
That’s it, you’re ready for battle =)

You may also like