{"id":109,"date":"2010-07-09T23:20:46","date_gmt":"2010-07-09T23:20:46","guid":{"rendered":"http:\/\/soliloquyforthefallen.net\/blog\/?p=109"},"modified":"2015-03-05T07:18:37","modified_gmt":"2015-03-05T07:18:37","slug":"email-updates-on-server-ip-change","status":"publish","type":"post","link":"https:\/\/soliloquyforthefallen.net\/?p=109","title":{"rendered":"Email Updates on Server IP Change"},"content":{"rendered":"<p>Here&#8217;s a script I wrote in perl (yes perl) that checks your ip and will email you if the ip changes.  Because I&#8217;m using private IP&#8217;s, I need to check via an external server.  What I want to know is if my IP changes, because if it does, I probably need to restart my IRC Bot.  Because of the private IP mess, I need to use an external to get my external IP.  I use a simple PHP script on my server from Fivebean to tell me the IP.<br \/>\nThe PHP is:<\/p>\n<pre>&lt;?php\necho $_SERVER['REMOTE_ADDR'];\n?&gt;<\/pre>\n<p>Stick that somewhere that&#8217;s not on your local LAN.<br \/>\nNow, make a batch file with this<br \/>\n<code>perl ip_checker.pl location_of_previous_php_script file_to_write_some_info_to<\/code><\/p>\n<p>Coincidentally, I think it would be best if I maybe removed those option and just put them in the configuration section of the perl script . . .<\/p>\n<p>Copy this and save this within the same place as that bat file, using the name ip_checker.pl.  Make sure you have Net::SMTP::SSL installed in your perl modules.<\/p>\n<pre><code>use LWP::Simple;\nuse Net::SMTP::SSL;\n\nuse warnings; use strict;\nsub text;\n\n#texting information\n#configure whether to send alerts.\u00a0 Email based\n################################\n######Script configuration######\n################################\n################################\n#####Options Confiugration######\n################################\nmy $send_alert = 'true'; #set true to allow messages to be sent informing you of changes to the local IP\n################################\n#####Email send information#####\n################################\n\nmy $email_from = '';\nmy $email_password = '';\nmy $send_to= '';\nmy $email_host ='smtp.gmail.com'; #something link that anyway\n\n################################\n##End of Configuration Section##\n################################\n\nif (@ARGV ne 2){\n\u00a0\u00a0\u00a0 die 'Usage: perl ipchecker.pl (URL To Retrieve External IP From) (File to ReadWrite LastCurrent IP From)n';\n}\n\nmy $ip_bouncer = shift;\nmy $read_file_location = shift;\nmy $current_ip = get($ip_bouncer); die \"Couldn't get it!\" unless $current_ip;\nmy $old_ip; my $new_ip;\n\nmy $read_file; my $print_file;\n\nopen OLD_IP, \"$read_file_location\" or die \"$!\"; #open with write rights\n$old_ip = &lt;OLD_IP&gt;;\nclose OLD_IP;\nopen OLD_IP, \"&gt;$read_file_location\" or die \"$!\"; #open with write rights\n\nprint \"Current IP Address: $current_ip n\";\nprint \"Last IP Address: $old_ip n\";\nprint OLD_IP $current_ip;\nclose OLD_IP;\n\nif ($current_ip ne $old_ip){\u00a0 #ne is a string comparison of equal or not == is numeric comparison\n\u00a0\u00a0\u00a0 #\/&amp;text ($host, $from, $password, $message );\n\u00a0\u00a0\u00a0 print \"Alert!\u00a0 IP address has changedn\";\n\u00a0\u00a0\u00a0 if ($send_alert eq 'true'){\n\u00a0\u00a0\u00a0 print \"Sending Alert!n\";\n\u00a0\u00a0\u00a0 my $desired_message = \"IP Change $current_ip\";\n\u00a0\u00a0\u00a0 &amp;text ( $send_to, $email_host, $email_from, $email_password, $desired_message);\n\u00a0\u00a0\u00a0 print \"Alert Sentn\";\n\u00a0\u00a0\u00a0 }\n} else {\n\u00a0\u00a0\u00a0 print \"All is welln\";\n\u00a0\u00a0\u00a0 }\n\nsub text { # ($to, $host, $from, $password, $message )\n\u00a0\u00a0\u00a0 #mostly from http:\/\/robertmaldon.blogspot.com\/2006\/10\/sending-email-through-google-smtp-from.html\n\u00a0\u00a0\u00a0 #modified to suit my needs\n\n\u00a0\u00a0\u00a0 my $to = shift;\n\u00a0\u00a0\u00a0 my $host = shift;\n\u00a0\u00a0\u00a0 my $from = shift;\n\u00a0\u00a0\u00a0 my $password = shift;\n\u00a0\u00a0\u00a0 my $message = shift;\n\n\u00a0\u00a0\u00a0 my $smtp;\n\u00a0\u00a0\u00a0 if (not $smtp = Net::SMTP::SSL-&gt;new($host,\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 Port=&gt; 465,\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 Debug =&gt; 1,\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0 ) ) { die \"Couldn't connect\" };\n\u00a0\u00a0\u00a0 $smtp-&gt;auth($from, $password) || die \"Authentication failed!n\";\n\n\u00a0\u00a0\u00a0 $smtp-&gt;mail($from.\"n\" );\u00a0\u00a0\u00a0\u00a0 # use the sender's address here\n\u00a0\u00a0\u00a0 $smtp-&gt;to($to.\"n\" );\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # recipient's address\n\u00a0\u00a0\u00a0 $smtp-&gt;data();\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # Start the mail\n\n\u00a0\u00a0\u00a0 # Send the header.\n\u00a0\u00a0\u00a0 $smtp-&gt;datasend(\"To: $to n\" );\n\u00a0\u00a0\u00a0 $smtp-&gt;datasend(\"From: $from n\" );\n\u00a0\u00a0\u00a0 $smtp-&gt;datasend(\"Subject: Server Alertn\");\n\u00a0\u00a0\u00a0 $smtp-&gt;datasend('Content-Type: text\/plain; charset=ISO-8859-1;' . \"nn\");\n\u00a0\u00a0\u00a0 # Send the body.\n\u00a0\u00a0\u00a0 $smtp-&gt;datasend(\"Automated Server Message n\", $message, \"n\");\n\u00a0\u00a0\u00a0 $smtp-&gt;dataend();\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # Finish sending the mail\n\u00a0\u00a0\u00a0 $smtp-&gt;quit;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # Close the SMTP connection\n}\n<\/code><\/pre>\n<p>Schedule every 6 hours or so.  I did it with the scheduled jobs thingy in windows server 2003.<\/p>\n<p>Sorry for the poor write up, hit me up in the comments if you need help setting this up or if you have other general questions.  Right now, I have to go mess with a wordpress theme in need of fixin . . . .<\/p>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 228px; width: 1px; height: 1px; overflow: hidden;\">use warnings; use strict;<br \/>\nsub text;<\/p>\n<p>#texting information<br \/>\n#configure whether to send alerts.\u00a0 Email based<br \/>\n################################<br \/>\n######Script configuration######<br \/>\n################################<br \/>\n################################<br \/>\n#####Options Confiugration######<br \/>\n################################<br \/>\nmy $send_alert = &#8216;true&#8217;; #set true to allow messages to be sent informing you of changes to the local IP<br \/>\n################################<br \/>\n#####Email send information#####<br \/>\n################################<\/p>\n<p>my $email_from = &#8221;;<br \/>\nmy $email_password = &#8221;;<br \/>\nmy $send_to= &#8221;;<br \/>\nmy $email_host =&#8217;smtp.gmail.com&#8217;; #something link that anyway<\/p>\n<p>################################<br \/>\n##End of Configuration Section##<br \/>\n################################<\/p>\n<p>if (@ARGV ne 2){<br \/>\ndie &#8216;Usage: perl ipchecker.pl (URL To Retrieve External IP From) (File to ReadWrite LastCurrent IP From)n&#8217;;<br \/>\n}<\/p>\n<p>my $ip_bouncer = shift;<br \/>\nmy $read_file_location = shift;<br \/>\nmy $current_ip = get($ip_bouncer); die &#8220;Couldn&#8217;t get it!&#8221; unless $current_ip;<br \/>\nmy $old_ip; my $new_ip;<\/p>\n<p>my $read_file; my $print_file;<\/p>\n<p>open OLD_IP, &#8220;$read_file_location&#8221; or die &#8220;$!&#8221;; #open with write rights<br \/>\n$old_ip = &lt;OLD_IP&gt;;<br \/>\nclose OLD_IP;<br \/>\nopen OLD_IP, &#8220;&gt;$read_file_location&#8221; or die &#8220;$!&#8221;; #open with write rights<\/p>\n<p>print &#8220;Current IP Address: $current_ip n&#8221;;<br \/>\nprint &#8220;Last IP Address: $old_ip n&#8221;;<br \/>\nprint OLD_IP $current_ip;<br \/>\nclose OLD_IP;<\/p>\n<p>if ($current_ip ne $old_ip){\u00a0 #ne is a string comparison of equal or not == is numeric comparison<br \/>\n#\/&amp;text ($host, $from, $password, $message );<br \/>\nprint &#8220;Alert!\u00a0 IP address has changedn&#8221;;<br \/>\nif ($send_alert eq &#8216;true&#8217;){<br \/>\nprint &#8220;Sending Alert!n&#8221;;<br \/>\nmy $desired_message = &#8220;IP Change $current_ip&#8221;;<br \/>\n&amp;text ( $send_to, $email_host, $email_from, $email_password, $desired_message);<br \/>\nprint &#8220;Alert Sentn&#8221;;<br \/>\n}<br \/>\n} else {<br \/>\nprint &#8220;All is welln&#8221;;<br \/>\n}<\/p>\n<p>sub text { # ($to, $host, $from, $password, $message )<br \/>\n#mostly from http:\/\/robertmaldon.blogspot.com\/2006\/10\/sending-email-through-google-smtp-from.html<br \/>\n#modified to suit my needs<\/p>\n<p>my $to = shift;<br \/>\nmy $host = shift;<br \/>\nmy $from = shift;<br \/>\nmy $password = shift;<br \/>\nmy $message = shift;<\/p>\n<p>my $smtp;<br \/>\nif (not $smtp = Net::SMTP::SSL-&gt;new($host,<br \/>\nPort=&gt; 465,<br \/>\nDebug =&gt; 1,<br \/>\n) ) { die &#8220;Couldn&#8217;t connect&#8221; };<br \/>\n$smtp-&gt;auth($from, $password) || die &#8220;Authentication failed!n&#8221;;<\/p>\n<p>$smtp-&gt;mail($from.&#8221;n&#8221; );\u00a0\u00a0\u00a0\u00a0 # use the sender&#8217;s address here<br \/>\n$smtp-&gt;to($to.&#8221;n&#8221; );\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # recipient&#8217;s address<br \/>\n$smtp-&gt;data();\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # Start the mail<\/p>\n<p># Send the header.<br \/>\n$smtp-&gt;datasend(&#8220;To: $to n&#8221; );<br \/>\n$smtp-&gt;datasend(&#8220;From: $from n&#8221; );<br \/>\n$smtp-&gt;datasend(&#8220;Subject: Server Alertn&#8221;);<br \/>\n$smtp-&gt;datasend(&#8216;Content-Type: text\/plain; charset=ISO-8859-1;&#8217; . &#8220;nn&#8221;);<br \/>\n# Send the body.<br \/>\n$smtp-&gt;datasend(&#8220;Automated Server Message n&#8221;, $message, &#8220;n&#8221;);<br \/>\n$smtp-&gt;dataend();\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # Finish sending the mail<br \/>\n$smtp-&gt;quit;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # Close the SMTP connection<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a script I wrote in perl (yes perl) that checks your ip and will email you if the ip changes. Because I&#8217;m using private IP&#8217;s, I need to check via an external server. What I want to know is if my IP changes, because if it does, I probably need to restart my IRC&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/soliloquyforthefallen.net\/?p=109\">Read More<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[117],"tags":[],"_links":{"self":[{"href":"https:\/\/soliloquyforthefallen.net\/index.php?rest_route=\/wp\/v2\/posts\/109"}],"collection":[{"href":"https:\/\/soliloquyforthefallen.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/soliloquyforthefallen.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/soliloquyforthefallen.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/soliloquyforthefallen.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=109"}],"version-history":[{"count":1,"href":"https:\/\/soliloquyforthefallen.net\/index.php?rest_route=\/wp\/v2\/posts\/109\/revisions"}],"predecessor-version":[{"id":769,"href":"https:\/\/soliloquyforthefallen.net\/index.php?rest_route=\/wp\/v2\/posts\/109\/revisions\/769"}],"wp:attachment":[{"href":"https:\/\/soliloquyforthefallen.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/soliloquyforthefallen.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/soliloquyforthefallen.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}