Guest User

Untitled

a guest
Jan 26th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. # ---------------------------------------
  7. # ------- ipchange_notify.pl ------------
  8. # ---------------------------------------
  9. # Version: 0.7a
  10. # Check for changes in WAN IP address
  11. # (dynamic). If there is a change since
  12. # the last check, send new IP in email.
  13. # ---------------------------------------
  14. # 2011-02-01
  15. # By: Philip Smith
  16. # ---------------------------------------
  17.  
  18. use Data::Dumper;
  19. use Email::Send;
  20. use Email::Send::Gmail;
  21. use Email::Simple::Creator;
  22. use HTML::Strip;
  23. use LWP::Simple;
  24. use Path::Class qw( file );
  25. use YAML qw( LoadFile );
  26. use YAML::Dumper;
  27.  
  28.  
  29. # load conf yaml
  30. my $config_dir = '.config';
  31. my $default_conf = file( $config_dir, '', 'ipchange_notify.yaml' );
  32. my $config = LoadFile( -e $default_conf
  33. ? $default_conf
  34. : die "Cannot load conf! $@\n" );
  35.  
  36. # check wan ip4 address
  37. my $url = $config->{urls}{check_ip};
  38. my $old_ip = $config->{local_info}{old_ip};
  39. my $new_ip = get_ip($url);
  40. my $diff = 0;
  41. if ( $new_ip ne $old_ip ) {
  42. $diff = 1;
  43. send_email();
  44. update_conf();
  45. }
  46.  
  47.  
  48. sub update_conf {
  49. my $fh_r;
  50. my $fh_w;
  51. my $file = './.config/ipchange_notify.yaml';
  52. open( $fh_r, '<', $file )
  53. or die "Cannot open $file to read: $!\n";
  54. my $line;
  55. my @lines = <$fh_r>;
  56.  
  57. foreach $line ( @lines ) {
  58. if ( $line =~ m/$old_ip/ ) {
  59. $line =~ s/$old_ip/$new_ip/;
  60. }
  61. }
  62. close( $fh_r );
  63. open( $fh_w, '>', $file )
  64. or die "Cannot open $file to write: $!\n";
  65. foreach $line ( @lines ) { print $fh_w $line; }
  66. close( $fh_w );
  67. }
  68.  
  69. sub send_email {
  70. # set up email object
  71. my $email = Email::Simple->create(
  72. header => [
  73. From => $config->{email_info}{from_email},
  74. To => $config->{email_info}{to_email},
  75. Suject => 'IP Address Change Notification',
  76. ],
  77. body => "New IP: $new_ip",
  78. );
  79.  
  80. # send the email notification
  81. my $sender = Email::Send->new(
  82. { mailer => 'Gmail',
  83. mailer_args => [
  84. username => $config->{email_info}{username},
  85. password => $config->{email_info}{password},
  86. ]
  87. }
  88. );
  89. eval ( $sender->send($email) ) if $diff;
  90. die "Error sending email: $@\n" if $@;
  91. }
  92.  
  93. sub get_ip {
  94. my $url = shift;
  95. my $html = get("$url")
  96. or die "Could not fetch index.html: $@\n";
  97. my $stripper = HTML::Strip->new();
  98. my $naked = $stripper->parse( $html );
  99. $stripper->eof;
  100. $new_ip = $1 if $naked =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/;
  101. return $new_ip;
  102. }
Add Comment
Please, Sign In to add comment