Python + Twitter API. Always know your IP

    Suppose you need to have access to your system from afar, and you have Stream, and, therefore, dynamic IP. At the same time, for some reason you do not want to use a service like No-IP , but you want simple and direct access via ssh / scp to your, of course, always on machine.

    So what do we need. First of all, the aforementioned always-on machine with an ssh server (of course, you should disable the ability to connect to it with a root / admin account). If the computer is located behind the router, then 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 it, port, open in the firewall'e.
    Next, of course, we install Python (I used the 2.xx branch, the latest stable version). Put python-twitter.
    Then we register the new user on Twitter and make it closed from left following.

    We create a .py file and write something like this: Put the script in your favorite scheduler (cron), set the appropriate time interval (it will be quite normal to use a small amount of time too, because duplicate messages cannot be posted for 12 hours, therefore the bot does not will litter once again). We follow it with our usual user of this freshly born (or now use the new account as your main one). We put a twitter client on your smartphone / phone / communicator / netbook (or we don’t put anything anywhere, but simply use the web-interface anywhere). We take with us on a flash drive the portable version of putty and
    import twitter
    import urllib
    import re

    # Логинимся свежесозданным юзером
    twapi = twitter.Api(username = "xxxxxx", password = "yyyyyy")

    # Выдираем IP с myip.ru
    myipF = urllib.urlopen("http://www.myip.ru/get_ip.php?loc=")
    myipContent = myipF.read()
    myipF.close()
    resIP = re.search('\d+.\d+.\d+.\d+', myipContent)

    # Постим его в Твиттер
    twapi.PostUpdate("My IP now is " + resIP.group(0))




    Winscp .
    Everything, you are ready for battle =)

    Also popular now: