Jump to content

Help making a simple Linux program

Jaybird
Go to solution Solved by fizzlesticks,

Instead of using curl shouldn't you probe your nic directly? No need to rely on a second service unless you're testing that as well?

 

Get IP with: ifconfig eth0 | grep "inet addr" | awk '{print $2}' | grep -oP '(?<=addr:).*'

 

Assuming you are on eth0 and not some other interface.

Don't know if it's because I'm running Linux in a VM but that doesn't return my public IP.

 

@Jaybird If you have a gmail account you can use this Python3 script I just happen to have laying around.

import json, urllib.request, time, smtplibusername = "XXX@gmail.com"password = "xxxx"def getIP():	try:		ip = json.loads(urllib.request.urlopen('http://httpbin.org/ip').read().decode())['origin']		return ip	except:		return Nonedef sendEmail(ip):	try:		msg = 'From: {}\r\nTo: {}\r\n\r\nNew IP: {}\r\n'.format(username,username,ip)		server = smtplib.SMTP('smtp.gmail.com:587')		server.starttls()		server.login(username,password)		server.sendmail(username, username, msg)		server.quit()		return True	except:		return Falsedef main():	ip = getIP()	while True:		newIP = getIP()		if not newIP:			time.sleep(30)			continue		if ip != newIP:			while not sendEmail(newIP): 				pass			print("Got new IP: {}".format(newIP))		ip = newIP		time.sleep(600)if __name__ == '__main__':	main()

I'm looking for a program or making a program that will email me when my public ip changes so if I am out of town I can still ssh into my server. I've tried some programs before but I have not been able to get any of those to work. I'd appreciate the help.

CM Storm Switch Tester MOD (In-Progress) - http://linustechtips.com/main/topic/409147-cm-storm-switch-tester-macro-mod/


       Ammo Can Speaker 02 (Completed) - http://linustechtips.com/main/topic/283826-ammo-can-speakers-02/       A/B Switch V 0.5 (Completed) - http://linustechtips.com/main/topic/362417-ab-switch-v0


     Build 01 - The Life of a Prodigy -  http://linustechtips.com/main/topic/13103-build-01-the-life-of-a-prodigy/             Build 02 - Silent Server 3000 - http://linustechtips.com/main/topic/116670-build-02-silent-server-3000/

Link to comment
Share on other sites

Link to post
Share on other sites

I'm looking for a program or making a program that will email me when my public ip changes so if I am out of town I can still ssh into my server. I've tried some programs before but I have not been able to get any of those to work. I'd appreciate the help.

Try a free no-ip.com account. It gives you a hostname and redirects it to whatever your IP is.

[spoiler=My Current PC]AMD FX-8320 @ 4.2 Ghz | Xigmatek Dark Knight Night Hawk II | Gigabyte GA-990FXA-UD3 | 8GB Adata XPG V2 Silver 1600 Mhz RAM | Gigabyte 3X Windforce GTX 770 4GB @ 1.27 Ghz/7.25 Ghz | Rosewill Hive 550W Bronze PSU | Fractal Design Arc Midi R2 | Samsung Evo 250 GB SSD | Seagate Barracuda 1TB HDD | ASUS VS239H-P | Razer Deathadder 2013 Partlist

 

LTT Build-Off Thread: http://linustechtips.com/main/topic/35226-the-ltt-build-off-thread-no-building-required/

Link to comment
Share on other sites

Link to post
Share on other sites

With no-ip you can download a program to your computer that will tell no-ip your IP address. You get a free name address (like "yourpc.somedomain.com") and you will always be able to connect to yourpc.somedomain.com because it will always know the latest IP of your router, and will redirect you there.

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
Share on other sites

Link to post
Share on other sites

I'm looking for a program or making a program that will email me when my public ip changes so if I am out of town I can still ssh into my server. I've tried some programs before but I have not been able to get any of those to work. I'd appreciate the help.

Put this command in a cron job

mail -s "IP Adress $(curl -s checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//')" YOUR@EMAIL.HERE

I didnt test if it works, but it should.

Link to comment
Share on other sites

Link to post
Share on other sites

Try a free no-ip.com account. It gives you a hostname and redirects it to whatever your IP is.

Not exactly what I am looking for. I really only need something that checks the current public ip, verify that has/has not changed and if it has changed, it will send me an email with the updated ip.

CM Storm Switch Tester MOD (In-Progress) - http://linustechtips.com/main/topic/409147-cm-storm-switch-tester-macro-mod/


       Ammo Can Speaker 02 (Completed) - http://linustechtips.com/main/topic/283826-ammo-can-speakers-02/       A/B Switch V 0.5 (Completed) - http://linustechtips.com/main/topic/362417-ab-switch-v0


     Build 01 - The Life of a Prodigy -  http://linustechtips.com/main/topic/13103-build-01-the-life-of-a-prodigy/             Build 02 - Silent Server 3000 - http://linustechtips.com/main/topic/116670-build-02-silent-server-3000/

