8000 Create whats_my_ip_script.py · casual-cod3r/python-scripts@9bb04c6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9bb04c6

Browse files
authored
Create whats_my_ip_script.py
1 parent 96f9ac2 commit 9bb04c6

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

get-my-ip/whats_my_ip_script.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/bin/python
2+
3+
from bs4 import BeautifulSoup as bs4
4+
import requests
5+
import smtplib
6+
from email.MIMEMultipart import MIMEMultipart
7+
from email.MIMEText import MIMEText
8+
9+
fromaddr = "<YOUR GMAIL EMAIL ADDRESS>"
10+
toaddr = "<DESIRED EMAIL ADDRESS>"
11+
12+
msg = MIMEMultipart()
13+
14+
# Turn on "Less Secure App for Google accounts. Refer - https://support.google.com/accounts/answer/6010255?hl=en"
15+
16+
msg['From'] = fromaddr
17+
msg['To'] = toaddr
18+
19+
#Make text file in the same directory for storing current value and avoid repititive emails.
20+
21+
PATH_INPUT = "/<Whatever directory>/current_ip.txt"
22+
23+
get_request = requests.get("http://www.ipvoid.com")
24+
soup = bs4(get_request.content,'lxml')
25+
current_ip = soup.find('input', {'name':''})['value']
26+
print "Current IP is " + current_ip
27+
28+
with open(PATH_INPUT, "r") as f:
29+
existing_IP = f.readlines()
30+
for line in existing_IP:
31+
existing_IP = str(line).strip('\n')
32+
f.close()
33+
34+
if existing_IP == current_ip:
35+
print "no email sent!"
36+
37+
else:
38+
msg['Subject'] = "Current IP is " + current_ip
39+
server = smtplib.SMTP('smtp.gmail.com', 587)
40+
server.ehlo()
41+
server.starttls()
42+
server.ehlo()
43+
server.login(fromaddr, '<PASSWORD>') #logins
44+
text = msg.as_string()
45+
46+
server.sendmail(fromaddr, toaddr, text) #sends email
47+
48+
with open(PATH_INPUT, "w") as f:
49+
f.write(str(current_ip + "\n"))
50+
f.close()

0 commit comments

Comments
 (0)
0