Advertisement
swaggboi

Untitled

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