Advertisement
swaggboi

Untitled

May 30th, 2019
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.61 KB | None | 0 0
  1. root@swaggserver-01:~/bin# ./pingy-swagg.pl 10.169.169.169
  2. pinging 10.169.169.169 via ICMP... not reachable!!
  3. restart zebra ospfd ospf6d... Failed to restart zebra\x20ospfd\x20ospf6d.service: Unit not found.
  4. Done!!
  5. waiting 9 seconds to cont... root@swaggserver-01:~/bin#
  6. root@swaggserver-01:~/bin# vi pingy-swagg.pl
  7. root@swaggserver-01:~/bin# cat pingy-swagg.pl
  8. #!/usr/bin/env perl
  9. #
  10. # danielbowling424@msn.com
  11. # http://swagg.cc/
  12. #
  13. # Copyright 2019 Daniel Bowling, Mount Rainier, Maryland, USA
  14. # All rights reserved.
  15. #
  16. # Redistribution and use of this script, with or without modification, is
  17. # permitted provided that the following conditions are met:
  18. #
  19. # 1. Redistributions of this script must retain the above copyright
  20. #    notice, this list of conditions and the following disclaimer.
  21. #
  22. #  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
  23. #  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  24. #  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
  25. #  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. #  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27. #  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  28. #  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. #  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  30. #  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  31. #  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32.  
  33. use strict;
  34. use warnings;
  35. use Net::Ping; # https://perldoc.perl.org/Net/Ping.html
  36. use Regexp::Common qw(net); # Net::Ping must be up-to-date for IPv6!!
  37.  
  38. my $p4 = Net::Ping->new("icmp");
  39. my $p6 = Net::Ping->new("icmpv6");
  40. my $svmgr = "systemctl";
  41. my $svact = "restart";
  42. my @svcs = ("zebra", "ospfd", "ospf6d");
  43. my $sleepy = 9;
  44.  
  45. sub rest_svc {
  46.     print "not reachable!!\n$svact @svcs... ";
  47.     system("$svmgr", "$svact", "@svcs");
  48.     print "Done!!\nwaiting $sleepy seconds to cont... ";
  49.     sleep ($sleepy);
  50. }
  51.  
  52. sub ping_ip {
  53.     if ($_ =~ m/$RE{net}{IPv4}/) {
  54.         print "pinging $_ via ICMP... ";
  55.         if ($p4->ping($_, 2)) {
  56.             print "success!!\n"
  57.         } else {
  58.             rest_svc();
  59.         }
  60.     } elsif ($_ =~ m/$RE{net}{IPv6}/) {
  61.         print "pinging $_ via ICMPv6... ";
  62.         if ($p6->ping($_, 2)) {
  63.             print "success!!\n"
  64.         } else {
  65.             rest_svc();
  66.         }
  67.     }
  68. }
  69.  
  70. foreach (@ARGV)
  71. {
  72.     if ($_ =~ m/[A-F]*/) {
  73.         $_ =~ tr/[A-F]/[a-f]/; # toggle case
  74.     }
  75.     ping_ip($_);
  76. }
  77.  
  78. $p4->close();
  79. $p6->close();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement