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

 更新时间:2014年10月23日 08:58:41   投稿:junjie  
这篇文章主要介绍了Perl脚本实现检测主机心跳信号功能,本文代码也可作为perl串口通信的实例,需要的朋友可以参考下

使用串口通信,在备机端使用如下脚本检测来自主机的心跳信号,一旦未接受次数超过指定记数,备机认为主机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脚本实现检测主机心跳信号功能

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

    这篇文章主要介绍了Perl脚本实现检测主机心跳信号功能,本文代码也可作为perl串口通信的实例,需要的朋友可以参考下
    2014-10-10
  • 如何使用perl的Tie::File 模块删除文件固定行

    如何使用perl的Tie::File 模块删除文件固定行

    使用perl的Tie::File 模块删除文件固定行, 这里的处理主要利用了perl的Tie::File 模块把数组和文件绑定,然后就可以使用perl的splice函数操作数组,从而达到操作文件的目的,对perl删除文件固定行感兴趣的朋友跟随小编一起看看吧
    2023-12-12
  • perl哈希hash的常见用法介绍

    perl哈希hash的常见用法介绍

    哈希在perl是非常重要且常用的,本文为大家介绍一些常见的用法,供大家学习参考
    2013-02-02
  • 使用 use re debug 查看正则表达式的匹配过程

    使用 use re debug 查看正则表达式的匹配过程

    使用 use re 'debug' 查看正则表达式的匹配过程,参见如下的代码
    2013-02-02
  • perl面向对象实例

    perl面向对象实例

    这篇文章主要介绍了perl面向对象实例,本文讲解了一个类只是一个简单的包、对象仅仅只是引用、一个方法就是一个简单的子程序等内容,并给出了一个简单示例,需要的朋友可以参考下
    2014-11-11
  • python 获取命令行参数 函数

    python 获取命令行参数 函数

    perl下获取命令行的参数跟数量的函数。
    2009-04-04
  • Perl实现删除Windows下的图片缓存缩略图Thumbs.db

    Perl实现删除Windows下的图片缓存缩略图Thumbs.db

    这篇文章主要介绍了Perl实现删除Windows下的图片缓存缩略图Thumbs.db,本文实现了批量删除Thumbs.db文件,需要的朋友可以参考下
    2014-12-12
  • Perl函数(子程序)学习笔记

    Perl函数(子程序)学习笔记

    这篇文章主要介绍了Perl函数(子程序)学习笔记,本文讲解了函数定义、函数返回值、函数参数传递等内容,需要的朋友可以参考下
    2014-11-11
  • Perl时间处理函数用法介绍

    Perl时间处理函数用法介绍

    本文重点讨论Perl时间处理函数的概念,Perl能在绝大多数操作系统运行,可以方便地向不同操作系统迁移,并且Perl借取了C、sed、awk、shellscripting以及很多其他程序语言的特性
    2017-09-09
  • perl命令行参数内建数组@ARGV浅析

    perl命令行参数内建数组@ARGV浅析

    这篇文章主要介绍了perl命令行参数内建数组@ARGV浅析,本文重点在于讲解@ARGV的用法,并通过实例来说明,需要的朋友可以参考下
    2014-06-06

最新评论