📜 ⬆️ ⬇️

Python + Twitter API. Always know your IP

Suppose you need to have access to your system from a distance, and you, let's say, 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 ssh / scp access to your, naturally, always-on, machine.

So what we need. First of all, the aforementioned eternally enabled 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 behind the router, then you need to configure the ssh port, because there is a great chance that the router has a 22 port assigned to it. Also, do not forget to open the port in the firewall.
Next, of course, we put Python (I used the 2.xx branch, the latest stable version). We put python-twitter .
Then we register a new user on Twitter 'e and make it closed from the left following.

Create a .py file and write something like this:
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))

We run the script in your favorite scheduler (cron), set a suitable time interval (it will be quite normal to use a small amount of time, since duplicate messages cannot be posted for 12 hours, so the bot will not litter once again).
We follow our usual user of this freshly born (or now use the new account as your main account).
We put a twitter client on your smartphone / phone / communicator / netbook (or don't put anything anywhere, but simply use the web interface anywhere).
We take a portable version of putty and Winscp on the flash drive.
Everything, you are ready to fight =)

')

Source: https://habr.com/ru/post/82478/


All Articles