Link to comment
Share on other sites

Link to post
Share on other sites

A bash or perl script can do that, have it run in cron repeatedly to check for the IP change and then mail you info or a note.

I roll with sigs off so I have no idea what you're advertising.

 

This is NOT the signature you are looking for.

Link to comment
Share on other sites

Link to post
Share on other sites

Put this command in a cron job

mail -s "IP Adress )" YOUR@EMAIL.HERE
I didnt test if it works, but it should.

Assuming that works, I'm sitting in my engineering class at the moment, I can run a crontab: said script every 10 minutes. Said script will: look something like this:

Curl $(curl -s checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//')

If "oldip" = "new ip"

Then do nothing

If "oldip" /= "new ip"

Then mail -s "IP Adress $(curl -s checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//' )" YOUR@EMAIL.HERE

(Update oldip with newip content)

(Delete newip file)

CM Storm Switch Tester MOD (In-Progress) - http://linustechtips.com/main/topic/409147-cm-storm-switch-tester-macro-mod/


       Ammo Can Speaker 02 (Completed) - http://linustechtips.com/main/topic/283826-ammo-can-speakers-02/       A/B Switch V 0.5 (Completed) - http://linustechtips.com/main/topic/362417-ab-switch-v0


     Build 01 - The Life of a Prodigy -  http://linustechtips.com/main/topic/13103-build-01-the-life-of-a-prodigy/             Build 02 - Silent Server 3000 - http://linustechtips.com/main/topic/116670-build-02-silent-server-3000/

Link to comment
Share on other sites

Link to post
Share on other sites

Instead of using curl shouldn't you probe your nic directly? No need to rely on a second service unless you're testing that as well?

 

Get IP with: ifconfig eth0 | grep "inet addr" | awk '{print $2}' | grep -oP '(?<=addr:).*'

 

Assuming you are on eth0 and not some other interface.

I roll with sigs off so I have no idea what you're advertising.

 

This is NOT the signature you are looking for.

Link to comment
Share on other sites

Link to post
Share on other sites

Instead of using curl shouldn't you probe your nic directly? No need to rely on a second service unless you're testing that as well?

 

Get IP with: ifconfig eth0 | grep "inet addr" | awk '{print $2}' | grep -oP '(?<=addr:).*'

 

Assuming you are on eth0 and not some other interface.

Don't know if it's because I'm running Linux in a VM but that doesn't return my public IP.

 

@Jaybird If you have a gmail account you can use this Python3 script I just happen to have laying around.

import json, urllib.request, time, smtplibusername = "XXX@gmail.com"password = "xxxx"def getIP():	try:		ip = json.loads(urllib.request.urlopen('http://httpbin.org/ip').read().decode())['origin']		return ip	except:		return Nonedef sendEmail(ip):	try:		msg = 'From: {}\r\nTo: {}\r\n\r\nNew IP: {}\r\n'.format(username,username,ip)		server = smtplib.SMTP('smtp.gmail.com:587')		server.starttls()		server.login(username,password)		server.sendmail(username, username, msg)		server.quit()		return True	except:		return Falsedef main():	ip = getIP()	while True:		newIP = getIP()		if not newIP:			time.sleep(30)			continue		if ip != newIP:			while not sendEmail(newIP): 				pass			print("Got new IP: {}".format(newIP))		ip = newIP		time.sleep(600)if __name__ == '__main__':	main()

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

 

Don't know if it's because I'm running Linux in a VM but that doesn't return my public IP.

 

@Jaybird If you have a gmail account you can use this Python3 script I just happen to have laying around.

import json, urllib.request, time, smtplibusername = "XXX@gmail.com"password = "xxxx"def getIP():	try:		ip = json.loads(urllib.request.urlopen('http://httpbin.org/ip').read().decode())['origin']		return ip	except:		return Nonedef sendEmail(ip):	try:		msg = 'From: {}\r\nTo: {}\r\n\r\nNew IP: {}\r\n'.format(username,username,ip)		server = smtplib.SMTP('smtp.gmail.com:587')		server.starttls()		server.login(username,password)		server.sendmail(username, username, msg)		server.quit()		return True	except:		return Falsedef main():	ip = getIP()	while True:		newIP = getIP()		if not newIP:			time.sleep(30)			continue		if ip != newIP:			while not sendEmail(newIP): 				pass			print("Got new IP: {}".format(newIP))		ip = newIP		time.sleep(600)if __name__ == '__main__':	main()

ah yus! see i knew there was a simple program out there. yes I do have a personal gmail account for my server.

 

I am stil slowly learning linux and very slowly understanding programs. I know you can use crontab to run a program at set intervals but for this python program, I dont need a crontab setting to activate it? 

CM Storm Switch Tester MOD (In-Progress) - http://linustechtips.com/main/topic/409147-cm-storm-switch-tester-macro-mod/


       Ammo Can Speaker 02 (Completed) - http://linustechtips.com/main/topic/283826-ammo-can-speakers-02/       A/B Switch V 0.5 (Completed) - http://linustechtips.com/main/topic/362417-ab-switch-v0


     Build 01 - The Life of a Prodigy -  http://linustechtips.com/main/topic/13103-build-01-the-life-of-a-prodigy/             Build 02 - Silent Server 3000 - http://linustechtips.com/main/topic/116670-build-02-silent-server-3000/

Link to comment
Share on other sites

Link to post
Share on other sites

 I dont need a crontab setting to activate it? 

Nope, just start it and it will keep running.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

Nope, just start it and it will keep running.

should be last question. 

I assume this scrip will send an email from my server email address to my server email address. but what if I want it sent to my personal email address. 

 

would i change 

 

server.sendmail(username, username, msg)

to

server.sendmail(other@gmail.com, username, msg)

CM Storm Switch Tester MOD (In-Progress) - http://linustechtips.com/main/topic/409147-cm-storm-switch-tester-macro-mod/


       Ammo Can Speaker 02 (Completed) - http://linustechtips.com/main/topic/283826-ammo-can-speakers-02/       A/B Switch V 0.5 (Completed) - http://linustechtips.com/main/topic/362417-ab-switch-v0


     Build 01 - The Life of a Prodigy -  http://linustechtips.com/main/topic/13103-build-01-the-life-of-a-prodigy/             Build 02 - Silent Server 3000 - http://linustechtips.com/main/topic/116670-build-02-silent-server-3000/

Link to comment
Share on other sites

Link to post
Share on other sites

should be last question. 

I assume this scrip will send an email from my server email address to my server email address. but what if I want it sent to my personal email address. 

 

would i change 

 

server.sendmail(username, username, msg)

to

server.sendmail(other@gmail.com, username, msg)

Changing that line and

msg = 'From: {}\r\nTo: {}\r\n\r\nNew IP: {}\r\n'.format(username,'other@gmail.com',ip)

should do it.

 

edit:

server.sendmail(other@gmail.com, username, msg)

should be

server.sendmail(username, other@gmail.com, msg)

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

Changing that line and

msg = 'From: {}\r\nTo: {}\r\n\r\nNew IP: {}\r\n'.format(username,'other@gmail.com',ip)

should do it.

 

edit:

server.sendmail(other@gmail.com, username, msg)

should be

server.sendmail(username, other@gmail.com, msg)

 

 

looking at the code again today, viewing from my ipad, so i decided to run it and it came back with a invalid syntax. This was the line with the error and what the error was. what do you think?

 
msg = 'From: {}\r\nTo: {}\r\n\r\nNew IP: {}\r\n'.format(username,personal@gmail.com,ip)

CM Storm Switch Tester MOD (In-Progress) - http://linustechtips.com/main/topic/409147-cm-storm-switch-tester-macro-mod/


       Ammo Can Speaker 02 (Completed) - http://linustechtips.com/main/topic/283826-ammo-can-speakers-02/       A/B Switch V 0.5 (Completed) - http://linustechtips.com/main/topic/362417-ab-switch-v0


     Build 01 - The Life of a Prodigy -  http://linustechtips.com/main/topic/13103-build-01-the-life-of-a-prodigy/             Build 02 - Silent Server 3000 - http://linustechtips.com/main/topic/116670-build-02-silent-server-3000/

Link to comment
Share on other sites

Link to post
Share on other sites

 

looking at the code again today, viewing from my ipad, so i decided to run it and it came back with a invalid syntax. This was the line with the error and what the error was. what do you think?

 
msg = 'From: {}\r\nTo: {}\r\n\r\nNew IP: {}\r\n'.format(username,personal@gmail.com,ip)

 

You need quotes around 'personal@gmail.com'

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

You need quotes around 'personal@gmail.com'

 

I knew that. ok next error. 

Traceback (most recent call last):  File "ipnote.py", line 1, in <module>    import json, urllib.request, time, smtplibImportError: No module named request

CM Storm Switch Tester MOD (In-Progress) - http://linustechtips.com/main/topic/409147-cm-storm-switch-tester-macro-mod/


       Ammo Can Speaker 02 (Completed) - http://linustechtips.com/main/topic/283826-ammo-can-speakers-02/       A/B Switch V 0.5 (Completed) - http://linustechtips.com/main/topic/362417-ab-switch-v0


     Build 01 - The Life of a Prodigy -  http://linustechtips.com/main/topic/13103-build-01-the-life-of-a-prodigy/             Build 02 - Silent Server 3000 - http://linustechtips.com/main/topic/116670-build-02-silent-server-3000/

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×