• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >perl > Perl脚本实现检测主机心跳信号功能

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

作者:junjie 字体:[增加 减小] 来源:互联网

junjie 通过本文主要向大家介绍了perl脚本,perl脚本下载,perl脚本实例,perl脚本怎么运行,perl脚本编辑器等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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

</div>

</div>

您可能想查找下面的文章:

  • perl中使用signal(信号)实例
  • perl读写文件代码实例
  • Perl脚本实现递归遍历目录下的文件
  • Perl脚本实现检测主机心跳信号功能
  • perl实现检测服务器中的服务是否正常脚本分享
  • perl 采集入库脚本分享
  • Perl中使用MIME::Lite发送邮件实例
  • perl脚本学习指南--读书笔记
  • perl脚本实现限制ssh最大登录次数(支持白名单)
  • perl查找进程PID的例子

相关文章

  • perl哈希hash的常见用法介绍
  • perl中子程序中参数的两种引用(传递)方式介绍
  • Perl函数(子程序)学习笔记
  • perl中尖括号运算符(<>)使用说明
  • perl中heredoc使用说明
  • 学习perl的unless控制结构
  • perl处理xml的模块介绍
  • 使用Perl创建指定编码格式(如utf-8)文件的实现代码
  • PERL 正则表达式详细说明
  • perl中的字符串操作函数chomp与chop介绍

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 用perl实现生物突变的随机模拟程序代码
    • perl文件读取的几种处理方式小结
    • Perl List::Util模块使用实例
    • perl 删除数组元素的几种方法小结
    • python urllib中的编码处理示例
    • Perl中使用File::Lockfile确保脚本单实例运行
    • Perl 获取shell命令的执行结果
    • Perl图形化包管理工具PPM学习使用笔记
    • perl中使用ip138网站查询ip归属地脚本分享
    • perl Socket编程实例代码

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有