Perl脚本实现检测主机心跳信号功能

所属分类: 脚本专栏 / perl 阅读数: 445
收藏 0 赞 0 分享

使用串口通信,在备机端使用如下脚本检测来自主机的心跳信号,一旦未接受次数超过指定记数,备机认为主机DOWN机,自动设置为主机的网络参数,顶替主机提供服务。

# ! 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 be tolerated 
 my  $interface  =  ' 
 
# ---------------------------------- 
# 接口 IP 配置     
# ---------------------------------- 
pushd interface ip


# "local" 的接口 IP 配置

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 addr=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
# 接口 IP 配置结束


 ' ; # 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 from remote machine
  # change ip address after designated count of failture 
   
   my ( $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 ;

更多精彩内容其他人还在看

perl 指定长度并生成一个随机的DNA序列的脚本代码

perl 指定长度并生成一个随机的DNA序列的代码,有需要的朋友可以参考下
收藏 0 赞 0 分享

perl生成特定碱基比例的随机序列的代码

怎么用perl程序,随机生成一条序列,使ACGT四种碱基的含量分别为0.3,0.3,0.2,0.2!
收藏 0 赞 0 分享

学习perl的unless控制结构

在perl的if控制结构中,只有当条件表达式为真时才执行某块代码。如果想让程序块在条件为假时才执行,此时可以把if改成unless
收藏 0 赞 0 分享

有关perl正则表达式的一些杂项

有关perl正则表达式的一些杂项,有需要的朋友可以参考下
收藏 0 赞 0 分享

perl中heredoc使用说明

在成块打印文本的时候特别有用,需要的朋友可以参考下
收藏 0 赞 0 分享

Perl使用chdir的实例代码

Perl使用chdir的例子,供朋友们参考学习
收藏 0 赞 0 分享

perl特殊符号及默认的内部变量

perl特殊符号及默认的内部变量,有需要的朋友不妨参考下
收藏 0 赞 0 分享

perl中my与our的区别介绍

our 和 my 一样,都是对变量的声明,不过 our 声明的是包全局变量,而 my 声明的是词法变量
收藏 0 赞 0 分享

在vim中添加perl注释时无法对齐问题的解决方法

在使用vim编辑perl脚本时,每当输入#号时,#号都会跑到行首问题,需要的朋友可以参考下
收藏 0 赞 0 分享

perl 文件操作总结

perl 文件操作总结,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多