Best Tips - TM1dk

Go to content

Main menu:

Best Tips

 
Visit the forum for TM1 tips

Send public ip by mail

 
Python 3.6.5 program tips

If you have a server at home that you want to access, and you have a broadband connection to the internet with a random ip address from your ISP, then you need to know todays ip. From internet there are diffrent ways to solve this.
Buy a program from https://www.gearboxcomputers.com/products/ip-watcher/

Write your own program in python -- here an example
Install python 365 from https://www.python.org/downloads/release/python-365/
Install some development plattform like http://ninja-ide.org/downloads/
To get the public ip create this lines in your python script

import requests
page = requests.get('http://botliam.com/1.php')
ip = page.text

# test the value of variable ip by show it on terminal
print (ip)

Test and ensure above file work.

Then to send a mail with your ISP just a code like below (this example is for TELENOR):

# use smtplib functions
import smtplib
server = smtplib.SMTP_SSL('smtp.bredband.net', 465)
# some ISP does not use tls instead you should use SMTP_SSL as python command
# server.starttls()
server.login("your email address", "your password")
# set the ip number as the message text
msg = "This is your public ip" + ip + " have a good day"
server.sendmail("your email adress", "the email address to send to", msg )
server.quit()


You need to change values above to match your login to your ISP mail server

Save both above codes in a python file called mypublicip.py in a folder like c:\code
Download and install the program that create a exe of a python script https://medium.com/dreamcatcher-its-blog/making-an-stand-alone-executable-from-a-python-script-using-pyinstaller-d1df9170e263
pip install pyinstaller

Then go to a command prompt on your server, and move to the folder where you have python installed.
cd C:\Program Files (x86)\Python36-32
Enter the below command to compile the File

pyinstaller.exe --onefile "c:\code\mypublicip.py"

After the file is created, it can be found in folder C:\Program Files (x86)\Python36-32\dist
Copy the mypublicip.exe file to another computer and test that it works.

Now you can use the windows schedule to make it run every day if you want.



More information on how to do it at http://naelshiab.com/tutorial-send-email-python/

import smtplib

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login("YOUR EMAIL ADDRESS", "YOUR PASSWORD")

msg = "YOUR MESSAGE!"
server.sendmail("YOUR EMAIL ADDRESS", "THE EMAIL ADDRESS TO SEND TO", msg)
server.quit()



More info on how to get IP
This website http;//botliam.com/1.php outputs your ip so you only need these 3 lines to get your ip.

import requests
page = requests.get('http://botliam.com/1.php')
ip = page.text

what its doing is:

opens my webpage and calls it "page"
sets "ip" to the contents of the "page"

https://stackoverflow.com/questions/24508730/finding-network-external-ip-addresses-using-python?noredirect=1&lq=1

Search google for more python examples.
 
Back to content | Back to main menu