Perl script realizes heartbeat signal function of host _perl

Source: Internet
Author: User
Tags eval sprintf win32 perl script

Using serial communication, use the following script at the standby to detect heartbeat signal from the host, once the number of not accepted more than the specified number, the standby machine think the host down machine, automatically set as the host network parameters, replace the host to provide services.

# !
 perl-w use strict;

 Use Win32:: SerialPort; My $port = ' COM2 '; # Serail Port name registried in OS my $count = 0; # count Number of heartbeat-receiving failture my $max _count = 5; # Max Fail Count to is tolerated my $interface = ' #----------------------------------# interface IP configuration #------- ---------------------------pushd Interface IP # ' local ' interface IP configuration set address name= ' local ' source=static addr=192.168 .6.185 mask=255.255.255.0 Set address name= ' local ' gateway=192.168.6.65 gwmetric=0 set DNS name= ' local ' source=static add r=192.168.6.112 register=primary Add DNS name= "local" addr=192.168.6.201 index=2 set wins name= "local" source=static addr =none POPD # interface IP configuration end '; # Net Inetrface Config information sub ErrLog {# Log the Failtrue occuring time Open ERR, ' >>err.
   Log ';
   My @time = LocalTime ();  My $time = sprintf ("%d", $time [5] + 1900).  ' - ' 
             . sprintf ("%d", $time [4] + 1).  ' - ' 
             .  "$time [3]".  '  ' 
             .  sprintf ("%.2d", $time [2]).  ' : ' 
             .  sprintf ("%.2d", $time [1]).  ' : ' 
             .
   sprintf ("%.2d", $time [0]); Print ERR $time.
   "\ n";
Close ERR;
   Sub Ipchange {# change IP addrress open TMP, ' >tmp ';
   Print TMP $interface;
  
   Close TMP;
     eval {System ' netsh-f tmp ';
  Unlink ' tmp ';
  
   };
   if ($@) {return 0;}
return 1;
 My $ob = Win32:: SerialPort-> New ($port) or die "Cannot OPEN $port";
   # Open serial Port eval {# Set serial port Properties $ob-> baudrate (9600);
   $ob-> parity (' none ');
   $ob-> databits (8);
   $ob-> stopbits (1);
$ob-> handshake (' none ');
  
 };

 if ($@) {die ' SET FAILED ';}
 $ob-> write_settings or die "Cannot write $port DRIVER";# Write to port driver to make it active while (1) {# Loop receiving Heartbeat ' Remote machine # change IP
  
   Address after designated Count of Failture me ($length, $result) = $ob-> read (10);
  if ($result) {$count = 0;
     } elsif ($count = = $max _count) {errlog ();
     if (not Ipchange ()) {print ' IP change FAILED ';}
  Last;
  else {$count + +;
Sleep (1);

 } undef $ob;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.